php - Conditional if statement after AJAX post using response, is this ok? -


a form i'm using single input uses ajax post server. plan take input's value string , check if string exists in database. i'll in_array() , if string doesn't exist insert database , echo 1 or else 0 if it's duplicate, sending 1 or 0 result.

in ajax i'm using simple function on success, if result returns 1 i'll use jquery display success message, or else i'll display error , exit. method validate server side , not have form submit returning 1 or 0 , exit(); duplicates?

    success: function(result)     {       if(result == 1)     { string inserted db }     else     {  duplicate exists        exit();       } 

thanks

i have made in php, return json encoded identity array of information response. include more information needed, debugging purposes , possible future changes.

if($results >= 1){     $duplicate_exists = 'true'; }elseif($results < 1){     $duplicate_exists = 'false'; };  $result = array(     'exists' => $duplicate_exists ,     'status' => $status,     'time' => time()     // etc );  echo  json_encode($result) 

then decode json object in javascript:

success: function(result){     result = jquery.parsejson(result) // can use eval(result) , it's slower.     if(result.exists == 'false'){         // string inserted db     }else{          // duplicate exists          exit();     } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -