如何使用php重定向页面

时间:2019-07-09 13:07:51

标签: php html


    define('DB_PASS' ,'');
    define('DB_NAME', 'codexworld');
    $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
    if(isset($_POST['submit'])){
    $username=$_POST['username'];
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $message=$_POST['message'];
    $q=mysqli_query($con,"insert into demo (username,email,subject,message)values('$username','$email','$subject','$message')");
    if ($q){
    echo 'Not Inserted';
    } else {
    echo 'Inserted Successfully';
    header('Location: ib.php');
    }
    }
    ?>
    <!DOCTYPE html>
    <html>
       <head>
          <title>Simple Contact form in PHP & MySQL</title>
          <!-- Latest compiled and minified CSS -->
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
       </head>
       <body>
          <div class="container">
             <div class="row">
                <form action="ib.php" class="col-md-6 col-md-offset-3" method="post">
                   <h2>Contact Us</h2>
                   <div class="form-group">
                      <label for="exampleInputEmail1">Name</label>
                      <input type="text" name="username" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Full Name" required="">
                   </div>
                   <div class="form-group">
                      <label for="exampleInputEmail1">Email address</label>
                      <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" required="">
                   </div>
                   <div class="form-group">
                      <label for="exampleInputPassword1">Subject</label>
                      <input type="text" name="subject" class="form-control" id="exampleInputPassword1" placeholder="Enter Your Subject" required="">
                   </div>
                   <textarea class="form-control"  name="message" rows="3" placeholder="Enter Your Query Here" required=""></textarea>
                   <button type="submit" name="submit"  class="btn btn-default">Submit</button>
                </form>
             </div>
          </div>
       </body>
    </html>

以上代码连接到数据库并将表单数据保存在数据库中,并且应该重定向 表单数据存储在数据库中,但不会重定向。 我该如何解决?保存表单数据并重定向

1 个答案:

答案 0 :(得分:3)

您需要在标题重定向后退出,以便html不会继续加载。

header("Location: ib.php");
exit;

最好不要在header()之前输出任何内容。不要在生产代码中回显“已成功插入”。