簡體   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