开发者

php validation using jquery and ajax

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(func开发者_开发百科tion () {

    $("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


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.


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.


$(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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜