Reputation: 3091
both my JS and PHP file work correctly, the problem i'm having is that it ALWAYS says error with submission even when I see the PHP script resulted in outputting "proceed". The database insert works correctly from the form and count stores the numbers of rows in my table. Is there something i'm overlooking?
var dataString = $("form").serialize();
var msg = '<?php echo $msg; ?>';
$.ajax({
type: "POST",
url: 'submit_form.php',
data: dataString,
datatype: 'html',
error: function() {
alert('Error');
},
success: function(msg) {
if (msg == 'proceed') {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>");
}
else {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Error with Submission!</h2>").append("<p>Please Try again.</p>");
}
}
});
return false;
if ($count > 15)
{
$msg = 'error';
echo $msg;
exit();
}
elseif ($stmt = $mysqli->prepare("INSERT INTO Comments (Name, Email, Comment) values (?, ?, ?)"))
{
//do sql logic here
$msg = 'proceed';
echo $msg;
}
else
{
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
Upvotes: 0
Views: 569
Reputation: 72
I had the same problem and the error is in php not ajax.
Define $MySQLi at the top of submit_form.php and try ;)
Upvotes: 0
Reputation: 1038
Two of the most common reasons I've seen that your ajax could always be running the error method.
dataType=json
if it's not json remove this line
async : true
consider changing this around
Upvotes: 0
Reputation: 5809
Try this : use trim to clean response
success: function(msg) {
if ($.trim(msg) == 'proceed') {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>");
}
Upvotes: 0
Reputation: 4686
var dataString = $("form").serialize();
var msg = '<?php echo $msg; ?>';
$.ajax({
type: "POST",
url: 'submit_form.php',
data: dataString,
dataType: 'html',
error: function() {
alert('Error');
},
success: function(msg) {
if (msg == 'proceed') {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>");
}
else {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Error with Submission!</h2>").append("<p>Please Try again.</p>");
}
}
});
return false;
Try this ?
Upvotes: 0
Reputation:
You might be outputting whitespace or other characters along with your "proceed" response, which would make the condition in the success handler false.
Upvotes: 2