【问题标题】:Redirect after clicking submit [duplicate]单击提交后重定向[重复]
【发布时间】:2014-11-27 12:23:41
【问题描述】:

所以在我使用了新的 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 中。

【问题讨论】:

  • 使用表单数据后,重定向需要在您的php文件中
  • 可以给我一个例子吗?
  • 在您将表单发送到的 php 文件中,您需要添加重定向。你可以使用header('Location:index.php');来做到这一点。

标签: javascript php


【解决方案1】:

当您执行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";}

【讨论】:

  • 工作 100% 谢谢你
  • 在添加 JavaScript 后,它现在禁用整个表单并且不会将数据从表单拉入 MySQL?是什么阻碍了它,因为一旦我删除了代码的平静,它就会将数据拉入数据库。
  • 在不知道您的代码的情况下几乎无法回答。请更新您的问题以包含它
  • 我已按要求更新了代码。
【解决方案2】:

如果你想在 JavaScript 中进行重定向,它会是:

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

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

header('Location:index.php');

【讨论】:

    【解决方案3】:

    你可以简单地使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      相关资源
      最近更新 更多