【问题标题】:Redirect to URL after form submit in PHP or JS [closed]在 PHP 或 JS 中提交表单后重定向到 URL [关闭]
【发布时间】:2015-10-11 14:24:07
【问题描述】:

在 PHP 或 JS 中提交表单时重定向到“谢谢”页面是否更好。我不关心重定向之前页面上显示的任何文本。

我可能提供了太多代码,但我会在下面提供 PHI 和 JS 以供参考。

在我的密件抄送标头之后,以下标头会按预期工作吗?

header('Location: nextpage.html');

PHP

<?php   
if(empty($_POST['name2']) || empty($_POST['email2']) || empty($_POST['message2']))
{
    return false;
}

$name2 = $_POST['name2'];
$email2 = $_POST['email2'];
$message2 = $_POST['message2'];

$to = 'lindsay@domain.com'; // Email submissions are sent to this email

// Create email 
$email_subject = "Message from Domain 8.1";
$email_body = "You have received a new message. \n\n".
              "Name2: $name2 \nEmail2: $email2 \nMessage2: $message2 \n";
$headers = "From: lindsay@domain.com\r\n";
$headers .= "Reply-To: $email2\r\n";
$headers .= "Bcc: dan@domain.io\r\n";

mail($to,$email_subject,$email_body,$headers); // Post message
return true;            ?>

JS

$(function()
{
	var successMsg = "Your message has been sent."; // Message shown on success.
	var failMsg = "Sorry it seems that our mail server is not responding, Sorry for the inconvenience!"; // Message shown on fail.
	
	$("input,textarea").jqBootstrapValidation(
    {
     	preventSubmit: true,
     	submitSuccess: function($form, event)
	 	{
			event.preventDefault(); // prevent default submit behaviour
			
			var processorFile = "./bin/"+$form.attr('id')+".php";
			var formData = {};

			$form.find("input, textarea").each(function(e) // Loop over form objects build data object
			{		
				formData[$(this).attr('id')] = $(this).val();	
			});
	
			$.ajax({
		        url: processorFile,
		    	type: "POST",
		    	data: formData,
		    	cache: false,
		    	success: function() // Success
		 		{  
					$form.append("<div id='form-alert'><div class='alert alert-success'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><strong>"+successMsg+"</strong></div></div>");		
		 	   	},
			   	error: function() // Fail
			   	{
					$form.append("<div id='form-alert'><div class='alert alert-danger'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><strong>"+failMsg+"</strong></div></div>");	
			   	},
				complete: function() // Clear
				{
					$form.trigger("reset");
				},
		   	});
         },
         filter: function() // Handle hidden form elements
		 {
			 return $(this).is(":visible");
         },
	 });
});

【问题讨论】:

标签: javascript php


【解决方案1】:

是的,标头应该重定向到应该在当前目录中的nextpage.html。您应该在标题之后放置一个exit() 调用,以确保在将标题发送到浏览器进行重定向之前没有输出任何其他内容。所以最后你会得到

<?php
header('Location: nextpage.html');
exit();
?>

如果您不发出退出调用并让脚本继续执行代码,则某些代码可能会生成输出,从而阻止重定向发生。

以上代码适用于PHP,JS使用window.location.href进行重定向。这看起来像这样

<script>
window.location.href = 'nextpage.html'
</script>

【讨论】:

    【解决方案2】:

    成功时使用 window.location

    success: function() // Success
    {  
        $form.append("");
        window.location = 'newURL.html';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 2016-09-22
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      相关资源
      最近更新 更多