Php 帮我拿一份简单的时事通讯订阅表?
我正在处理的时事通讯订阅表单可以在页脚中找到 当前,提交表单时,错误/成功消息将显示在新页面上 相反,我希望错误/成功消息直接显示在页脚表单下方 例如,如果用户输入电子邮件地址name@website.com,文本“Success”应显示在输入字段下方,而不是新页面上 非常感谢您的帮助 这是我现在得到的: 标题中的Php 帮我拿一份简单的时事通讯订阅表?,php,forms,email,webforms,newsletter,Php,Forms,Email,Webforms,Newsletter,我正在处理的时事通讯订阅表单可以在页脚中找到 当前,提交表单时,错误/成功消息将显示在新页面上 相反,我希望错误/成功消息直接显示在页脚表单下方 例如,如果用户输入电子邮件地址name@website.com,文本“Success”应显示在输入字段下方,而不是新页面上 非常感谢您的帮助 这是我现在得到的: 标题中的 <script> function submitEmail() { var email = $('#emailText').val(); jQuery
<script>
function submitEmail() {
var email = $('#emailText').val();
jQuery.post('/common/php/send_news.php', {
email: email
},function(data){
$('#submissionResponse').html(data);
},html);
}
</script>
函数submitEmail(){
var email=$('#emailText').val();
jQuery.post('/common/php/send_news.php'{
电邮:电邮
},函数(数据){
$('#submissionResponse').html(数据);
},html);
}
表格
<form method="post">
<fieldset>
<h3></h3>
<input type="text" name="email" maxlength="80" class="input" value="enter your email address..." onfocus="this.className=('input_active')" onblur="this.className=('input')" onclick="this.value='';" />
<input type="button" name="submit" value="" class="subscribe_btn" onmouseover="this.className=('subscribe_btn_over')" onmouseout="this.className=('subscribe_btn')" onclick="submitEmail();" />
<small>*we will not share your email address</small><span class="clear"></span>
</fieldset>
</form>
<div id="submissionResponse"></div>
*我们不会共享您的电子邮件地址
send_news.php
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "test@emailtest.com";
$email_subject = "Newsletter Subscription";
function died($error) {
// your error code can go here
echo "There was an error with your submission";
echo $error."<br /><br />";
die();
}
// validation expected data exists
if(
!isset($_POST['email'])) {
died('There was an error with your submission');
}
$email_from = $_POST['email']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thanks for signup up for the Newsletter!
<?php
}
?>
感谢您注册本通讯!
如果您希望这样做,您需要使用类似于AJAX POST提交的东西,而不是普通形式的POST提交。然后,您可以将响应文本放在任何您希望指示错误或成功条件的地方。我要做的是设置一个简单的页面来响应成功或失败,然后使用Ajax调用将信息从表单传递到该页面——您可以使用jQuery来实现这一目标
将其添加到javascript文件或脚本标记的标题中:
function submitEmail() {
var email = $('#emailText').val();
jQuery.post('/submission.php', {
email: email
},function(data){
$('#submissionResponse').html(data);
},html);
}
然后更改表单,使提交按钮如下所示:
<input type="button" name="submit" value="Submit" onclick="submitEmail();" />
表格下方有:
<div id="formStatus"><?php echo isset($formStatusResponse) ? $formStatusResponse : null; ?></div>
实现同一目标的两种方法。使用Ajax的速度更快,因为它只需要最终用户加载一行文本,而第二种方法则需要完全重新加载整个页面。您可以使用jQuery,但它并不是真正必要的,也不是比本机代码短很多。但这只是我的观点。jQuery用于实现跨浏览器兼容性。框架的使用从来都不是必需的,但它确实使编码更容易。jQuery允许您不必担心兼容性,也不必担心记住所有令人讨厌的小细节,它被捆绑到一个易于使用的函数中。我还添加了一个辅助方法来完成同样的事情。我仍然建议使用Ajax方法,但个人偏好和Javascript知识可能决定了第二种方法可能更容易实现。submission.php是您用来处理存储的文件名。您很可能没有包含到jQuery源文件的链接。您可以通过将
添加到您的网页标题来完成此操作。您是否介意详细说明设置AJAX POST提交所需的特定代码,以及如何准确设置和定位错误或成功条件?
if(isset($_POST['email'])){
include('submission.php');
}
<div id="formStatus"><?php echo isset($formStatusResponse) ? $formStatusResponse : null; ?></div>