Forum Moderators: open

Message Too Old, No Replies

help with ajax post

         

phpbilly

1:22 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



Hi,

I dont have much experience with ajax and was wondering if anyone can point me in the right direction.

I using ajax to post variables to a seperate php script that performs a mysql insert which works fine. Except when there is an mysql_error for example duplicate key. In this case the success message would still be displayed even if there has been no insert.

My ajax code is:
$(function() {
$(".submit").click(function() {
var countryID = $("#countryID").val();
var codetype = $("#codetype").val();
var code = $("#code").val();
var userid = $("#userid").val();
var dataString = 'countryID='+ countryID + '&codetype=' + codetype + '&code=' + code + '&userid=' + userid;
alert(dataString)
if(countryID=='' || codetype=='' || code=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "process2.php",
data: dataString,
success: function(message){
alert(message)
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});


I would think I need something similar to
if(message!='') { show error message}


But I can't seem to implement this. Thanks,

Bill

Tommybs

8:00 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



Hi,

Can't you just get process2.php to return the correct message and display it in the same div? I.e. so .success and .error are both actually in the same container, but you use the php script to decide what info is in there?


<div id="result">
<?php
$query = mysql_query("select 1");
if($query){
echo "<div class=\"success\">I worked</div>";
}else{
echo "<div class=\"error\">".mysql_error()."</div>";
}
?>
</div><!--End result -->