dtip91401 2015-08-18 16:51
浏览 99
已采纳

将用户名/密码传递给服务器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.

  • 写回答

1条回答 默认 最新

  • dongmubei7950 2015-08-18 17:07
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题