I'll get right into it. I need to hide some content on a page using PHP and Javascript.
I'm using Joomla. Here is the table:
<table class="orders">
<tr>
<th>Name</th>
<div class="testclass"><th>Expiry Date</th>
<th>Batch ID</th></div>
<th>Local ICOS</th>
</tr>
<tr>
<td>...</td>
<div class="testclass"><td>...</td>
<td>...</td></div>
<td>...</td>
</tr>
</table>
Now, let's say I want to hide "Expiry Date" and "Batch ID" for the user whose user ID is 50. This is the code I have written for that:
<?php
$user = Jfactory::getUser();
$userID = $user->get(id);
if ($userID == "50") {
echo "<script type='text/javascript>'
$(document).ready(function(){
$('.testclass').show();
})
</script>";
}
else {
echo "<script type='text/'javascript>'
$(document).ready(function(){
$('.testclass').hide();
})
</script>";
}
?>
Now, my page does load so there are no errors. However, it still does not physically hide the div's where the class is "testclass".
It just displays them normally. I know a better way is to use groups and I am going to assign the selected users on our site the required groups.
SO I think to call the groups might be easier instead of doing it for all the user ID's.
This is the only thing left for the completion of this custom "default.php" of a component I'm using. Any help is greatly appreciated.
FOA - Your code has it mixed - when userID is 50, the code is showing (.show()
) the class elements, not hiding them.
Second - Don't hide things using Javascript as anyone with access to dev tools (so anyone) can change the css and see the code.
Better way is not to render the code at all. It would go sth like this:
<table class="orders">
<tr>
<th>Name</th>
<?php if ($user->id!="50") { ?>
<th>Expiry Date</th>
<th>Batch ID</th>
</div>
<?php } ?>
<th>Local ICOS</th>
</tr>
</table>
You can hide stuff using JS when it's not important and is just for convenience for the user not to see them. Don't do that with important stuff.
Plus, your JS code is not correct in both parts. It should start like:
echo "<script type='text/javascript'> ... "
Notice the quatation marks.