0

I'm having a problem on my code which the username and password does not match. Here is my output. When the username and password is not match, then it will give a message that it is not match , However, when the username and password match, then there will be a message that it is match here is my code below:

Image here

html code

<body>  
  <div class="container box">  
   <div class="form-group">  
    <h3 align="center">Live Username Available or not By using PHP Ajax Jquery</h3><br />  
    <label>Enter Username</label>  
    <input type="text" name="username" id="username" class="form-control" />
    <input type="password" name="password" id="password" class="form-control" />

    <span id="availability"></span>
    <br /><br />
    <button type="button" name="register" class="btn btn-info" id="register" disabled>Register</button>
    <br />
   </div>  
   <br />  
   <br />  
  </div>  
 </body>  
</html>  

script code

  <script>  
     $(document).ready(function(){  
       $('#username','#password').blur(function(){

         var username = $(this).val();
         var password = $(this).val();

         $.ajax({
          url:'check.php',
          method:"POST",
          data:{user_name:username, password:password},
          success:function(data)
          {
           if(data != '1')
           {
            $('#availability').html('<span class="text-danger">Username and Password not Match</span>');
            $('#register').attr("disabled", true);
           }
           else
           {
            $('#availability').html('<span class="text-success">Username and Password Available</span>');
            $('#register').attr("disabled", false);
           }
          }
         })

      });
     });  
    </script>

check.php - my database connection and query that fetch it from the database

 <?php  
    //check.php  
    $connect = mysqli_connect("localhost", "username", "", "dbname"); 
    if(isset($_POST["user_name"] && $_POST["password"]))
    {
     $username = mysqli_real_escape_string($connect, $_POST["user_name"]);
     $password = mysqli_real_escape_string($connect, $_POST["password"]);
     $query = "SELECT * FROM admin WHERE username = '".$username."' AND password = '".$password."' ";
     $result = mysqli_query($connect, $query);
     echo mysqli_num_rows($result);
    }
 ?>
  • And what is the output from `console.log(data)` in success callback function?? – Mohamed-Yousef May 15 '19 at 02:15
  • @Mohamed-Yousef when I remove the password the program works correctly just by username the program works fine, but when I added the password then it is not working cannot get data from `console.log` I am not sure about this `if(isset($_POST["user_name"] && $_POST["password"]))` and this `$('#username','#password').blur(function(){` –  May 15 '19 at 02:18
  • Try this https://stackoverflow.com/questions/56034740/redirect-to-another-page-in-php-through-ajax/56035009#56035009 – Ghanshyam Nakiya May 15 '19 at 02:19
  • @Mohamed-Yousef just to inform you sir, that code is a tutorial from this site [Check username availability](https://www.webslesson.info/2016/02/how-to-check-username-availability-in.html). I just modify and change it to make two inputs which is the password then it is not working correctly. –  May 15 '19 at 02:20
  • @Nick answer the `jquery selector` and `isset` part .. but to let you know .. **didn't work** is not a good thing to describe the problem specially with `ajax` . With AJAX. 1- check the connection between the js and the php file .. 2- `console.log` the returned data .. 3- pass variables from js to php .. then echo it out in php and it should return in a returned data .. 4- Add the db part then check the data again and again to output the desired output .. Last thing to be accurate trim the data before any if statement `var data = data.trim();` to avoid any unwanted white spaces – Mohamed-Yousef May 15 '19 at 02:34

1 Answers1

1

Notes

Answer

You are using isset incorrectly. To check that multiple values are set, separate them by commas, not with &&:

if(isset($_POST["user_name"], $_POST["password"]))

Your PHP code as it currently stands won't produce any output as it will terminate with a fatal error on that line.

In your jQuery, you're not specifying multiple selectors correctly. They should all be inside the same set of quotes:

$('#username, #password').blur(function(){

You also need to change this code, which will set both username and password values to the same thing:

var username = $(this).val();
var password = $(this).val();

to

var username = $('#username').val();
var password = $('#password').val();
Nick
  • 138,499
  • 22
  • 57
  • 95
  • can you check on the jquery code as well please ? thank you sir –  May 15 '19 at 02:22
  • im not sure about this line sir, `$('#username','#password').blur(function(){` –  May 15 '19 at 02:22
  • @webccodez your jQuery has a problem too. See my edit. – Nick May 15 '19 at 02:26
  • sir, great thing is the message is now showing `Username and Password not Match` although even I input the correct username and password. it is still not saying it is **Match** can we see it more thoroughly please sir thank you .. we're closed to it i guess?? –  May 15 '19 at 02:30
  • What is the output from `console.log(data)` in the callback function? – Nick May 15 '19 at 02:32
  • the output is `0` –  May 15 '19 at 02:38
  • That is saying that you didn't get a match. Are you sure you're entering values that are in the database? If not, it's probably due to your use of `mysqli_real_escape_string` on the username and password fields, so try removing those lines and see what happens. – Nick May 15 '19 at 02:39
  • how about this line sir `if(data != '1')` do you think that is the conflict? can you please see this line `if(data != '0') { $('#availability').html('Username and Password not Match'); $('#register').attr("disabled", true); } else { $('#availability').html('Username and Password Available'); $('#register').attr("disabled", false); }` –  May 15 '19 at 02:43
  • @webccodez no, the value of `data` is 0, indicating no match in the database, and that code is working fine. Please try what I said in my last comment. – Nick May 15 '19 at 02:44
  • already did :( then console.log does not return anything even 0 or 1. it is blank.. how about in the ajax or in the jquery sir? or in the query? –  May 15 '19 at 02:48
  • See my note about another issue with your jQuery code. – Nick May 15 '19 at 02:51
  • still didnt match sir. :( –  May 15 '19 at 02:54
  • ok, you need to provide more useful information than "still didnt match sir.". I can't help debug that. Try putting some debug statements in your PHP, then `console.log(data)` in the success function and see if what is in your PHP is what you expect. – Nick May 15 '19 at 02:57
  • to provide more help sir please see the image above on what my console and what my UI looks like please thank you sir, –  May 15 '19 at 03:00
  • That doesn't help at all. Please do what I asked and put debugging statements in your PHP and then `console.log(data)` to see what they say – Nick May 15 '19 at 03:05
  • Nick , yes :( but the datat always put into `0` sorry sir –  May 15 '19 at 03:07