Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Absolute form confirmation (jQuery + PHP)

    I have a simple php mailer:

    PHP Code:
    <?php
      $name 
    $_REQUEST['name'];
      
    $email $_REQUEST['email'];
      
    $message $_REQUEST['message'];
      
    $to "whoever@me.com";
      
    $subject "Some subject";
      
    $header "From: $name <$email>";
      if(
    $name == "" || $email == ""){
        echo 
    "Some reminder to fill in all fields.";
      }else{
        
    mail($to$subject"$message"$header);
        echo 
    "Some success message.";
      }
    ?>
    My AJAX jQuery form looks like this:

    Code:
    $(document).ready(function () {
        var name=encodeURIComponent(document.getElementById("name").value);
        var email=encodeURIComponent(document.getElementById("email").value);
        var message=encodeURIComponent(document.getElementById("message").value);
        $("#form").submit(function (event) {
            event.preventDefault();
            $.ajax({
                type: "post",
                dataType: "html",
                url: 'mail.php',
                data: "name="+name+"&email="+email+"&message="+message,
                success:function(something){}
            });
        });
    });
    Basically, I want the PHP message to return to the page via the success function, but I'm not sure how to make that happen. this code [document.getElementById("results").innerHTML=something.responseText;] returns an "undefined" error. The reason I want this is because most jQuery forms respond with success if the the form has been submitted successfully (i.e., all fields have been correctly filled in), but this does not mean that the PHP has processed successfully. Any help would be appreciated.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    you doing things the hard way.

    - here's some info on error handling as well - http://www.maheshchari.com/jquery-ajax-error-handling/

    Code:
    $(document).ready(function () {
    var datastring= $("#form").serialize();
        $("#form").submit(function (event) {
            event.preventDefault();
            $.ajax({
                type: "post",
                dataType: "html",
                url: 'mail.php',
                data: datastring,
                success:function(html){
               $("#results").html(html);
    }
            });
        });
    });
    Last edited by DanInMa; 05-24-2011 at 09:56 PM.

  • #3
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for your reply. I tried using .serialize, but it broke my PHP somehow. And using success:function(html){ $("#results").html(html); } isn't calling the PHP output. Any suggestions? Thanks.

  • #4
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Quote Originally Posted by q1h View Post
    Thanks for your reply. I tried using .serialize, but it broke my PHP somehow. And using success:function(html){ $("#results").html(html); } isn't calling the PHP output. Any suggestions? Thanks.

    - Please define how it broke your php, because serialize does the same thing your doing with the longer version of your code -

    - the results only return on success, so perhaps it's failing. I've always found the php is kind of stingy on providing error messages, make sure you put in some type of echo on your php page that will fire some info on success or fail, maybe that will help.

    also Id put in a temp alert or console.log to see how the datastring is actually coming out

  • Users who have thanked DanInMa for this post:

    q1h (06-09-2011)

  • #5
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Sorry, it actually does work. My datastring had a capital "S", which firebug helped my find. Thanks for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •