当按下“提交”按钮时,它显示相同的页面,但为空白。我希望它加载感谢页面

问题描述 投票:0回答:1

由于某些原因,当我按下“提交”按钮时,它将再次加载相同的URL,并且页面为空白。当我单击提交按钮时,它应该会加载一个新的谢谢页面,其中包含一般信息。如果您查看我的位置标头网址,它会显示应该加载的感谢网址,但是这就像我的页面完全忽略了加载感谢页面,而仅加载相同的注册页面,但显示为完全空白。

<html>
 <body>
   <?php       
    $output_form = true; //declare a FLAG we can use to test whether or not to show form

    $first_name = NULL;
    $last_name = NULL;
    $email = NULL;

    if (isset($_POST['submit']) ) { //conditional processing based on whether or not the user has submitted.

    $dbc = mysqli_connect('localhost', 'db', 'psw', 'db_folder')
    or die('Error connecting to MySQL server.');

    $first_name = mysqli_real_escape_string($dbc, trim($_POST['firstname']));
    $last_name = mysqli_real_escape_string($dbc, trim($_POST['lastname']));
    $email = mysqli_real_escape_string($dbc, trim($_POST['email']));

    $output_form = false; // will only change to TRUE based on validation

 //Validate all form fields 
    if (empty($first_name)) {

    echo "WAIT - The First Name field is blank <br />";
    $output_form = true; // will print form.
    }

    if (empty($last_name)) {

    echo "WAIT - The Last Name field is blank <br />";
    $output_form = true; // will print form.
    }

    if (empty($email)) {

    echo "WAIT - The Email field is blank <br />";
    $output_form = true; // will print form.
    }

    if ((!empty($first_name)) && (!empty($last_name)) && (!empty($email))) {
 //End of form validation


 //This section establishes a connection to the mysqli database, and if it fails display error message

    $query = "INSERT INTO email_list (first_name, last_name, email) " .
    "VALUES ('$first_name', '$last_name', '$email')";

    $result = mysqli_query($dbc, $query)
    or die('Error querying database.');

    mysqli_close($dbc);

    $to = '[email protected]';
    $subject = 'New Signup';
    $msg = "$first_name $last_name\n" .
    "Email: $email\n";
    $mail =  mail($to, $subject, $msg, 'From:' . $email);

    if($mail){
    header("Location: http://www.example.com/thanks.php?fn=".$first_name);
    exit();
    }

   }//end of validated data and adding recored to database. Closes the code to send the form.

  } //end of isset condition. This closes the isset and tells us if the form was submitted.

  else { //if the form has never been submitted, then show it anyway
  $output_form = true;
}

  if ( $output_form ) { //we will only show the form if the user has error OR not submitted.

?>
<div id="box">
    <center><img src="../../images/signup/image.png" class="sign-up" alt="Sign Up">   </center>
    <br>
    <p>Sign Up</p><br>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
        <div> 
            <label for="firstname">First name:</label>
            <input type="text" id="firstname" name="firstname" size="37" maxlength="37" value="<?php echo $first_name; ?>" />
        </div>
        <div>
            <label for="lastname">Last name:</label>
            <input type="text" id="lastname" name="lastname" size="37" maxlength="37" value="<?php echo $last_name; ?>" />
        </div>
        <div>
            <label for="email">Email:</label>
            <input type="text" id="email" name="email" size="37" maxlength="37" value="<?php echo $email; ?>" />
        </div>
            <div id="submit">
            <input type="submit" name="submit" value="Submit" />
            </div></center>
    </form>
  </div>
 <?php  
 }
 ?>
 </body>
</html>
php button submit
1个回答
0
投票
需要在header("location...");之后。
尝试将其放在<div id="box">之前。
© www.soinside.com 2019 - 2024. All rights reserved.