Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable a href links using PHP

Tags:

css

php

I have a page with a menu on for logged in users

i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links

How can I disable all the links on that page if a PHP variable = 'no'

i know i can use

if($php_var == 'no') {
    //do something here
}

but I'm not sure how to disable the links?

Is there any way using CSS or Javascript to disable links?

like image 694
user2710234 Avatar asked Jan 13 '14 11:01

user2710234


People also ask

How do you make a href disabled?

It is still possible to disable a link by following 3 steps: remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.

How do you disable a hyperlink?

To remove a hyperlink but keep the text, right-click the hyperlink and click Remove Hyperlink. To remove the hyperlink completely, select it and then press Delete.

How do I remove a href from a tag?

Right-click the hyperlink text, and then click Remove Hyperlink.

Can we disable anchor tag?

Disable HTML Anchor Tag with CSS The best way to disable a link tag is by using the CSS property pointer-events: none; . When you apply pointer-events: none; to any element, all click events will be disabled. Because it applies to any element, you can use it to disable an anchor tag.


4 Answers

If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?

if($php_var === "no") {
    echo "This is the text of your link.";
} else
{
    echo "<a href="your.link">This is the text of your link.</a>";
}

But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.

like image 24
Realitätsverlust Avatar answered Sep 18 '22 03:09

Realitätsverlust


this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work

 <?php
    if($php_var === "no"){
    echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
    }

?>
like image 42
Shn Avatar answered Sep 19 '22 03:09

Shn


You would need to do the processing pre-output, PHP will not dynamically disable the href of an already created DOM element.

If you are producing the output of the links via PHP, you could do something like:

echo '<a href="' . ($php_var == 'no' ? '#' : 'actual_link.html') . '">Link</a>';

Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.

like image 29
Ryan Avatar answered Sep 18 '22 03:09

Ryan


try this

if($php_var == "no")
{
    echo '<a href="javascript:void(0);">Your Text For Link</a>';
} 
else
{
    echo '<a href="your link">Your Text For Link</a>';
}

user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.

like image 174
Satish Sharma Avatar answered Sep 19 '22 03:09

Satish Sharma