PHP表单提交和重定向

时间:2016-05-12 02:39:36

标签: javascript php html ajax forms

所以我有一个模态表单,我想获取输入的字段并将其发送到我想要的电子邮件地址,然后将其重定向到某个网页/下载页面 我已经设法用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>

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

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

import multiprocessing as mp
import os
import urllib.request # Check out the `requests` 3rd-party library too, it's great

# Split string into (URL, author) tuples. You can read this from stdin or a file, too.
urls = 'http://domain.com/book1**Shakespeare http://domain.com/book2**King' # etc
args = map(lambda x: x.split('**'), urls.split(' '))

def download_file(url, author_name):
    if not os.path.isdir(author_name):
        os.mkdir(author_name)
    # Transfer URLs contents to local file
    with urllib.request.urlopen(url) as u, open(author_name + '/book.zip', 'wb') as f:
        f.write(u.read())

# Run the download function in a pool of worker processes (defaults to CPU count)
# A simple `os.system()` or `os.popen()` call would work too
with multiprocessing.Pool() as pool:
    pool.map(download_file, args)