PHP中的JS消息返回到HTML页面
JS message in PHP return to HTML page
HTML代码
<div id="fourmstyle" class="fourm">
<form action="scripts/mail.php" method="post">
<label for="name">Your Name <required>*</required>
</label>
<input type="text" name="Name" id="Name" placeholder="Joe Bloggs">
<label for="email">Your Email <required>*</required>
</label>
<input type="text" name="Email" id="Email" placeholder="Joebloggs@example.com">
<label for="telephone">Telephone
</label>
<input type="text" name="Telephone" id="Telephone">
<label for="type">Type
</label>
<select name="Type">
<option value="Booking" selected>Booking</option>
<option value="B&B">B&B</option>
<option value="Question">Question</option>
<option value="General">General</option>
<option value="Website Feedback">Website Feedback</option>
</select></p>
<label for="messsage">Message <required>*</required>
</label>
<textarea name="Message" id="Message" rows="5" cols="25">
</textarea></p>
<label for="btn"> </label>
<button type="submit" class="button">Submit
</button>
<br> <requireddescription> *(indicates that the information is required)
</requireddescription>
</form>
PHP代码<?php
if(isset($_POST))
{
$name = (isset($_POST['Name'])) ? strip_tags($_POST['Name']) : NULL; //if name is set, strip html tags, and return it, otherwise set the string as NULL.
$email = (isset($_POST['Email'])) ? strip_tags($_POST['Email']) : NULL; //same as above.
$telephone = (isset($_POST['Telephone'])) ? preg_replace('~[^0-9'-]~','',$_POST['Telephone']) : NULL; //if telephone is set, remove any characters that are not a number, or a dash, othewise set as NULL.
$type = (isset($_POST['Type'])) ? strip_tags($_POST['Type']) : NULL; //strip tags.
$message = (isset($_POST['Message'])) ? strip_tags($_POST['Message']) : NULL; //strip tags.
if(empty($name) || empty($email) || empty($message))
{
//name, email, and message are required fields, if they are empty, tell the user to go back and fill them in.
echo '<script type="text/javascript"> alert ("Please go back and fill in all required lines"); </script>';
}
else
{
//if the fields are NOT empty, proceed with the mailing.
$formcontent=" From: $name 'n Type: $type 'n'n Message: $message 'n'n Telephone: $telephone";
$recipient = "joebloggs@example.com";
$subject = "Website Contact Form: $type";
$mailheader = "From: $email 'r'n";
if(mail($recipient, $subject, $formcontent, $mailheader))
{
//if mail is sent to the SMTP server successfully, echo 'thank you'.
echo '<script type="text/javascript"> alert ("Thankyou '.$name.' we have submitted your message and we will get back to you as soon as possible, if you need to speak to us in the mean time please call 01983 872244 "); </script>';
}
else
{
//otherwise, tell the user it did not go through.
echo '<script type="text/javascript"> alert ("I am sorry but there has been an error submitting your request please try again or call us on 01983 872244"); </script>';
}
}
}
?>
好的,所以我上面的代码工作得很好,我有JS弹出警报。然而,当我打开JS警报时,它会把我带回到mail.php脚本,而不是它起源于的HTML页面,我该如何纠正这一点?
这应该有助于解决您眼前的问题…但在将来,可以尝试AJAX调用。
if(mail($recipient, $subject, $formcontent, $mailheader))
{
//if mail is sent to the SMTP server successfully, echo 'thank you' and return to previous page.
echo '<script type="text/javascript"> alert ("Thankyou '.$name.' we have submitted your message and we will get back to you as soon as possible, if you need to speak to us in the mean time please call 01983 872244 "); window.history.back(); </script>';
}
else
{
//otherwise, tell the user it did not go through.
echo '<script type="text/javascript"> alert ("I am sorry but there has been an error submitting your request please try again or call us on 01983 872244"); window.history.back(); </script>';
}
编辑
忘记包含第一个实例
if(empty($name) || empty($email) || empty($message))
{
//name, email, and message are required fields, if they are empty, tell the user to go back and fill them in and send them back.
echo '<script type="text/javascript"> alert ("Please go back and fill in all required lines"); window.history.back()</script>';
}
提交到mail.php时,mail.php将成为用户所在的新页面。如果希望它们仍然在调用脚本的页面上,则需要通过AJAX提交脚本,或者告诉浏览器返回到调用脚本的页面。要使用第二种方法,请将以下内容添加到脚本echos中:
window.history.back()
这取决于你在HTML页面中做了什么:
-
如果你的PHP是通过ajax调用的,那么你需要将
return false
添加到启动ajax调用的javascript函数中 -
如果你有一个表单,并且表单的提交通过常规回发到动作的url是你如何处理该PHP页面的,那么你需要在PHP中添加一个重定向到该页面,同时在会话中存储该警告消息,这样它就不会丢失,或者在PHP之后抛出一些HTML。
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- 在chrome.tabs.onCreated之后加载HTML页面
- 如何在vs2002中调试html页面
- 使用angular重定向到html页面
- Html页面上的多个Base64图像和平滑加载
- 无法将数据从firebase获取到我的html页面
- 如何在chrome扩展中重定向到html页面
- 如何将值传递到上一个html页面
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何使用Node/Express显示有关特定错误的自定义html页面
- 如何在 html 页面中调用 jquery
- 加载存储在IndexedDB中的HTML页面
- 同一HTML页面中的两个不同版本的JQuery
- HTML页面如何提取通过表单传递的参数
- 在HTML页面上将URL解析为可读的json格式
- 使用jQuery插入HTML页面的第一个项目缺少结束标记
- 在HTML页面上显示node.js服务器中的数据
- 加载页面时更改html页面的位置
- Ajax调用返回当前html页面,而不是请求的文件
- css是从远程node.js添加的,但不适用于html页面