I'm using jQuery $.ajax to post a simple contact form to a php script. I'm trying to evaluate the response and based on that, either replace the whole form with a "thank you" message or populate an "error" div with the problems. The success callback is being called, so (I think) there is no error, but the data is nevertheless empty.
jQuery code:
$(document).ready(function() {
$("input[type='submit']").click(function(event) {
event.preventDefault();
var name = '#' + $(this).closest('form').attr('name');
$.ajax({
url: "ajax/index1.php",
type: "POST",
data: $(name).serialize(),
success: function(data) {
alert(data);
if (data == 'yes') {
$('#error').html(data);
} else {
$(name).html(data);
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
});
});
I'm pretty sure the Success function (not the error one) is running because I get the alert even if I remove the error function. Also if I remove that alert line I don't get any alerts. And the form contents are being replaced with an empty div, so it looks like it's running smoothly.
Also, if I replace "alert(data)" with "alert('hello')" that does show. So Data is empty!
If I try to post the form directly (without jQuery), I get a response from the php page - either "Please enter your name. Please enter your e-mail. Please enter your company." Or "Thank you! Your message has been submitted."
Everything is on the same domain.
Why is Data empty? For the past 6 hours, I just can't get my head around this.
I haven't set any global ajax settings. Before posting here, I've also tried this with JSON just as suggested in one of the answers and the same result - empty response. I tried xml as well.
index1.php has a ton of code related to the other forms on the site. The gist of it:
if (!isset($_POST['submit'])) exit();
if (isset($_POST['submit'])) {
switch (basename($_SERVER['HTTP_REFERER'])) {
//define required fields, fields to e-mail in the form, success message, form div id...
}
}
if (is_array($required)) {
$errors = array();
foreach ($required as $req) {
$error = '';
if (empty($_POST[$req])) {
switch ($req) {
//adjust error message based on name of field...
default: $error = 'Please enter your ' . $req . '.';
}
$errors[] = $error;
}
}
}
if (empty($errors) && is_array($fields)) {
//decide to which e-mail address to send everything based on the form input
$headers = "From: ...";
$subject = "Inquiry";
$body = "The following information was submitted:\n\n";
foreach ($fields as $a => $b) {
if (is_array($_POST[$a])) {
$body .= sprintf("%s: %s\n", $b, stripsl开发者_运维百科ashes(implode(', ', $_POST[$a])));
} else {
$body .= sprintf("%s: %s\n", $b, stripslashes($_POST[$a]));
}
}
$send = mail($to, $subject, $body, $headers);
if (!$send) $msg = 'We encountered an error sending your message, please go back and try again. If the problem persists, please call us.';
}
$error = 'no';
if (is_array($msg)) {
$msg = implode(' ', $msg);
$error = 'yes';
}
header('Content-type: text/html');
echo $error . ' ' . $msg;
}
I'm obviously going to change the final text being echoed to something that can actually be meaningfully parsed, but at this point I'm just trying to get something to show in the success callback.
One thought - could $_SERVER['HTTP_REFERER'] be my issue? I'm switching from prototype and it wasn't a problem there, so I took it for granted here. If so is there a way to identify the page from which the form was submitted?
[UPDATE] Tried adding a "default" scenario under the $_SERVER['HTTP_REFERER'] switch statement to no avail, so I'm guessing this isn't the issue.
The problem with your AJAX is that you are overriding the default submit button action, so the submit button does not really submit the data, the Javascript that does it and looking the Javascript code, the $_POST['submit']
will never be set when the PHP page is called:
var name = '#' + $(this).closest('form').attr('name');
...
data: $(name).serialize(),
The only POST info send here is the variable name
, try to send submit
field with some value and you will see a different response.
You can also check if this is really happening changing the exit()
by die('empty submit')
.
try add the dataType: "text"
option and the complete callback below to check your response
complete: function(xhr, status) {
alert(xhr.responseText);
}
精彩评论