jquery php电子邮件表单无法区分回调

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.

donglian7879
donglian7879 它更多地与来自php的jquery和回调有关。
大约 7 年之前 回复
doujianglin6704
doujianglin6704 除了这个轻微的错误错误,PHP是好的
大约 7 年之前 回复
dongpan1416
dongpan1416 未终止的字符串文字,从$sitemail=“mail@mysite.com开始;也许您可以在phpfiddle.org上发布一个”工作“示例来演示您的问题?
大约 7 年之前 回复

2个回答

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;
    }
douhei8633
douhei8633 这是一个完全不同的脚本,只是想知道是否有人使用我拥有的并且能够使回调起作用
大约 7 年之前 回复

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;
    }   
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐