Grow your CSS skills. Land your dream job.

[resolved] jQueryy AJAX, getting response from php file

  • # April 25, 2010 at 8:39 pm

    Hello,

    I am trying to AJAX a typical contact form on this website… (http://marcosrobles.com/contact.php)

    When I fill out the form and click "Send" I get the success message added to the page, but there was no response from the PHP file, and the email is not sent :(

    I am new to AJAX, so I am not sure why this doesn’t work. I expect to see the "Thanks for contacting me." as a response (in firebug console.) upon success.

    AJAX call

    Code:
    $(“.general-submit”).click(function(){
    var general_fname = $(“#general_fname”).val();
    var general_lname = $(“#general_lname”).val();
    var general_email = $(“#general_email”).val();
    var general_phone = $(“#general_phone”).val();
    var general_phone2 = $(“#general_phone2″).val();
    var general_phone3 = $(“#general_phone3″).val();
    var general_message = $(“#general_message”).val();

    var data_msg = “general_fname=”+general_fname+”&general_lname=”+general_lname+”&general_email=”+general_email+”&general_phone=”+general_phone+”&general_phone2=”+general_phone2+”&general_phone3=”+general_phone3+”&general_message=”+general_message;

    $.ajax({
    type: “POST”,
    url: “/includes/validate-general.php”,
    data: data_msg,
    success: function(){
    $(‘#general-tab’).html(“

    “);
    $(‘.messages’).html(“

    Contact Form Submitted!

    “)
    .append(“

    We will be in touch soon.

    “);
    }
    });
    return false;

    });

    PHP validation script…

    Code:
    < ?php
    $errmsg = '';
    $general_fname = '';
    $general_lname = '';
    $general_email = '';
    $general_phone = '';
    $general_phone2 = '';
    $general_phone3 = '';
    $general_message = '';

    function isEmail($email) {
    return preg_match('|^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]{2,})+$|i', $email);
    };

    if(isset($_POST['general-submit']))
    {
    $general_fname = $_POST['general_fname'];
    $general_lname = $_POST['general_lname'];
    $general_email = $_POST['general_email'];
    $general_phone = $_POST['general_phone'];
    $general_phone2 = $_POST['general_phone2'];
    $general_phone3 = $_POST['general_phone3'];
    $general_message = $_POST['general_message'];

    if(trim($general_fname) == '')
    {
    $errmsg = '

    Please enter your first name‘;
    }
    if(trim($general_lname) == ”)
    {
    $errmsg .= ‘

    Please enter your last name

    ‘;
    }
    if(trim($general_email) == ”)
    {
    $errmsg .= ‘

    Please enter your email address

    ‘;
    }
    if(!isEmail($general_email))
    {
    $errmsg .= ‘

    Please enter a valid email address.

    ‘;
    }
    if(trim($general_message) == ”)
    {
    $errmsg .= ‘

    Please enter your message

    ‘;
    }

    if($errmsg == ”)
    {
    if(get_magic_quotes_gpc())
    {
    $general_message = stripslashes($general_message);
    }

    $to = “blue@ourownchange.com”;
    $subject = ‘[General Information] : Inquiry from ‘ . $general_fname . ‘ ‘ . $general_lname;
    $msg = “From :” . $general_fname . ‘ ‘ . $general_lname . “rn ” . “Phone Number: ” . “(” . $general_phone . “)-” . $general_phone2 . “-” . $general_phone3 . “rn” . “Message: ” . $general_message;
    mail($to,
    $subject,
    $msg,
    “From:” . $general_email. “rnReturn-Path:”. $general_email.”rn”);

    echo “Thanks for contacting me.”;
    } else{ echo $errmsg;}
    }


    EDIT: AHA moment! basically, the PHP was searching to see if the submit value was posted before it would run… I didn;t have the AJAX xubmit that value…. works now!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".