简体   繁体   中英

passing username/password to server url

How to use $.post request to pass username and password to the server url?

I want to return true or false from that request.

$.post("dtbs.php",{username:userName,password:password},function(result){
});

The above code represents that but I want to pass the true or false value to the text file from that request. I am confused how to do that?

The below is my script:

<script>
                  $(document).ready(function(){
                      $("#loginBtn").click(function(){
                        var userName = $("#inputlUsername3").val();
                        var password = $("#inputlPassword3").val();
                        if(userName && password) {
                          //your php request goes here and return true or false or any ohter response as per your need
                          $.post("dtbs.php",{username:userName,password:password},function(result){

});
                          $.ajax({url: "response.txt", success: function(result){
                              if(result === 'true') {
                                alert("Redirect it to dashboard");
                              } else {
                                alert("Show error");
                              }
                          }});
                        } else {
                          alert("Plz fill all the field");
                        }
                      });
                  });
            </script>

The below represents the dtbs.php

<?php
$uname=$_POST['txtuname'];
$pwd2=$_POST['txtpwd2'];
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("onlineshop",$con);
$r=mysql_query("select password from users where username='$uname'",$con);
$row = mysql_fetch_row($r);
    $val=$row[0];

if($val!="")
{
if($pwd2==$val)
{
    session_start();
    $_SESSION['username']=$uname;
    $myfile=  fopen("response.txt", "w") or die("unable to open file");
    $txt="true";
    fwrite($myfile, $txt);
    fclose($myfile);
}
 else {

      $myfile=  fopen("response.txt", "w") or die("unable to open file");
    $txt="false";
    fwrite($myfile, $txt);
    fclose($myfile);


}
}
 else {

$myfile=  fopen("response.txt", "w") or die("unable to open file");
    $txt="false";
    fwrite($myfile, $txt);
    fclose($myfile);
}
?>

suggest me solution.

dtbs.php should return the results if successful username/password, not write to a text file.

<script>
    $(document).ready(function(){
        $("#loginBtn").click(function(){
            var userName = $("#inputlUsername3").val();
            var password = $("#inputlPassword3").val();
            if(userName && password) {
                 $.post("dtbs.php", username:userName, password:password}, function(result){
                     if(result === 'true') {
                         alert("Redirect it to dashboard");                              
                     }
                     else {
                         alert("Show error");
                     }
                 });

            } else {
                alert("Plz fill all the field");
            }
        });
    });
</script>


<?php
$uname=$_POST['username'];
$pwd2=$_POST['password'];
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("onlineshop",$con);
$r=mysql_query("select password from users where username='$uname'",$con);
$row = mysql_fetch_row($r);
$val=$row[0];

if($val!="")
{
    if($pwd2==$val)
    {
        session_start();
        $_SESSION['username']=$uname;
        echo 'true';
    }
    else {
        echo 'false';
    }
}
else {
    echo 'false';
}
?>

Overall, you have the following problems with your code:

  • Don't use the msql_ functions, those are deprecated.
  • SQL injection issues.
  • Storing passwords in the DB as plain text.
  • Connecting to mySql as root without a password.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM