使用PHP重新加载提交表单两次的页面

时间:2016-09-02 06:37:05

标签: php html twitter-bootstrap

最近我开始使用PHP和Bootstrap创建一个简单的网站,其中包含三个PHP页面Header.php,Footer.php和Home.php。在标题页面中,我有一个名为注册的按钮。单击注册按钮将弹出一个模式(bootstrap模式)和注册表单。在我的home.php页面我包括我的页眉和页脚php文件。我的代码的问题是当我点击按钮,模态将打开,表单将由用户填写。提交用户将注册的表单和模态将被关闭。但问题是当我刷新主页时用户再次注册。所以我需要在提交模态表单后重新加载页面不应该再次提交用户。有人建议我这样做..

这是我在header.php中的模态

       <div class="modal fade" id="myModalNorm" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span> <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">

                <form class="form" role="form" method="post" action="Home.php">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Email address</label> <input
                            type="email" class="form-control" id="exampleInputEmail1"
                            placeholder="Enter email"  name="userEmail" required=""/>
                    </div>
                    <div class="form-group">
                        <label for="exampleInputUsername1">Username</label> <input
                            type="text" class="form-control" id="exampleInputUsername1"
                            placeholder="Enter username"  name="name" required=""/>
                    </div>
                    <div class="form-group">
                        <label for="exampleInputPassword1">Password</label> <input
                            type="password" class="form-control" id="exampleInputPassword1"
                            placeholder="Password"  name="password" required=""/>
                    </div>
                    <div class="form-group">
                        <label for="exampleInputPassword2">Confirm Password</label> <input
                            type="password" class="form-control" id="exampleInputPassword2"
                            placeholder="Re-enter Password" 
                            name="confirm_password" required=""/>
                    </div>

                    <label class="radio-inline"> <input type="radio" name="gender" value="Male">Male
                    </label> <label class="radio-inline"> <input type="radio"
                        name="gender" value="Female">Female
                    </label> 
                    <div class="checkbox ">
                        <label id="lgcheck"> <input type="checkbox" name= "terms"/> Check me out
                        </label>
                    </div>
                    <button type="submit" class="btn bsubmit btn-primary" name="submit">Submit</button>

                </form>


            </div>

            <!-- Modal Footer -->
            <!-- <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="save"">Save
                    changes</button>
                <button type="button" class="btn btn-default" id="close"
                    data-dismiss="modal">Close</button>
            </div> -->
        </div>
    </div>
</div>

这是我的Home.php文件

     <?php include_once 'Header.php';?>
      <?php include_once 'db/Conection.php';?>
       <div class="container">
<div class="row">
<?php
$register = array ();
/* Form Required Field Validation */
if (isset ( $_POST ['submit'] )) {
    if (isset ( $_POST ['userEmail'] )) {
        $userEmail = $_POST ['userEmail'];
    } 
    if (isset ( $_POST ['name'] )) {
        $name = $_POST ['name'];
    }
    if (isset ( $_POST ['password'] )) {
        $password = $_POST ['password'];
    } 
    if (isset ( $_POST ['confirm_password'] )) {
        $confirm_password = $_POST ['confirm_password'];
    } 
    $my_query = "select * from users where username like '$userEmail' ";
    $res = mysqli_query ( $conn, $my_query );

    if (mysqli_num_rows ( $res ) > 0) {
        echo "user exists";
    } else {
        $my_query = "INSERT INTO users (username,email,password,phonenumber) values ('$userEmail','$name','$password','$confirm_password') ";
        if ($conn->query ( $my_query ) === TRUE) {
            echo "registered successfully";
        } else {

            echo "failed" . "</br>" . $conn->error;
        }
    }

}else{
    echo "hiii";
}
?></div>

我想重定向到我的同一主页,而不重新提交表单。

0 个答案:

没有答案