单击提交后重定向

时间:2014-10-02 13:37:08

标签: javascript php

所以在我使用新的JavaScript之后。这是所有的代码。请记住,他们仍然是顶级身体缺失。没有把它放进去,因为它可能与我的问题无关。

            <?php
                // define variables and set to empty values
                $nameErr = $surnameErr = $emailErr = $contact_numberErr = "";
                $name = $surname = $email = $comment = $contact_number = "";

                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                   if (empty($_POST["name"])) {
                     $nameErr = "Name is required";
                   } else {
                     $name = test_input($_POST["name"]);
                     // check if name only contains letters and whitespace
                     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                       $nameErr = "Only letters and white space allowed"; 
                     }

                   }
                   if (empty($_POST["surname"])) {
                     $surnameErr = "Surname is required";
                   } else {
                     $surname = test_input($_POST["surname"]);
                     // check if name only contains letters and whitespace
                     if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
                       $surnameErr = "Only letters and white space allowed"; 
                     }
                   }

                   if (empty($_POST["contact number"])) {
                     $contact_numberErr = "Please provide contact details";
                   } else {
                     $contact_number = test_input($_POST["contact number"]);
                     // check if name only contains letters and whitespace
                     if (!preg_match("/^[a-zA-Z ]*$/",$contact_number)) {
                       $contact_number = "Only numbers allowed"; 
                     }
                   }

                   if (empty($_POST["email"])) {
                     $emailErr = "Email is required";
                   } else {
                     $email = test_input($_POST["email"]);
                     // check if e-mail address is well-formed
                     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                       $emailErr = "Invalid email format"; 
                     }
                   }

                   if (empty($_POST["comment"])) {
                     $comment = "";
                   } else {
                     $comment = test_input($_POST["comment"]);
                   }
                }

                function test_input($data) {
                   $data = trim($data);
                   $data = stripslashes($data);
                   $data = htmlspecialchars($data);
                   return $data;
                }
                ?>

                    <div class="container" align="center">
                        <div class="row featured-boxes login">
                            <div class="col-md-6">
                                <div class="featured-box featured-box-secundary default info-content">
                                    <div class="box-content">
                                        <h4 align="center">Customer Feedback</h4>
                                            <p><span class="error">* required field.</span></p>
                                        <form method="post" action="insert_customer_feedback.php" name="customer_feedback">
                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>Name</label> *<?php echo $nameErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtName" name="txtName">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>Surname</label> *<?php echo $surnameErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtSurname" name="txtSurname">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>E-mail</label> *<?php echo $emailErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtEMail" name="txtEmail">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>Contact Number</label> *<?php echo $contact_numberErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtContact_number" name="txtContact_number">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <label>Comment</label>
                                                        <input type="text" value="" class="form-control input-lg" id="txtComment" name="txtComment">
                                                        </div>
                                                    </div>
                                                <input type="button" value="Submit" class="btn btn-primary pull-right push-bottom" onClick="Confirm(this.form)">
                                            </div>
                                        </div>
                                    </form>

       <script type="text/javascript">
        function Confirm(form){
        alert("Thank you for your feedback.");
        form.submit();
        window.location.href = "/index.php";
        }
       </script>

出于某种原因,当添加了JavaScript的和平(window.location.href =“/ index.php”;)时,它不想将我的数据加载到MySQL中。

3 个答案:

答案 0 :(得分:0)

当您执行form.submit()时,您告诉表单执行表单操作属性中声明的操作。因此,它将自动重定向到那里设置的任何内容。

示例:

<form action="submit.php" method="POST">

您解除警报消息后,您的代码会将用户重定向到submit.php。重定向到submit.php时,您必须从那里处理重定向。

除非您使用AJAX发布表单或类似内容,否则当然是这样。

然后你可以添加:

window.location.href = "/index.html"; 

虽然在这种情况下,最好放在AJAX调用的成功函数中:

function Confirm(form){alert("Thank you for your feedback.");form.submit(); window.location.href = "/index.html";}

答案 1 :(得分:0)

您只需使用

即可
function Confirm(form){alert("Thank you for your feedback.");form.submit();
window.location = "location";
}

答案 2 :(得分:0)

如果你想在JavaScript中进行重定向,那就是:

window.location.replace("yourwebsite.com/index.php");

如果你想用PHP做,那就是:

header('Location:index.php');