如何让我的页面在后台运行?

时间:2017-04-05 14:32:01

标签: php html



if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
	
    header("Location: final.html");
}
?>




我有一个表单,当完成后执行脚本,但我不希望该脚本显示在浏览器的url部分,因此我希望它在后台运行或即使脚本立即关闭没有完全完成。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

你需要Javascript Ajax。 在Background中运行PHP的最佳实践是使用Ajax。

一个好的做法是:

HTML - > JS / Ajax - > PHP HTML< - JS / Ajax< - PHP

答案 1 :(得分:0)

您可以使用ajax实现此目的。例如,一旦一个人填写表格,他们可以按一个按钮。然后为此按钮设置单击侦听器并执行ajax调用。 ajax方法将数据发送到php脚本并运行php脚本,然后返回你设置它的任何内容返回到ajax方法而不重新加载页面,你将能够以任何你喜欢的方式操作结果。例如,

您的表单提交按钮;

<button id="submit">Submit Form</button>

您的javascript侦听器和ajax方法;

//This is executed whenever the submit buttom is clicked.
$( "#submit" ).bind( "click", function() {

      var myVar="Test Variable";

   $.ajax({
         type: 'POST',
           url: "get_message.php",//your php script
           data: {myVar:myVar},//your variable
           success: function(result){
              alert(result);
           }
     })
})

我的PHP脚本,我称之为get_message.php

<?php
$var = $_POST['myVar'];//get sent variable from ajax using post method
echo "The sent string is " . $var;

?>

然后输出The sent string is Test Variable

如果这个答案有助于您或您遇到任何问题,请告诉我。请注意,我必须包含jquery才能使代码生效。

相关问题