使用ajax将数据插入mysql表而不刷新页面

时间:2011-10-12 03:47:40

标签: php mysql ajax

我有一个使用recaptcha的基本联系表单,它有点工作。我有字段验证,验证码验证,它发布到我的数据库,但我的页面在INSERT之后刷新。这是我的JS:

function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();

    var html = $.ajax({
        type: "POST",
        url: "ajax.recaptcha.php",
        data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
        async: false
    }).responseText;

    if(html == "success") {
         processForm();
    } else {
        $('#wrongCaptcha').fadeIn().delay('5000').fadeOut();
        return false;
    }
}

function processForm(){
    var contactForm = $('#contactUsForm');
    if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#senderComments').val() ) {
        ('#incompleteMessage').fadeIn().delay('5000').fadeOut();
    } else {
        var answer = $.ajax( {
              url: "processContact.php?ajax=true",
              type: contactForm.attr( 'method' ),
              data: contactForm.serialize(),
              async: false,
              success: submitFinished
            } );
    }
}

function submitFinished( response ) {
    response = $.trim( response );
    if ( response == "success" ) {
        //$('#successMessage').fadeIn().delay('2000').fadeOut();
        $('#senderName, #senderEmail, #senderComments').val( "" );
        return false;
     } else {
        alert('errororoer');
        //$('#failureMessage').fadeIn().delay('5000').fadeOut();
     }      
}

这是我的PHP

define( "RECIPIENT_NAME", "Chris Tate" );
define( "RECIPIENT_EMAIL", "chris@nimmbl.com" );
define( "EMAIL_SUBJECT", "Visitor Message" );

// Read the form values
$success = false;

$senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";
$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
$senderComments = isset( $_POST['senderComments'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['senderComments'] ) : "";

// If all values exist, send the email
if ( $senderName && $senderEmail && $senderComments ) {

    $senderName = mysql_real_escape_string($senderName);
    $senderEmail = mysql_real_escape_string($senderEmail);
    $senderComments = mysql_real_escape_string($senderComments);

    $con = mysql_connect('localhost', 'root', 'root');

    if(!$con) { $success = "error"; }

    mysql_select_db('mf_db', $con);
    $sql="INSERT INTO emailData ( senderName, senderEmail, senderComments) VALUES ( '$senderName', '$senderEmail', '$senderComments' )";

    if(!mysql_query($sql,$con)) {
        $success = "error"; 
    } else {
        $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
        $headers = "From: " . $senderName . " <" . $senderEmail . ">";
        $success = mail( $recipient, EMAIL_SUBJECT, $senderComments, $headers );
    }

    mysql_close($con);
}


if ( isset($_GET["ajax"]) ) {
  echo $success ? "success" : "error";
} else { ... I cut the rest off b/c it's just some html.

1 个答案:

答案 0 :(得分:0)

尝试编辑jquery ajax的typeasync部分,如下所示:

var answer = $.ajax( {
    url: "processContact.php?ajax=true",
    type: "post",
    data: contactForm.serialize(),
    async: true,
    success: submitFinished
 } );

你在运行萤火虫吗?