adding CSS class to link dynamically - php

Can anybody tell me how can I add a css class to this links when click on it?
This is my links :
echo '<li>Tutor</li>
<li>Institute</li>';
My problem is how I identify which link is clicked by users?

I assume you want to add a class to a link if it is the current link being viewed.
Since each link has a different GET parameter you could use that to identify it.
echo '<li><a '. (isset($_GET['tutor']) ? 'class="current"' : '') .' href="?tutor=link&subject='.urlencode($subject).'">Tutor</a></li>
<li><a '. (isset($_GET['institute']) ? 'class="current"' : '') .' href="?institute=link&subject='.urlencode($subject).'">Institute</a></li>';

Using a pure JavaScript solution you could do:
<a id="tutor" href="?tutor=link&subject='.urlencode($subject).'" onclick="changeClass(this)">Tutor</a>
function changeClass(link) {
if (link.id == "tutor") {
link.className = "current";
}
}
Edit:
You can identify which link was clicked by the link variable passed into the changeClass function. The link variable will include an id property which will tell you the identifier of the a tag.

you can achieve this using javascript or jquery
$(this).css('newclassname')
thats the example of jquery
updated
you can bind it to a function to get the hold of the clicked element
$('a').click(function() {
$(this).css('current');
alert($(this).html()); // this will pop up tutor if its clicked
});

Related

Codeigniter 3: insert an email link with framework specific syntax doesn't work

In a Codeignieter 3 application I am making, I need to output some email links from the data base.
While <?php echo $record->email; ?></td> outputs address#domain.com
<?php echo anchor('mailto:' . $record->email, '<span class="glyphicon glyphicon-envelope"></span>', 'title="Email" class="btn btn-success btn-sm"'); ?>
outputs <span class="glyphicon glyphicon-envelope"></span>
It adds the base url to the href attribute. How can I avoid this?
And how do I make a phone link, like Call me?
Thank you!
The CodeIgnIter's anchor function is only for creating hyperlinks, for creating email links use mailto function.
Currently there is no such function to create telephone links, so you can use the below function tel_link as helper.
function tel_link($telno, $title = '', $attributes = ''){
$title = (string) $title;
if ($title === ''){
$title = $telno;
}
return '<a href="tel:'.$telno.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
}
Here is an example echo tel_link('+91-1234567890', 'Click Here to Contact Me','class="tel_link"');
Source : https://www.codeigniter.com/user_guide/helpers/url_helper.html#mailto
In order to get a valid email link, use Codeigniters mailto() instead of anchor()
see more here
anchor function automatically it takes your main url (site_url), as mention above http://localhost/cicrud/index.php/ .
Below is the few example:
Please refer this https://www.codeigniter.com/user_guide/helpers/url_helper.html .
You can use mailto of safe_mailto (for spam safe hyperlink) function.

How to create an if statement for onclick on a attribute

I want to make an if statement that if they click a link with "Embed" link type, an onclick event will occur. This is my code, but it doesn't work.
I Have two link types:
External //This link will open in new tab
Embed // This is my problem, i want the embed link(video) to load in my page not open in new page or new tab that is why i need the onclick to work if they click this link type.
<a target="_blank' href="<?php get_option('url') ?>/wpwm-redirect?link_id=<?php $t_link->linkID ?>" <?php if($t_link->link_type == 'Embed') echo ' onclick="ayeLoadVideo('/wpwm-redirect?link_id=<?php $t_link->linkID ?>'); return false;"'; ?>> <?php $t_link->link_title ?> </a>
Change:
echo ' onclick="ayeLoadVideo('/wpwm-redirect?link_id=<?=$t_link->linkID ?>'); return false;"'
to:
echo ' onclick="ayeLoadVideo(\'/wpwm-redirect?link_id=' . $t_link->linkID . '\'); return false;"'
You need to escape the quotes that are inside the string, otherwise they'll end the string.
You can't use <?= inside a string. <? is used for switching from HTML code into PHP code, but you're already in PHP code when you execute echo. Use string concatenation in PHP code.

disable a href links using 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?
try this
if($php_var == "no")
{
echo 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.
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 "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
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>';
}
?>
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 'Link';
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.
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
echo 'LINK';
}
that will keep showing the link but will lead nowhere if the user is not logged in.
Or you can do :
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
//do nothing here or put a link to the login page
}
that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it.
Note that code in this answer is just a guess of your real code
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">

php function on page load

I have 2 functions that makes menu links active/inactive:
function disableCtrl() {
echo 'style="background: #999999;" onclick="return false;"';
}
function enableCtrl() {
echo ' ';
}
I'm using them like this:
<li><a href="#" <?php if (#$_POST['submit']){disableCtrl();} if (#$_GET['guest'] == "add") {enableCtrl();} ?> >MenuButton</a></span></li>
The only problem I have is that menu links must be disabled by default (when the page is loaded).
When I tried to write <?php disableCtrl(); if (#$_POST['submit'])..., the button became perma-disabled.
Is there a way to make it work, maybe with JavaScript or something else?
you need to put the 'guest' check as if and the default(disabled)mode as else.
<li><a href="#" <?php if ($_GET['guest']== "add"){enableCtrl();} else {disableCtrl();} ?>>MenuButton</a></li>
Or with a ternairy (= short if/else-> [condition ? ifTrue : ifFalse] )
<li><a href="#" <?php $_GET['guest']=="add" ? enableCtrl() : disableCtrl() ?>>MenuButton</a></li>
In combination with disabled (which is made for this exact situation):
<li><a href="#" <?=($_GET['guest']=="add" ? 'disabled="disabled"' : '')?>>MenuButton</a></li>
If you want something to vary, say, with every load of a page, use a server-side technology such as php. If you want it to vary within one page load, I.e. in response to user interaction, use a client-side technology I.e. JavaScript.
To attempt to answer the original question, or at least give you some pointers, you want something a bit like:
<ul id="menu" style="display: none;"><li>...</li>...</ul>
<a id="btn" href="#">disable menu</a>
<script>
document.getElementById('btn').addEventListener('click', function() {
document.getElementById('menu').setAttribute('style', 'display: block');
}
</script>
That's not perfect (in practice, for one thing, you don't want to just point your link to "#" since that won't do anything for those with javascript disabled) but it's a pointer in the right direction. The jQuery equivalent would be something like:
<script>
$('#btn').click(function() { $('#menu').hide(); });</script>
</script>
if that's more appealing, check out jQuery.
Keep in mind that php is server side so you can use printf to put your javascript into your page.
printf("<script>Here is my javascript code</script>");
I do this a lot with the mapping services.
For example reading in locations from a database and making markers:
public function MarkerGoogle($s)
{
$str = sprintf("\r\n var amkr%d = new google.maps.Marker();\r\n", $s);
// More php code here …

make my current link active

Well lets try to explain am sorry about my english.
I have some xml files where I get the url from with some php scripts
everything goes right the only prob is i want to change the li BGcolor of the selected link like in css :active or giving only that link eg a class="current"
this below make dynamically the urls to the data
echo "<ul><li ><a href='?xml1=".$xmlGet."&link=".$link." '>".$slide->title."</a></li></ul><br/>";
with the above i get a list of links not only one like below as urls
http://localhost/html5/playerEnd/hoofdstuks.php?xml1=chapter_3733&link=1
http://localhost/html5/playerEnd/hoofdstuks.php?xml1=chapter_3733&link=2
http://localhost/html5/playerEnd/hoofdstuks.php?xml1=chapter_3733&link=3
etc etc
and it display as menu like this
link 1
link 2
link 3
link 4
etc etc
each link load a different data to my page when clicked so i want the one clicked to be active like an other color or something.
Use $_GET['link'] to find out which link has been clicked. Then add a class to the link which corresponds to this. You'll have to define the active class.
$linkID = $_GET['link'];
echo "<ul><li ><a href='?xml1=".$xmlGet."&link=".$link." '";
if ($linkID == $link) { echo " class=\"active\" "; }
echo ">".$slide->title."</a></li></ul><br/>";
And if you want the li to have the class (as asked in comments):
$linkID = $_GET['link'];
echo "<ul><li";
if ($linkID == $link) { echo " class=\"active\" "; }
echo "><a href='?xml1=".$xmlGet."&link=".$link."'>".$slide->title."</a></li></ul><br/>";

Categories