php - How to callback ajax request separate -
i'm learning way work ajax php send request with;
$.ajax({ url: 'check_exists.php', type: "post", data: registerform.serialize(), success: function(data) { console.log(data) registermsg = $('.registermsg'); if (data == 'nickname_exists') { nickname.addclass('error'); } else if (data == 'email_exists') { email.addclass('error'); } else { registermsg.html(''); } } });
now send php file , checks if data exists if input nickname nickname_exist
, if same email email_exists
back.
but if both data console.log nickname_existsemail_exists
way doesn't trigger if statement.
i send php file like;
require_once('db_connect.php'); if(isset($_post['nickname'])){ $nickname = $_post['nickname']; $st = $db->prepare("select * users nickname=?"); $st->bindparam(1, $nickname); $st->execute(); if($st->rowcount() > 0) { echo 'nickname_exists'; } } if(isset($_post['email'])){ $email = $_post['email']; $st = $db->prepare("select * users email=?"); $st->bindparam(1, $email); $st->execute(); if($st->rowcount() > 0) { echo 'email_exists'; } }
how fix this, , way how handle ajax php right way, can me hand.
i need make console.log
nickname_exists
email_exists
instead of
nickname_existsemail_exists
collect results array this:
require_once('db_connect.php'); //initalize array $result = array( 'nickname_exists' => 0, 'email_exists' => 0 ); if(isset($_post['nickname'])){ $nickname = $_post['nickname']; $st = $db->prepare("select * users nickname=?"); $st->bindparam(1, $nickname); $st->execute(); if($st->rowcount() > 0) { $result['nickname_exists'] = 1; } } if(isset($_post['email'])){ $email = $_post['email']; $st = $db->prepare("select * users email=?"); $st->bindparam(1, $email); $st->execute(); if($st->rowcount() > 0) { $result['email_exists'] = 1; } } //gives result in json. echo json_encode($result);
then return json. after can check of them in javascript:
//initialize haserror var haserror = false; if (data.nickname_exists == 1) { //set error nickname nickname.addclass('error'); haserror = true; } if (data.email_exists == 1) { //set error email email.addclass('error'); haserror = true; } //if had no error: if (!haserror) { registermsg.html(''); }
Comments
Post a Comment