在提交表单之前,PHP代码正在执行

时间:2017-07-28 17:35:19

标签: php

<?php
function log_page(){
    print_r($_POST);
    //connection variables
$host = 'localhost';
$user = 'root';
$password = '';
$db = 'firstwebsite';

//create mysql connection
$mysqli = mysqli_connect($host,$user,$password);
if(!$mysqli){
    echo " connection";
}
else{
    echo "no connection";
}
$select = mysqli_select_db( $mysqli, $db);

    $_SESSION['message']='';

    if (isset($_POST['register'])) { 
    if ($_POST['password'] == $_POST['confirmpassword']) 
    {
        $$username = $mysqli->real_escape_string($_POST['username']);
        $email = $mysqli->real_escape_string($_POST['email']);
        $password = md5($_POST['password']);

        echo $username . " " . $email . " " . $password;
    }
    }
?>
<link rel="stylesheet" href="log_style.css" type="text/css">
<div class="body-content">
  <div class="module">
    <h1>Create an account</h1>
    <form class="form" action="log_page.php" method="post" autocomplete="off">
      <input type="text" placeholder="User Name" name="username" required />
      <input type="email" placeholder="Email" name="email" required />
      <input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
      <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
      <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
      <input type="submit" value="Already registered? Sign in" name="register2" class="btn btn-block btn-primary" />
    </form>
  </div>
</div>
<?php
}
?>

当我要求回显用户名,电子邮件和密码时,我的代码没有打印任何内容。它没有输入$ _POST [&#39;]&#39;] if语句。我认为php代码是在表单值执行之前执行的

1 个答案:

答案 0 :(得分:0)

不要在表单中使用相同的名称。如果您没有在表单中上传文件,也不需要enctype="multipart/form-data"

<form class="form" action="log_page.php" method="post"  autocomplete="off">
              <input type="text" placeholder="User Name" name="username" required />
              <input type="email" placeholder="Email" name="email" required />
              <input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
    <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
          <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
          <input type="submit" value="Already registered? Sign in" name="register2" class="btn btn-block btn-primary" />
</form>

请注意,这应该在你的log_page.php;

if ($_POST['register']) { 
    if ($_POST['password'] == $_POST['confirmpassword']) 
    {
        $username = $mysqli->real_escape_string($_POST['username']);
        $email = $mysqli->real_escape_string($_POST['email']);
        $password = md5($_POST['password']);

echo $username . " " . $email . " " . $password; //test purpose
    }
}