繁体   English   中英

PHP代码在提交之前执行

[英]PHP code executes before submit

我制作了一个登录表单,但还有很多其他问题,现在我发现在提交 Submit 之前已执行了PHP代码。 该如何解决? 由于PHP不支持onclick事件(反正很烂!),所以我看不到任何已知的解决方案!

码:

//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>";}

将应该在表单提交后执行的代码包装在IF语句中,该语句检查是否提交了表单:

if ('POST' === $_SERVER['REQUEST_METHOD'])
{
    // your code goes here
}

检查表格是否提交

if(isset($_POST['user'])) {

   //Your php code goes here

}

根据您的评论,我扩展了此答案

只需将所有代码置于您在问题中发送的这种情况下

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM