I am running Javascript/Jquery inside a php code. What it should do is when someone goes onto the page, the session is checked (If there is a session, they have logged in), and if there is a session, it turn the login button into a logout button by changing the class. I have read that you need a delegation, but I am not sure what to put to check for a session. Here is the session check/run Jquery script:
<?php
if (isset($_SESSION['mysession']) && $_SESSION['mysession'] == true) {
echo "<script type='text/javascript'> $('.btna').addClass('.btnh');
$('.btna').removeClass('.btna');</script>";
}
?>
<!DOCTYPE html>
//html code
The "btna" and "btnf" classes are the buttons. "btna" is the login, "btnf" is the logout. Here is the code for the logout button:
<!DOCTYPE html>
<head>
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<button type="submit" name="submit" class="btnh" placeholder="Logout">
Logout
</button>
</body>
I have not linked my button to the logout form yet. The problem is even when my session is set, the login button does not change class into the logout button. So simply, what delegation would I use in my JQuery to check for sessions, and/or is my JQuery code correct (in terms of semicolons, apostrophes, etc.)? Thanks
EDIT: Unfortunately it seems that so far none of these answers have succeeded so far - but thank you guys so much for the help. However, I think the problem lies elsewhere to what I am asking. It must be in the HTML code for the logout button, or somewhere in CSS. I will keep looking, however if you guys do have a suggestion, please post it.
EDIT 2: If it makes any difference, my logout button is on another page, but linked with CSS. Here is some new PHP code I made, can anyone see why it is not working (It does not show up at all). This is in the body section of my homepage.
<?php
if (isset($_SESSION['flinders']) && $_SESSION['flinders'] == true) {
echo '<button type="submit" name="submit" class="btna" placeholder="Login">';
echo 'Login';
echo '</button>';
}
else{
echo '<button type="submit" name="submit" class="btnh" placeholder="Logout">';
echo 'Logout';
echo '</button>';
}
?>
Change this :
echo "<script type='text/javascript'> $('.btna').addClass('.btnh');
$('.btna').removeClass('.btna');</script>";
To
echo "<script type='text/javascript'> $('.btna').addClass('btnh');
$('.btna').removeClass('btna');</script>";
You don't need to add a dot before the class name
Maybe, DOM is not ready when you're running your script. Wait for ready event before run it:
$( function() {
//your code here
} )
<script type='text/javascript'>
$('.btna').addClass('btnh');
$('.btna').removeClass('btna');
</script>
This should work. If not, use this to be sure that the DOM is ready :
<script type='text/javascript'>
$(document).ready(function ()
{
$('.btna').addClass('btnh');
$('.btna').removeClass('btna');
}
</script>
In JQuery statement, do not use dot(.) for class.
before : $('.btna').addClass('.btnh');
after : $('.btna').addClass('btnh');
Good luck.
try
<script>
$(document).ready(function(){
var sessvalue='<?php echo $_SESSION['mysession']; ?>';
if(sessvalue == true) {
$('.btna').addClass('btnh');
$('.btna').removeClass('btna');
}
});
</script>
Try this..:D
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<button type="submit" id="myChangeBtn" name="submit" class="btnh" placeholder="Logout">
Logout
</button>
</body>
</html>
<?php
if (isset($_SESSION['mysession']) && $_SESSION['mysession'] == true) {
echo "<script type='text/javascript'>
if($('#myChangeBtn').hasClass('btna')){ $('#myChangeBtn').removeClass('btna');}
if(!$('#myChangeBtn').hasClass('btnh')){ $('#myChangeBtn').addClass('btnh');}
</script>";
}
?>
</div>
You don't need to use JavaScript for this at all. Just add a condition to set the class using PHP.
<?php
$btnClass = (isset($_SESSION['mysession']) && $_SESSION['mysession'] == true)
? 'btnh' // value if above condition evaluates true
: 'btna'; // value if above condition evaluates false
?>
<button type="submit" name="submit" class="<?php echo $btnClass; ?>">