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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)