【发布时间】:2018-10-06 10:51:09
【问题描述】:
我一直在尝试通过从 PHP 获取变量来更改 Javascript 中的显示属性。我可以使用 Ajax 向 PHP 发送一个变量,但我不能这样做,反之亦然。基本上,我正在使用 PHP 制作一个登录页面,这个登录表单在我的索引页面中。所以每次我登录时,登录表单仍然存在。我不想要它。是否可以隐藏该登录表单并显示任何与用户相关的信息(例如用户的个人资料图片)(不知何故,它应该已经由某些网站完成,我知道)? 这是我到目前为止所做的;
.tablelogin{
background: #044559;
background: -webkit-linear-gradient(342deg, #08384B, #03617C);
background: -o-linear-gradient(342deg, #08384B, #03617C);
background: linear-gradient(72deg, #08384B, #03617C);
border-top-left-radius: 11%;
border-top-right-radius: 11%;
border-bottom-left-radius: 5%;
border-bottom-right-radius: 5%;
display: none;
}
.loginbuttons{
border-radius: 39%;
border-bottom-color: rgba(17,101,255);
color: rgba(0,0,64);
background-size: 255px;
box-shadow:20px 30px 50px 5px,rgba(9,54,64);
border-top-color: rgba(14,77,191);
background-image: radial-gradient(rgba(5,25,64),rgba(11,52,129), rgba(16,91,235));
width: 55px;
font-size: 11px;
}
.loginbuttons:hover{
border-top-color: rgba(17,101,255);
border-bottom-color: rgba(14,77,191);
}
.loginbuttons:active{
border-left-color: rgba(5,45,64);
border-right-color: rgba(5,83,127);
}
.textboxes{
height: 11px;
width: 55px;
background-color: rgba(20,127,120);
border-radius:11%;
font-size: 11px;
color: rgba(0,0,64);
}
.textboxes:focus{
border-radius: 5%;
width: 91px;
height: 16px;
box-shadow: rgba(5,11,91);
}
.textboxes:active{
border-radius: 5%;
width: 59px;
}
::placeholder{
color: rgba(5,5,91);
}
<form method="post" action="loginRegister.php">
<table class="tablelogin" id="loginTables" name="loginTable">
<tr style="display: inline">
<td>
<input type="text" placeholder="Username" class="textboxes" name="username">
</td>
<td>
<input type="text" placeholder="Password" class="textboxes" name="password">
</td>
</tr>
<tr style="display: block">
<td>
<input type="radio" name="logins" value="signIn" onClick="SignIn()" placeholder="Sign in"><label style="font-size: 11px; color: #1295D5">Login</label>
</td>
<td>
<input type="radio" name="logins" value="register" onClick="Register()"><label style="font-size: 11px; color: #1295D5">Register</label>
</td>
</tr>
<tr id="values" style="display: none;">
<td>
<input type="text" placeholder="Name" class="textboxes">
</td>
<td>
<input type="text" placeholder="E-mail" class="textboxes">
</td>
</tr>
<tr id="values2" style="display: none;">
<td>
<input type="text" placeholder="Country" class="textboxes">
</td>
<td>
<input type="text" placeholder="Birth-Date" class="textboxes">
</td>
</tr>
<tr>
<td style="display: inline-block;">
<input type="submit" value="Login" class="loginbuttons" id="submits" name="submits" onClick="login()">
<input type="button" value="0" style="display: none;" name="logins" id="logins">
</td>
<td style="display: inline-block; margin-left: 9.12%">
<input type="reset" value="Cancel" class="loginbuttons">
</td>
</tr>
</table>
</form>
<!-- php codes inside my index file -->
<?php
session_start();
include("connection.php");
$sql = "SELECT userState FROM users WHERE userID = '$_SESSION[userID]'";
$result = mysqli_query($db,$sql);
$userState=0;
$row = mysqli_fetch_assoc($result);
if($result)
{
$userState=$row["userState"]; //gets the login state. It is set to 0 when user logouts
}
if($userState==1) // if login state is 1 then it means user is logged in. So it must hide login form and display user related informations
{
echo "<script> window.alert('sfyhsjj');</script>"; //prints that random text
echo ' <script> document.getElementById("logoupper").style.display="none";</script>' // It doesnt hide that
echo "<script> window.alert('sfyhsjj');</script>"; // prints that random text
}
?>//End of the code
【问题讨论】:
-
难道你不能用 php 包装你的登录表单来检查 userState 是否不是 1 吗?
<?php if ($userState!=1) { ?> YOUR_HTML_LOGIN_HERE <?php } ?>它应该默认为 0,我们可以检查是否为 0,但是如果您的 userState 曾经返回另一个数字,例如 -1 或 2 表示错误,我们仍然希望显示表单。 -
如果您使用的是 jQuery,您可以在 ajax 返回成功(并且登录成功)时使用
$('#loginTables').css('display','none');隐藏表单。 -
您可能还有一个 div 来显示 ajax 调用返回的用户信息,并填充它,在 ajax 返回时显示它。
-
您好,首先感谢 cmets。但是,@bigbitecode 这些代码只是整个网站的一小部分。所以我不能这样做,因为它的位置会搞砸。
-
@Sloan Thrasher $(document).ready(function(){ $("#submits").on("click", function(){ var buttonState = $("#loginTables") .serialize(); $.ajax({ url:'loginRegister.php', type:'POST', data:buttonState, success:function(e){ $("div").html("").html( e); } }); }); });我完全不明白你的意思。这就是我将变量发送到 PHP 的方式。它将我重定向到 login.php 页面检查登录数据并重定向回 index.php。所以我认为我需要制作类似页面加载器的东西,以便在每次重新加载索引页面时只加载网站所需的部分。
标签: javascript php html mysql display