views:

337

answers:

3

Hi, i have a jquery ajax form. i have validation at server side for repeated username and email ID. which works fine without jquery/ajax.

in my php code i have used die() to return if any error occurs. my main problem is at ajax

here is the code

$(document).ready(function(){

$("form#regist").submit(function() {
  var str = $("#regist").serialize();
  $.ajax({
  type: "POST",
  url: "submit1.php",
  data: $("#regist").serialize(),
  success: function(){

   $("#loading").append("<h2>you are here</h2>");

   }

 });
  return false;


 });
});

The success function works properly. if my data is valid then it is added in the db, if my data is repeated then it is not added in the db. Now what i want to know is how do i return the error from my php file and use it at success event. Thanks in advance..

edit : this is how my php script looks

$query = "SELECT username from userdetails WHERE username = '$username'";
$q = mysql_query($query) or die("error" . mysql_error());
$numrows = mysql_num_rows($q);
if($numrows > 0)
{
die("username already exixt");
//should i put something like this
//$error = "username already exists";
//return $error; --->> i am not sure about this..
}

thanks in advance

A: 
$(document).ready(function(){

$("form#regist").submit(function() {
  var str = $("#regist").serialize();
  $.ajax({
  type: "POST",
  url: "submit1.php",
  data: $("#regist").serialize(),
  success: function(msg)
  {
   alert(msg);
  }

 });
  return false;

 });
});

Here from server side send the message and show it, how i have shown it :)

Devi
A: 

In the function definition which you have done like: success: function(){ introduce a parameter like: success: function(retVal){

Now in the function you can check for the value of retVal. Say, you return from your PHP script, "successful" for success case and "this email exists" for failure. Now you can directly compare this here and do whatever you want to, like:

if(retVal == 'this email exists') { window.alert('please re-enter the email, this record exists!'); }

and so on... Hope this helps.

mkamthan
forgive my ignorance. how do i return from php script.My guess :If i have say$retval = check in db if value exist;if($retval){return $retval;}do you mean i should have such code in my php script. thanks for your reply and am really sorry for such a n00b question. thanks
noobcode
+2  A: 

Php side:

if($numrows > 0)
{
echo "username already exist";
}

Javascript side:

success: function(msg)
  {
   if(msg == 'username already exist') alert(msg);
  }

But this is so crude, If you plan to develop this further try to read some articles on JSON, so you can use json to communicate to server side. And also you should try to use some default error controlling, like return an array with php:

echo json_encode(array('error' => true, 'notice' => 'username exists'));

Then on the javascript side (jquery), use json ajax request and always check if error variable is true or not, if it is maybe you can use a default function for error controlling.

Hope this helped.

dfilkovi
thanks it helped..also i think i should look deeper into json..thanks
noobcode