[英]PHP code executes before submit
I made a login form, I had a lot of other problems with it and now I found out that the PHP code was being executed before the submit . 我制作了一个登录表单,但还有很多其他问题,现在我发现在提交 Submit 之前已执行了PHP代码。 HOW can the problem be solved? 该如何解决? Since PHP does not support onclick events(which anyway sucks!), I don't see any solution that I know! 由于PHP不支持onclick事件(反正很烂!),所以我看不到任何已知的解决方案!
Code: 码:
//Get the form with POST
$user = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['pass']);
$usernames=mysql_query("SELECT * FROM user_info WHERE Username='" . $_POST['user'] . "' AND Password='" . $_POST['pass'] . "'");
if(!$usernames){echo mysql_error();}
$count=mysql_num_rows($usernames);
if(!$count){echo mysql_error();}
//If $count is equal to one, register the user and redirect him to his page, or else echo him that his info is wrong
if($count === 1) {
$_SESSION['code'] = "titan";
$_SESSION['ande'] = $user;
$_SESSION['password'] = $password;
header("location: home.php");}
else {
echo "<p style='color:red;text-align:center;'>Wrong username or password!</p>";}
Wrap the code that is supposed to be executed after the form submission in an IF statement that checks to see if the form is submitted: 将应该在表单提交后执行的代码包装在IF语句中,该语句检查是否提交了表单:
if ('POST' === $_SERVER['REQUEST_METHOD'])
{
// your code goes here
}
Check if form is submitted 检查表格是否提交
if(isset($_POST['user'])) {
//Your php code goes here
}
Based on your comment I extended this answer 根据您的评论,我扩展了此答案
Just place all code in this condition which you have sent in your question 只需将所有代码置于您在问题中发送的这种情况下
if(isset($_POST['user']))
{
//Get the form with POST
$user = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['pass']);
$usernames=mysql_query("SELECT * FROM user_info WHERE Username='" . $_POST['user'] . "' AND Password='" . $_POST['pass'] . "'");
if(!$usernames){echo mysql_error();}
$count=mysql_num_rows($usernames);
if(!$count){echo mysql_error();}
//If $count is equal to one, register the user and redirect him to his page, or else echo him that his info is wrong
if($count === 1) {
$_SESSION['code'] = "titan";
$_SESSION['ande'] = $user;
$_SESSION['password'] = $password;
header("location: home.php");}
else {
echo "<p style='color:red;text-align:center;'>Wrong username or password!</p>";}
}
我没有告诉你的是,PHP代码与HTML表单和其余HTML文件在同一个文件中...我试图用AJAX重新制作代码,((==无效,但还是有帮助! ),为此,我将PHP代码移到了另一个文件( Login_engine.php
)中,就是这样!当然,仍然存在一个小问题:现在,红色文本显示在Login_engine.php
... Login_engine.php
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.