I need to get this to distinguish between a successful and a failed submission.
send-email.php
$name = trim($_REQUEST['name']);
$email = trim($_REQUEST['email']);
$content = $_REQUEST['comment'];
if (eregi("",$email) || eregi("
",$email)){
die("Why ?? :(");
}
$sitemail = "mail@mysite.com";
$ip='none';
$ip = getenv('REMOTE_ADDR');
if( $_SESSION['security_code'] != $_POST['security_code'] || empty($_SESSION['security_code'] ) ) {
echo'<div class="message">The security code you entered did not match the image.</div>';
return false;
}
else{
//$sent=mail( $sitemail, "Message Title","From: $name
$message
$ip", "From: $email" );
echo'<div class="message">Thank you for your message. We will get back to you as soon as possible.</div>';
}
Jquery:
jQuery("#contact-form").submit(function(e){
var $form = jQuery(this),
$msg = jQuery(this).prev('div.message'),
$action = $form.attr('action');
jQuery.post($action,$form.serialize(),function(data){
$form.fadeOut("fast", function(){
$msg.hide().html(data).show('fast');
});
});
e.preventDefault();
});
form
<div class="message"></div>
<form id="contact-form" method="post" action="send-email.php">
<p class="column one-half">
<input name="name" type="text" placeholder="Your Name" required >
</p>
<p class="column one-half last">
<input name="email" type="email" placeholder="Your Email" required >
</p>
<p class="clear"><textarea name="comment" placeholder="Your Message" cols="5" rows="3" required></textarea>
</p>
<p class="column one-third"><input id="security_code" type="text" placeholder="Enter Code in image" name="security_code" required></p>
<img src="captcha/verifyimg.php?width=192&height=48" alt="Image verification" align="left" style="margin:10px;" />
<p><input name="submit" type="submit" value="Send Message">
</p>
</form>
I want to be able to get the form to stay visible if the form fails on the security_code.
So perhaps a response from the send-email.php is needed and then used with an 'if' clause. I have tried several ways to do this and couldn't even get it to distinguish between the callback results! I just seem to have gone round in circles. Any help appreciated.
can you please check this code
Jquery Code
jQuery.ajax({
url: jQuery('form#FrmQuote').attr("action"),
type: "POST",
dataType: 'json',
data: jQuery('form#FrmQuote').serialize()+'&action=SubmitQuote',
success: function(data){
if(data['status']) {
// if it return success
} else {
// if it return error
for(var id in data['error']) {
jQuery('#' + id).html(data['error'][id]);
}
}
return false;
}
});
PHP Code
/* Submit Quote Form */
if(isset($_POST['action']) && $_POST['action'] == 'SubmitQuote')
{
/*Retrive Data From Session */
$price_cal_array = $__Session->GetValue("Sess_Calculator_Option");
$SubmitQuoteResult = array();
$Svalidation = false;
$err['FullNameError'] = isEmpty($_POST['fullname'],COMMON_FULLNAME_IS_REQUIRED)?isEmpty($_POST['fullname'],COMMON_FULLNAME_IS_REQUIRED):'';
$err['EmailError'] = isEmpty($_POST['clientemail'],ERROR_EMAIL_ID_IS_REQUIRED)?isEmpty($_POST['clientemail'],ERROR_EMAIL_ID_IS_REQUIRED):'';
$err['PhoneError'] = isEmpty($_POST['phonel'],COMMON_PHONE_IS_REQUIRED)?isEmpty($_POST['phone'],COMMON_PHONE_IS_REQUIRED):'';
$err['EnquiryError'] = isEmpty($_POST['enquiry'],COMMON_MESSAGE_IS_REQUIRED)?isEmpty($_POST['enquiry'],COMMON_MESSAGE_IS_REQUIRED):'';
//$err['FullNameError'] = COMMON_FULLNAME_IS_REQUIRED;
if (isset($_REQUEST['captcha_code'])) {
require_once SITE_DOCUMENT_ROOT . '/new_captcha/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_REQUEST['captcha_code']) == false) {
$err['CaptchaError'] = CAPTCHA_INVALID;
}
}
foreach($err as $key => $Value){
if($Value != '') {
$Svalidation=true;
}
}
if($Svalidation == false) {
$SubmitQuoteResult['status'] = true;
$SubmitQuoteResult['QuoteSubmitSuccessMsg'] = "success message";
// send mail
} else {
$SubmitQuoteResult['status'] = false;
$SubmitQuoteResult['error'] = $err;
}
echo json_encode($SubmitQuoteResult);
exit;
}
Here is the solution. Jquery:
jQuery("#contact-form").submit(function(e){
var $form = jQuery(this),
$msg = jQuery(this).prev('div.message'),
$action = $form.attr('action');
jQuery.post($action,$form.serialize(),function(data){
if(data=='sent'){
$form.fadeOut("fast", function(){
$msg.hide().html('Thank you for your message. We will get back to you as soon as possible.').show('fast');
});
}
else{
$msg.hide().html(data).show('fast');
return false;
}
});
e.preventDefault();
});
the php file:
$name = trim($_REQUEST['name']);
$email = trim($_REQUEST['email']);
$content = $_REQUEST['comment'];
$email"";
if( $_SESSION['security_code'] != $_POST['security_code'] || empty($_SESSION['security_code'] ) ) {
echo'<div class="message">The security code you entered did not match the image.</div>';
return false;
}
else{
echo"sent";
return false;
}