javascript - ajax - Verifying if a row already exist in the database -


i have simple form submitting email address database. want check first, using javascript , ajax, if email address exists in table. code using returns true regardless if row exists in database. hints?

js code

<script>  $('form').submit(function(e)  {   alert('submit intercepted');   e.preventdefault(e);   var u = $('#email').val();   $.ajax({       url: './test2.php',       type: 'post',       data: u,       success: function (response) {       //get response php page (what echo or print)         $('#status').append('<p>the email ok use , row has been inserted </p><p>' + response);         console.log('submitted');       },       error: function(jqxhr, textstatus, errorthrown) {         console.log(textstatus, errorthrown);         alert('email exists');       }     }); }); </script> 

html code

<form id="form" action="./test2.php" method="post">      e-mail: <input type="email" name="email" id="email" required><br>     <span id="status">   </span>     <input type="submit" id="btnsub">   </form> 

php code

if(isset($email)) {    $sql_email_check = $conn->query("select email test email='$email' limit 1") ;    $email_check = $sql_email_check->num_rows;    if ($email_check < 1) {      //return "available";     return true;        //header('location: /test.php');     exit();    }    else    {     //return "unavailable";     return false;     //header('location: /test.php');     exit();     } } 

returning true , false in php outputs nothing in browser. try this:

header('content-type: application/json'); if (isset($email)) {   $sql_email_check = $conn->query("select email test email='$email' limit 1") ;   $email_check = $sql_email_check->num_rows;   if ($email_check < 1) {     echo json_encode(['status' => 1]);     die();   }  }  echo json_encode(['status' => 0]); 

then, in javascript code, this:

success: function (response) {   if (response.status === 1) {     $('#status').append('<p>the email ok use , row has been inserted </p><p>' + response);   } else {     alert('email exists');   } }, 

btw, should use prepared statements. have no idea if query works or not, root of problem in original question echoing true or false not work expect.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -