2013-09-05 10 views
0

我需要得到這個區分成功和失敗的提交。jQuery的電子郵件形式不區分回調

發送-email.php

$name = trim($_REQUEST['name']); 
$email = trim($_REQUEST['email']); 
$content = $_REQUEST['comment']; 

if (eregi("\r",$email) || eregi("\n",$email)){ 
die("Why ?? :("); 
} 
$sitemail = "[email protected]"; 
$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 \n \n $message \n\n $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(); 
    }); 

形式

<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> 

我希望能夠得到的形式保持可見如果表單上失敗security_code。

因此,可能需要send-email.php的響應,然後使用'if'子句。 我已經嘗試了幾種方法來做到這一點,甚至無法區分回調結果! 我只是似乎圍繞着圈子。任何幫助讚賞。

+0

未結束的字符串,起始於'$ sitemail =在http「[email protected]'也許你可以發佈一個 「工作」 的例子/phpfiddle.org/,這表明你的問題? –

+0

除了這個輕微的錯字錯誤的PHP是好的 – martyn

+0

這是更多的是從php的jQuery和回調。 – martyn

回答

1

可以請你檢查這個代碼

jQuery代碼

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代碼

/*提交報價表*/

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; 
    } 
+0

這是一個完全不同的腳本只是想知道如果任何人使用一個我有,並能夠得到回調工作 – martyn

0

這裏是解決方案。 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(); 

     }); 

php文件:;:/

$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; 
    }