这是我的 contact-form-handler.php 文件。单击提交按钮后,我想在PHP中发出警报。我成功接收邮件并重定向到我的主页但没有警报消息。如何在PHP中使用警告框?
我的表格工作正常,但这次我坚持这个简单的任务。
<?php
if(isset($_POST['submit'])){
//$companyMail = 'enesh@gmail.com';
$to = "eneshpal@gmail.com"; // this is your Email address
$customerMail = $_POST['formEmail']; // this is the sender's Email address
$first_name = $_POST['formFirstName'];
$last_name = $_POST['formLastName'];
$phone = $_POST['formPhone'];
$text = $_POST['formText'];
/* foreach($_POST['project_type'] as $project_type_value){} */
$projectType = implode(', ',$_POST['project_type']);
$scopeProject = implode(', ',$_POST['scope_project']);
$project_type_Str = 'Project Type : '.$projectType;
$scope_project_Str = 'Scope of Project : '.$scopeProject;
$subject = "Form Submission";
$message = "Hi, \n\n";
$message .= "First Name: ".$first_name . "\nLast Name: " . $last_name . " \nEmail: " . $customerMail . " \nPhone : " . $phone . "\nDescription: " . $text . "\n";
$message .= $project_type_Str."\n";
$message .= $scope_project_Str;
//$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
//$headers = "From:" . $companyMail;
//$headers2 = "From:" . $to;
if(mail($to,$subject,$message)){
//echo 'Mail Sent';
//$message = "Thanks, We Will Contact you Shortly";
//echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script>alert('Thanks, We Will Contact you Shortly');</script>";
header('Location:get_estimation.php');
}else{
echo 'Mail Not Sent';
}
//mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
/*if( "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.")
else die; */
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
答案 0 :(得分:0)
您的脚本存在许多问题,但要回答您的关键问题...您echo
正在使用alert()
的javascript代码,这将阻止您的php {{ 1}}执行,因为在header('Location:get_estimation.php');
被调用之前已经有输出发送到浏览器。 See here for more info
请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。使用include或require,函数或其他文件访问函数读取代码是一个非常常见的错误,并且在调用header()之前输出空格或空行。使用单个PHP / HTML文件时存在同样的问题。
根据你上面的评论......
是的,先生,我只是希望在提交表单后呈现成功消息?
为您举例说明如何实现您所需要的......
header
要解释一下,您不应该使用从php渲染的浏览器原生javascript <?php
if (!empty($_GET['success'])) {
echo 'Thanks, we will contact you shortly';
exit;
}
if (isset($_POST['submit'])) {
$to = "eneshpal@gmail.com";
$customerMail = $_POST['formEmail'];
$first_name = $_POST['formFirstName'];
$last_name = $_POST['formLastName'];
$phone = $_POST['formPhone'];
$text = $_POST['formText'];
$projectType = implode(', ', $_POST['project_type']);
$scopeProject = implode(', ', $_POST['scope_project']);
$project_type_Str = 'Project Type : ' . $projectType;
$scope_project_Str = 'Scope of Project : ' . $scopeProject;
$subject = "Form Submission";
$message = "Hi, \n\n";
$message .= "First Name: " . $first_name . "\nLast Name: " . $last_name . " \nEmail: " . $customerMail . " \nPhone : " . $phone . "\nDescription: " . $text . "\n";
$message .= $project_type_Str . "\n";
$message .= $scope_project_Str;
if (mail($to, $subject, $message)) {
header('Location: get_estimation.php?success=1');
exit;
} else {
echo 'Mail Not Sent';
exit;
}
}
来弹出并通知用户表单提交成功,特别是在重定向后立即。这只是一个糟糕的用户体验,它劫持浏览器窗口,直到他们点击确定,你无法真正设置本机警报窗口等。
相反,您应该在邮件成功发送后重定向成功alert
变量(返回原始页面,假设在我的示例中为GET
- 但您可以将其他地方重定向到另一个脚本) 。然后,您可以在重定向到的脚本中检测此变量的存在,并显示相应的消息等。
答案 1 :(得分:0)
在您的情况下,最好重定向用户
header('Location:get_estimation.php?success=true');
并传递GET参数。
当设置GET参数时,它将在你的get_estimation.php页面上显示你想要的内容
答案 2 :(得分:0)
echo "Thank You,will contact you soon";
echo "<script>setTimeout(\"location.href = 'get_estimation.php';\",3000); </script>";