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代码中运行Javascript / Jquery。它应该做的是当有人进入页面时,会话被检查(如果有会话,他们已经登录),如果有会话,它会通过更改类将登录按钮变为注销按钮。我已经读过你需要一个代表团,但我不确定要检查一个会话。这是会话检查/运行Jquery脚本:
<?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:
“btna”和“btnf”类是按钮。 “btna”是登录,“btnf”是登出。这是注销按钮的代码:
<!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
我还没有将我的按钮链接到注销表单。问题是即使设置了我的会话,登录按钮也不会将类更改为注销按钮。那么简单地说,我将在JQuery中使用哪个委托来检查会话,和/或我的JQuery代码是否正确(就分号,撇号等而言)?谢谢
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.
编辑:不幸的是,到目前为止,似乎到目前为止这些答案都没有成功 - 但非常感谢你们的帮助。但是,我认为问题出在我所要求的其他地方。它必须在注销按钮的HTML代码中,或者在CSS中的某个位置。我会继续寻找,但如果你们有建议,请发布。
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.
编辑2:如果它有任何区别,我的注销按钮在另一页上,但与CSS链接。这是我制作的一些新的PHP代码,任何人都可以看到它为什么不起作用(它根本没有显示)。这是在我的主页的正文部分。
<?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>';
}
?>
7 个解决方案
#1
5
In JQuery statement, do not use dot(.) for class.
在JQuery语句中,不要对类使用点(。)。
before : $('.btna').addClass('.btnh');
after : $('.btna').addClass('btnh');
Good luck.
祝你好运。
#2
0
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
您不需要在类名前添加一个点
#3
0
<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 :
这应该工作。如果没有,请使用它来确保DOM已准备就绪:
<script type='text/javascript'>
$(document).ready(function ()
{
$('.btna').addClass('btnh');
$('.btna').removeClass('btna');
}
</script>
#4
0
try
尝试
<script>
$(document).ready(function(){
var sessvalue='<?php echo $_SESSION['mysession']; ?>';
if(sessvalue == true) {
$('.btna').addClass('btnh');
$('.btna').removeClass('btna');
}
});
</script>
#5
0
Try this..:D
试试这个......: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>";
}
?>
#6
0
You don't need to use JavaScript for this at all. Just add a condition to set the class using PHP.
您根本不需要使用JavaScript。只需添加一个条件来使用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; ?>">
#7
-1
Maybe, DOM is not ready when you're running your script. Wait for ready event before run it:
也许,当您运行脚本时,DOM还没有准备好。在运行之前等待就绪事件:
$( function() {
//your code here
} )
#1
5
In JQuery statement, do not use dot(.) for class.
在JQuery语句中,不要对类使用点(。)。
before : $('.btna').addClass('.btnh');
after : $('.btna').addClass('btnh');
Good luck.
祝你好运。
#2
0
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
您不需要在类名前添加一个点
#3
0
<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 :
这应该工作。如果没有,请使用它来确保DOM已准备就绪:
<script type='text/javascript'>
$(document).ready(function ()
{
$('.btna').addClass('btnh');
$('.btna').removeClass('btna');
}
</script>
#4
0
try
尝试
<script>
$(document).ready(function(){
var sessvalue='<?php echo $_SESSION['mysession']; ?>';
if(sessvalue == true) {
$('.btna').addClass('btnh');
$('.btna').removeClass('btna');
}
});
</script>
#5
0
Try this..:D
试试这个......: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>";
}
?>
#6
0
You don't need to use JavaScript for this at all. Just add a condition to set the class using PHP.
您根本不需要使用JavaScript。只需添加一个条件来使用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; ?>">
#7
-1
Maybe, DOM is not ready when you're running your script. Wait for ready event before run it:
也许,当您运行脚本时,DOM还没有准备好。在运行之前等待就绪事件:
$( function() {
//your code here
} )