我正在处理的时事通讯订阅表格可以在此链接的页脚中找到。
目前,当提交表单时,错误/成功消息会显示在新页面上。
相反,我希望错误/成功消息直接显示在页脚的表单下方。
IE。如果用户输入电子邮件地址 name@website.com,则文本“Success”应显示在输入字段下方而不是新页面上。
帮助将不胜感激!
这是我现在得到的:
在标题中
<script>
function submitEmail() {
var email = $('#emailText').val();
jQuery.post('/common/php/send_news.php', {
email: email
},function(data){
$('#submissionResponse').html(data);
},html);
}
</script>
形式
<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
}
?>