title: Checking username and password with php ajax
body: <p>I am working on a homework related to php. In the assignment, a log on page is wanted. In this log on page, each user is expected to enter his/her username and password. These username and password will be compared with the values in customer table in mysql database. If the password and username agrees with the records under 'cid' and 'name' columns in the customer table, a notice about successful login will be shown.</p>
  
  <p>I have 3 php files which are loginGUI.php , check_login.php and db_connect.php.</p>
  
  <p>db_connect.php:</p>
  
  <pre><code>  &lt;?php
       $db_name="ozcan_b"; // Database name 
       $tbl_name="customer"; // Table name 
       //connect to db
       $con=mysql_connect("127.0.0.1","xxxxxx","xxxxxx");
       mysql_select_db("ozcan_b")or die("cannot select DB");
      // Check connection
      if (mysqli_connect_errno($con))
      {
      echo "Failed to connect to MySQL: " . mysql_connect_error();
      exit(0);
      }
   ?&gt;
  </code></pre>
  
  <p>loginGUI.php:</p>
  
  <pre><code>&lt;?php
     session_start();
     require_once ('db_connect.php'); // include the database connection 
  
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;
  Login page
  &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
  color:#00FF00;&gt;
  Login Page
  &lt;/h1&gt;
  &lt;form name="myForm" method="POST" &gt;    
  Username&lt;input type="text" name="userid"/&gt;
  Password&lt;input type="password" name="pswrd"/&gt;
  &lt;input type="button" value="Login" id='checklogin' onclick="check()"/&gt;  
  &lt;div id='username_availability_result'&gt;&lt;/div&gt;
  &lt;/form&gt;
  
  &lt;script language="JavaScript"&gt;
  
  var xhr_request = false;
  var checking_html = 'Checking...';  
  
      //when button is clicked  
      function check(){ 
  
          //Check the fields
          if(document.forms['myForm'].userid.value=="" || document.forms['myForm'].pswrd.value=="")
          {
              alert('Please enter your password and your username!');
          }
  
          else
          {
  
              //else show the cheking_text and run the function to check  
               //$('#username_availability_result').html(checking_html);  
              check_availability();  
          }   
      }
  
      //function to check username availability  
   function check_availability(){  
      //get the username  
      var userid = document.forms['myForm'].userid.value;  
      var pswrd = document.forms['myForm'].pswrd.value;
  
  
      //use ajax to run the check 
  
      var request = $.ajax(
      {
        url:check.php,
        type:POST,
        data:{pswrd:pass, userid:username}
        success:function()
        {
  
          alert("Success");
        }
      });
  
  
  
     &lt;/script&gt;
  
  
  
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>check_login.php:</p>
  
  <pre><code>&lt;?php
      session_start();
      require_once ('db_connect.php');
  
  
  
      if(isset($_POST['pswrd']) &amp;&amp; isset($_POST['userid']))
      {
          $sql = "SELECT * FROM users WHERE username='".mysqli_real_escape($dbc,        trim($_POST['userid'])."' AND password= '".mysqli_real_escape($dbc,    trim($_POST['pswrd'])."'");
  
          $result = mysql_query($dbc, $sql) or die("Could Not make request");
          if(mysql_num_rows($result) == 1)
          {
              //redirect to the welcome page
  
           }
           else
           {
               echo "User Not Found";
  
       }
  
  
      }
  
   ?&gt; 
  </code></pre>
  
  <p>And my question is that I cannot get any result after ajax , it seems does not work. What is wrong?</p>
  
  <p>P.S : I have edited my php files according to the recommendations. Thank you for all , but ajax does not work again. Help me!!</p>