Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-24-2011, 09:07 PM   PM User | #1
q1h
New Coder

 
Join Date: May 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
q1h is an unknown quantity at this point
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.
q1h is offline   Reply With Quote
Old 05-24-2011, 09:52 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
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);
}
        });
    });
});
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 05-24-2011 at 09:56 PM..
DanInMa is offline   Reply With Quote
Old 05-25-2011, 03:02 AM   PM User | #3
q1h
New Coder

 
Join Date: May 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
q1h is an unknown quantity at this point
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.
q1h is offline   Reply With Quote
Old 05-25-2011, 01:59 PM   PM User | #4
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
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
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
q1h (06-09-2011)
Old 06-09-2011, 09:38 PM   PM User | #5
q1h
New Coder

 
Join Date: May 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
q1h is an unknown quantity at this point
Sorry, it actually does work. My datastring had a capital "S", which firebug helped my find. Thanks for your help.
q1h is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:43 AM.


Advertisement
Log in to turn off these ads.