php - Having trouble with inserting query after program checks username availability using ajax -


i'm having trouble inserting query database after program checks username availability on blur using ajax check if username available register or not. after checks username availability, insert empty queries in database.

here sample output more details:

enter image description here

but problem when displays username available automatically inserts empty query database.

this insert database after checks availability of user:

enter image description here

i want insert query inserted when user submits or when he/she clicks register button. availability of username checking only. there problem code?

here code:

sample.php file

<!doctype html> <html> <head>     <title>sample</title>     <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">     <meta charset="utf-8">     <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <input type="text" placeholder="username" id="username" name="username" class="form-control" > <span id="availability" style="color:green;"></span> <input class="form-control" placeholder="first name" name="firstname" type="text" > <input type="submit" id="signup" name="signupsubmit" class="btn btn-info form-control" value="register"> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>  <script> //check username availability $(document).ready(function() {     $('#username').blur(function()     {         var username = $(this).val();         $.ajax({             url:"sampleprocess.php",             method:"post",             data:{username:username},             datatype:"text",             success:function(html)             {                 $('#availability').html(html);             }         })     }); }); </script> </html> 

i use ajax checking username availability inside script here:

<script>     //check username availability     $(document).ready(function()     {         $('#username').blur(function()         {             var username = $(this).val();             $.ajax({                 url:"sampleprocess.php",                 method:"post",                 data:{username:username},                 datatype:"text",                 success:function(html)                 {                     $('#availability').html(html);                 }             })         });     }); </script> 

the process file checking username , inserting query.

sampleprocess.php file

<?php    //this external file dbcontroller creating connection db     require_once("dbcontroller.php");     //dbhandler handles dbcontroller class     $dbhandler = new dbcontroller();      //call function mainconnect()     $connect = $dbhandler->mainconnect();      //check username availability     if(isset($_post["username"]))     {         $sql = "select * tablereminders username ='".$_post["username"]."'";         $result = mysqli_query($connect, $sql);          if(mysqli_num_rows($result) > 0 )         {             echo"<span class=\"text-danger\">username not available</span>";             echo'<script>document.getelementbyid("username").focus();</script>';         }         else         {             //in else condition query inserted in             //database after user clicks register button, insert right              //after program check availability              echo"<span class=\"text-success\">username available</span>";              //problem inserting query here             $username = isset($_post['username']);             $firstname = isset($_post['firstname']);              //query insert tablereminders             $query = mysqli_query($connect, "insert `tablereminders`(`username` , `firstname`)              values ('$username' , '$firstname')");              if($connect->query($query) == true)             {                 echo "<script type='text/javascript'>alert('new record created successfully');</script> ";                 echo "<script>settimeout(\"location.href = 'loginreminder.php'\", 0)</script>";             }         }      }  ?> 

in else condition after checking mysqli_num_rows put insert query there perform when user clicks register buton. there problem if-else condition or ajax function inside sample.php file, should use or post method? either way tried doesn't correct problem here. please help.

try ;)

there 2 things

  1. check username availability.
  2. submit form , create record.

1. check username availability.

for this, have done code need modify code this:

<?php  //this external file dbcontroller creating connection db require_once("dbcontroller.php"); //dbhandler handles dbcontroller class $dbhandler = new dbcontroller();  //call function mainconnect() $connect = $dbhandler->mainconnect();  //check username availability if(isset($_post["username"])){   $sql = "select * tablereminders username ='" . $_post["username"] . "'";   $result = mysqli_query($connect, $sql);    if(mysqli_num_rows($result) > 0){     echo '<span class="text-danger">username not available</span>';     echo '<script>document.getelementbyid("username").focus();</script>';   }else{     echo '<span class="text-success">username available</span>';   } } 

2. submit form , create record. create new ajax call other file insert record submits form data on button click event.

this can same done in case of username availability.

code sample insert record:

$username = isset($_post['username']); $firstname = isset($_post['firstname']);  //query insert tablereminders $query = mysqli_query($connect, "insert `tablereminders`(`username` , `firstname`)          values ('$username' , '$firstname')");  if($connect->query($query) == true){   echo '<script type="text/javascript">alert("new record created successfully");</script>';   echo '<script>settimeout(function(){ location.href = "loginreminder.php"}, 0);</script>'; } 

remember 2 things before inserting new record in database:

  1. validate user data.
  2. escape user data may function mysqli_real_escape_string or can use prepared statement , bind params.

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -