【问题标题】:PHP form submit and redirectPHP 表单提交和重定向
【发布时间】:2016-05-12 02:39:35
【问题描述】:

所以我有一个模式表单,我想在其中获取输入的字段并将其发送到我想要的电子邮件地址,然后将其重定向到某个网页/下载页面 我已经设法用 PHP 做到了,但是我想使用客户端重定向它,只使用 PHP 邮件进行字段解析以获取电子邮件,我必须创建大量 PHP 表单才能这样做,因为重定向 url 在很多情况。

这里是 PHP

$to = "some@email.com";
$subject = "Contact Page";
$headers = "From: Contact Page";
$forward = 1;
$location = "somelocation";
$date = date ("l, F jS, Y");
$time = date ("h:i A");

$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
        foreach ($_POST as $key => $value) {
                $msg .= ucfirst ($key) ." : ". $value . "\n";
        }
}
else {
        foreach ($_GET as $key => $value) {
                $msg .= ucfirst ($key) ." : ". $value . "\n";
        }
}
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
    print("You will be redirected to your download link shortly";) 
    header ("Location:$location");
}
else {
    include("index.html");
} 

现在是 html

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">x</span>
    <form method="POST" onsubmit="myFunction()" action="form2.php"> 

<label for='name'>Name: </label>
<input type="text" name="name" >
<label for='email'>Email: </label>
<input type="text" name="email" >
<input type="submit" id="submit" value="Submit" name="submit">
<script>
function myFunction() { 
alert("form Submitted Successfully you will be redirected shortly");
        }
</script>

【问题讨论】:

  • 您想使用 Javascript 重定向页面吗?此外,您应该使用更好的标点符号来编写您的问题。这样更容易阅读。
  • 结束标签在哪里?截至目前,您在表单中拥有 JavaScript 代码。

标签: javascript php html ajax forms


【解决方案1】:

您可以使用 JavaScript 的 window.location.assign() 函数重定向到另一个页面。请尝试以下操作。

<script>
   function myFunction() { 
     alert("form Submitted Successfully you will be redirected shortly");
     window.location.assign("http://urlOfThePageYouWantToRedirectTo");
   }
</script>

【讨论】:

    【解决方案2】:

    您可以将这段代码放在您的 form2.php 文件中。

    请记住,您需要在 form2.php 文件中使用 HTML 代码才能使其工作。
    确保将其放在 head 标记下。

    <meta http-equiv="refresh" content="0; url=http://YourRedirectLink.com/" />
    

    如果您想延迟重定向(以秒为单位),请更改 content 属性的

    【讨论】:

    • 我能够使用 header ("Location:$location") 在 php 上重定向;但我想从客户端重定向它,因为我必须为每个模式表单创建单独的 php 表单
    【解决方案3】:

    为什么不通过 ajax 提交表单并在成功响应时重定向它。

    <script>
    function myFunction() {
    //ajax code
    //on success 
    
       alert("form Submitted Successfully you will be redirected shortly");
       //Redirect on url you want
       window.location.href= "write_your_url_here";
    
    }
    </script>
    

    【讨论】:

    • 不知道 ajax 你可以在这里指导我
    • SO 不是一个“学习编码”的网站 - 有大量关于使用 PHP 进行 ajax 的教程,只需搜索即可。
    猜你喜欢
    • 2021-03-01
    • 2016-09-22
    • 2013-09-10
    • 2021-02-15
    • 1970-01-01
    • 2017-05-16
    • 2016-10-05
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多