Javascript 表单提交后运行脚本和php

Javascript 表单提交后运行脚本和php,javascript,php,forms,Javascript,Php,Forms,我有这样一个html表单: <form method="get" action="save.php"> <input type="email" id="email" name="email"/> <input type="submit" name="submit" value="Submit" /> </form> <?php session_start(); $email = $_REQUEST['email']; $email_con

我有这样一个html表单:

<form method="get" action="save.php">
<input type="email" id="email" name="email"/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
session_start();

$email = $_REQUEST['email'];
$email_content = "Thank you for your subscription";
mail($email,"Thank you",$email_content);
header("Location:thankyou.php");
?>

在save.php中,我有如下内容:

<form method="get" action="save.php">
<input type="email" id="email" name="email"/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
session_start();

$email = $_REQUEST['email'];
$email_content = "Thank you for your subscription";
mail($email,"Thank you",$email_content);
header("Location:thankyou.php");
?>

现在,在save.php文件中,我需要发送此电子邮件,但也需要回显一个运行js函数的脚本。比如说

<?php
    echo "<script>";
    echo "<script src='my_path_to_file/file.js'></script>";
    echo "var subscriberEmail = '" . $email . "';";
    echo "mySubscribe(subscriberEmail);";
    echo "</script>";
 ?>

这是因为echo命令将内容发送到浏览器,而标题重定向将永远无法工作

你可以试着像这样使用Cometing:

<?php
echo "<script>";
echo "<script src='my_path_to_file/file.js'></script>";
echo "var subscriberEmail = '" . $email . "';";
echo "mySubscribe(subscriberEmail);";
echo "document.location.href='thankyou.php';";
echo "</script>";
?>

这是因为echo命令将内容发送到浏览器,而标题重定向将永远无法工作

你可以试着像这样使用Cometing:

<?php
echo "<script>";
echo "<script src='my_path_to_file/file.js'></script>";
echo "var subscriberEmail = '" . $email . "';";
echo "mySubscribe(subscriberEmail);";
echo "document.location.href='thankyou.php';";
echo "</script>";
?>

可能是mySubscribe(subscriberEmail)中的某个错误;功能。这就是为什么您将脚本放在电子邮件和标题之前(“Location:thankyou.php”);它不会发送邮件并重定向您。而且,如果您将脚本放在$email变量之前,还没有设置,

可能是mySubscribe(subscriberEmail)中的某个错误;功能。这就是为什么您将脚本放在电子邮件和标题之前(“Location:thankyou.php”);它不会发送邮件并重定向您。此外,如果将脚本放在$email变量之前,则尚未设置(

)(1)尝试回显的HTML
元素无效。(2) 您可以将输出回显到页面,也可以重定向到其他页面。不是两者都有。你必须选择你想做的事情。您是要回显此脚本还是要重定向到“谢谢”页面?(也许只是把这个脚本放在“谢谢”页面上?)为什么不使用ajax将表单内容(作为JSON)发送到php文件,然后在“完成”函数上执行任何脚本?(1)尝试回显的HTML
元素无效。(2) 您可以将输出回显到页面,也可以重定向到其他页面。不是两者都有。你必须选择你想做的事情。您是要回显此脚本还是要重定向到“谢谢”页面?(也许只是把这个脚本放在“谢谢”页面上?)为什么不使用ajax将表单内容(作为JSON)发送到php文件,然后在done函数上执行任何脚本呢?