Javascript PHP表单提交和重定向

Javascript PHP表单提交和重定向,javascript,php,html,ajax,forms,Javascript,Php,Html,Ajax,Forms,因此,我有一个模式表单,我想获得输入的字段并将其发送到我想要的电子邮件地址,然后将其重定向到某个网页/下载页面 我已经成功地用PHP实现了这一点,但是我想使用客户端重定向它,只需使用PHP邮件进行字段解析以获取电子邮件,我必须创建大量的PHP表单来实现这一点,因为重定向url在许多情况下是不同的 <!-- Modal content --> <div class="modal-content"> <span class="close">x<

因此,我有一个模式表单,我想获得输入的字段并将其发送到我想要的电子邮件地址,然后将其重定向到某个网页/下载页面 我已经成功地用PHP实现了这一点,但是我想使用客户端重定向它,只需使用PHP邮件进行字段解析以获取电子邮件,我必须创建大量的PHP表单来实现这一点,因为重定向url在许多情况下是不同的

  <!-- 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>
这里是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>

x
姓名:
电邮:
函数myFunction(){
警报(“表单提交成功,您将很快被重定向”);
}

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

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

函数myFunction(){
警报(“表单提交成功,您将很快被重定向”);
window.location.assign(“http://urlOfThePageYouWantToRedirectTo");
}

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

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

函数myFunction(){
警报(“表单提交成功,您将很快被重定向”);
window.location.assign(“http://urlOfThePageYouWantToRedirectTo");
}

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

请记住,您的form2.php文件中需要HTML代码才能运行此功能。
请确保将其放在head标记下

  <!-- 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>
<meta http-equiv="refresh" content="0; url=http://YourRedirectLink.com/" />


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

您可以将这段代码放入form2.php文件中。
  <!-- 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>


请记住,您的form2.php文件中需要HTML代码才能运行此功能。
请确保将其放在head标记下

  <!-- 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>
<meta http-equiv="refresh" content="0; url=http://YourRedirectLink.com/" />


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

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

  <!-- 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>
<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>

函数myFunction(){
//ajax代码
//论成功
警报(“表单提交成功,您将很快被重定向”);
//重定向到您想要的url
window.location.href=“在此处写入您的url”;
}

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

  <!-- 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>
<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>

函数myFunction(){
//ajax代码
//论成功
警报(“表单提交成功,您将很快被重定向”);
//重定向到您想要的url
window.location.href=“在此处写入您的url”;
}

是否要使用Javascript重定向页面?此外,你应该用更好的标点符号来写你的问题。这样读起来更容易。收尾标签在哪里?您现在的表单中有JavaScript代码。是否要使用JavaScript重定向页面?此外,你应该用更好的标点符号来写你的问题。这样读起来更容易。收尾标签在哪里?现在表单中有JavaScript代码。我可以使用标题(“Location:$Location”)在php上重定向;但我想从客户端重定向它,因为我必须为每个模态表单创建单独的php表单。我可以使用标题(“Location:$Location”)在php上重定向;但是我想从客户端重定向它,因为我必须为每个模式表单创建单独的php表单。我不知道ajax你能在这里指导我吗?所以这不是一个“学会编码”的网站-有无数关于用php做ajax的教程,只需搜索它。不知道ajax你能在这里指导我吗?所以不是一个“学会编码”网站网站-有无数关于用PHP做ajax的教程,只要搜索一下就可以了。