javascript - Ajax failed registration do not echo result -


i have ajax post method if success alert "account created". problem when it's not created should alert account exists, problem still alert same.

script code:

$(document).ready(function(){      $("#btn-register").click(function(){         var regaccount = $("#regaccount").val();         var regpass = $("#regpass").val();          if((regaccount == "") || (regpass == "")){            alert("information required!");         }else {           $.ajax({             type: "post",             url: "register.php",             data: "regaccount="+regaccount+"&regpass="+regpass,             success: function(data){                      alert("account created!");             },             error:function(){                  alert("account exists");             }           });         }         $("#regaccount").val('');         $("#regpass").val('');         return false;       }); }); 

register.php

<?php      include 'function.php';     session_start();     ob_start();      $userid = rand(100000, 999999);         $regaccount = $_post['regaccount'];         $regpass = $_post['regpass'];          $regaccount = stripslashes($regaccount);         $regpass = stripcslashes($regpass);         $salt = "dctech2015abcrxd";         $regpass = md5($regpass) . $salt;         $regpass = sha1($regpass);          $con = new functions();         $con = $con->db;          $stmt = $con->query("select * users username = '$regaccount'");          $count = $stmt->rowcount();          if($count != 1){             $con = new functions();             $con = $con->db;             $status="offline";             $stmt = $con->prepare("insert users(user_id, username, password, status)values(:userid, :account, :password, :status)");             $stmt->bindvalue(':userid', $userid);             $stmt->bindvalue(':account', $regaccount);             $stmt->bindvalue(':password', $regpass);             $stmt->bindvalue(':status', $status);             $stmt->execute();         }else{             echo '<script>alert("account name exists");</script>';          }      ob_end_flush(); ?> 

you need following changes make work:

1) in php code, not write alert.

2) whether user exists in database or newly inserted, ajax request fetch data. have handle logic.

3) ajax error method called when ajax request failed (either request not sent or response status not 200 ok).

4) in case, error method never called if user exists or user inserted data being correctly transferred javascript php.

if($count != 1){   $con = new functions();   $con = $con->db;   $status="offline";   $stmt = $con->prepare("insert users(user_id, username, password, status)values(:userid, :account, :password, :status)");   $stmt->bindvalue(':userid', $userid);   $stmt->bindvalue(':account', $regaccount);   $stmt->bindvalue(':password', $regpass);   $stmt->bindvalue(':status', $status);   $stmt->execute();   echo 'success'; } else{   echo 'exists';  } 

and

$.ajax({   type: "post",   url: "register.php",   data: "regaccount="+regaccount+"&regpass="+regpass,   success: function(data){         if (data == 'success') {       alert("account created!");     }     else if (data == 'exists') {       alert("account exists");     }   },   error:function(){     alert("unknown problem occured.");   } }); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -