I’m sending a form with ajax but my .php processor doesn’t seem to be receiving the data string from ajax because when the email arrives, none of the POST data appears.
Can anyone spot why, please?
ajax.js:
$.ajax({
type: "POST",
url: "bin/process.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
You didn’t post your complete process.php file but i’m wondering if at the end you do something like:
<?php ob_end_flush(); ?>
If not then read up in the manual on what ob_start() does.
If you are sending the buffer then post a bit more of your process code so we can see where $name is being echoed. And maybe copy/paste/post the actual response from firebug.
I removed all whitespace (just source formatting) from the beginning of the dataString and alert lines in the .js file and can now generate an alert WITH the data intact, so it’s going astray somewhere between the ajax file and the php processor.
I tried all the steps you suggested, all the way through to checking that I could get any kind of alert. I can’t! Not even alert(‘here’);
Immediately prior to the dataString line I have my input validation for all three fields. The only variation in how each field is handled is that the email address is also compared with a regexp. Here’s what they look like:
ajax.js
$('form#contact').submit(function() {
$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
Apart from some CSS setup at the top of the .js file, that’s essentially everything that’s in it.
Consider installing firebug and using the Firefox browser for development. Firebug lets you monitor traffic between the browser and the server so you can quickly and easily see what is being sent (or perhaps not sent). Firebug also allows stepping through your javascript and examine variables.
You need some sort of tool like this for doing almost any sort of ajax work.
Need more than that to figure it out. The assignment there looks fine. So now that you know dataString doesn’t exist test the other variables dataString is made up of.