0

我需要得到这个来区分成功和失败的提交。

发送电子邮件.php

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

if (eregi("\r",$email) || eregi("\n",$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 \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("#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”子句一起使用。我已经尝试了几种方法来做到这一点,甚至无法区分回调结果!我似乎只是在兜圈子。任何帮助表示赞赏。

4

2 回答 2

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;
    }
于 2013-09-05T13:01:57.653 回答
0

这是解决方案。查询:

  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;
    }   
于 2013-09-05T14:31:32.173 回答