0

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.

I have 3 php files which are loginGUI.php , check_login.php and db_connect.php.

db_connect.php:

  <?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);
    }
 ?>

loginGUI.php:

<?php
   session_start();
   require_once ('db_connect.php'); // include the database connection 

?>

<html>
<head>
<title>
Login page
</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
color:#00FF00;>
Login Page
</h1>
<form name="myForm" method="POST" >    
Username<input type="text" name="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" value="Login" id='checklogin' onclick="check()"/>  
<div id='username_availability_result'></div>
</form>

<script language="JavaScript">

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");
      }
    });



   </script>






</body>
</html>

check_login.php:

<?php
    session_start();
    require_once ('db_connect.php');



    if(isset($_POST['pswrd']) && 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";

     }


    }

 ?> 

And my question is that I cannot get any result after ajax , it seems does not work. What is wrong?

P.S : I have edited my php files according to the recommendations. Thank you for all , but ajax does not work again. Help me!!

5
  • 3
    Where are you including the jQuery library? Do you have any other JS or CSS? Commented Nov 21, 2013 at 17:29
  • I did not include any jQuery library!! Where should I include? I am writing all php files by using TextWrangler on mac. Commented Nov 21, 2013 at 17:39
  • Place this in the <head> section of loginGUI.php: <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> Commented Nov 21, 2013 at 18:03
  • Okey I added but it does not affect the result. I have edited my php files according to the recommendations. Please help me on this edited version. Commented Nov 21, 2013 at 18:28
  • You need to give some details about the error(s) you're receiving. Commented Nov 21, 2013 at 19:23

5 Answers 5

1

You are not actually sending the userid and pswrd to your PHP script. You can't just send $.ajax random properties and hope it works. Your data needs to go in the data parameter.

$.ajax({
    type:"POST",
    url: "check_login.php",
    cache: false,
    data:{
        userid:userid,
        pswrd:pswrd
    },
    success: function(result){
        //if the result is 1  
        if(result == 1){  
            //show that the username is available  
            $('#username_availability_result').html(username + ' is Available');  
        }else{  
            //show that the username is NOT available  
            $('#username_availability_result').html(username + ' is not Available');  
        }  
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

No it does not work again..:(. I think my document is not ready because when I use $( document ).ready(function() {...} , it does not work. What should I do , I have very few hours..:((
No I do not see . My code does not work even if I have changed some of the lines according to recommendations..:(
1

You can use jquery ajax

<input type='text' id='username'/>
<input type='text' id='password'/>
<input type='button' id='submit'/>

<script type='text/javascript'>
  $(document).ready(function()
  {
     var pass = $("#password").val();
     var username = $("#username").val();
     $().click(function()
     {
         var request = $.ajax(
         {
             url:server.php,
             type:POST,
             data:{password:pass, username:username}
             success:function()
             {                 
               alert("Success");
             }
         });
   });       

});

</script>

This should work for you, u can collect the data from the server side using normal $_POST['password'] and $_POST['username']

On the server side you could do this

<?php
   if(isset($_POST['password']) && isset($_POST['username']))
   {
      $sql = "SELECT * FROM users WHERE username='".mysqli_real_escape($dbc, trim($_POST['username'])."' AND password= '".mysqli_real_escape($dbc, trim($_POST['password'])."'");

  $result = mysqli_query($dbc, $sql) or die("Could Not make request");
  if(mysqli_num_rows($result) == 1)
  {
    //redirect

  }
 else
 {
      echo "User Not Found";

 }


   }


?>

2 Comments

No it does not work! Already , $(document).ready(function() is not working. What should I do?
It should, remove all javascript from the page and try something like this.. $(document).ready(function(){alert("Hello World");}); remember this would only work within <script type='text/javascript'></script> and must be placed after the call to the directory of your jquery file... also make sure that your file has located the jquery file
0
$sql="SELECT * FROM customer WHERE name='" . $myusername . "' and cid='" . $mypassword . "'"; // fix this line
if ($count === 1){ // and fix this line
if (result === 1){ // and this line

It looks to me that you're using code from different places. You should be validating that the username / password combination exists, not checking to see if it is available.

Nobody is going to do your homework for you, sorry. Here's a good reason why..

<?php
    session_start();
    require_once ('db_connect.php');
    if(isset($_POST['pswrd']) && 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";
     }
    }
 ?>

You're mixing mysql and mysqli here. You should be using JUST mysqli! Your userid and pswrd variables get assigned to username and pass in the AJAX call but the check_login.php script never sees them. It's still looking for userid and pswrd. You've got too many errors and you don't seem to be very responsive about getting them fixed. Besides, we're not a homework site.

1 Comment

Yes , because I am new to php.
0

The problem could be due to missing type attribute in your script tag. It should be:

<script type="text/javascript" >

This should solve the problem.

Comments

0
$("#frm-signin").validate({
  errorClass: "error fail-alert",
  validClass: "valid success-alert",
    // Specify validation rules
    rules: {
        signin_email: { email :true, required: true,
        regex: /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i,
        remote: {
           url: "checkemail.php",
           type: "POST" }
       },
       signin_pass: {
        required: true,
        remote: {
            url: "checkpassword.php",
            type: "post",
            data: {signin_email: $("#signin_email").val()}              
       }
   }
   },
   messages: {
    signin_email: {
        required: "Email is mandatory",
        email: "Please enter valid email <h5 class='text-danger'>[email protected]</h5>",
        remote: "Please check your email",
        regex: "Please enter valid email <h5 class='text-danger'>[email protected]</h5>"
    },
    signin_pass: {
        required: "Password is mandaitory",
        remote: "Please check your password"
    }
},
tooltip_options: {
    signin_email: {
        required: {placement:'right',html:true},
        remote : {placement:'right',html:true},
        email :  {placement:'right',html:true},
        regex :  {placement:'right',html:true}
    },
    signin_pass: {
        required: {placement:'right',html:true},
        remote: {placement:'right',html:true}
    }
},
submitHandler: function(form) {
    form.submit();`enter code here`
}

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.