Php+ajax verification problem (with complete code). The error screenshot has been uploaded. Please point out where the error is.


this is the backend code

<?php
 $password=$_POST["password"];
function getRandPass(){
$chars = ("0123456789abcdefghijklmnopqrstuvwxyz");
$min = 6;//
$max = 9;//
$len = mt_rand($min,$max);//
$password= "";
$a_len = strlen($chars);
for($i=0;$i<$len;$iPP){
$password.= $chars[mt_rand(0,$a_len-1)];//
}
return $password;
}
$password=getRandPass();
 if($password==$password){
    $data["code"] = 1;
    $data["password"] = $password;
}else{
    $data["code"] = 0;    
}
$data="{password:"" . $password. ""}";//json
 echo json_encode($data);//json
?>

the following is the front-end code

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<input type="text" id="password">
 <button id="sub"></button>
  <button id="gopass"></button>
 <input type="text" id="text">
 <span id="texts"></span><!--  -->
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
     $("-sharpgopass").click(function(){
  var password=$("-sharppassword").val();
  if(password==""){
      $("-sharptexts").html("");
      return false;
  }
        $.ajax({
            url: "password.php",
            type: "POST",
            dataType: "json",
            data: {password: password},
            beforeSend: function(){
                $("-sharptexts").html("");//
            },
            success: function(data){
                if(data.code==1){ //
                    $("-sharptexts").html("" );
                }else{
                    $("-sharptexts").html("");
                }
            }
        });

 });
</script>
<script>
$(function(){
$("-sharpsub").click(function(){
  var password=$("-sharppassword").val();
  $.ajax({
  type: "post",
  url: "password.php",
  data: {password: password}, //password.php
  dataType: "json", //

  success: function(msg){
   $("-sharptext").empty(); //Text
   var data="";
   if(msg!=""){
   data = eval("("+msg+")"); //jsondata
   }
   $("-sharptext").val(data.password); //-sharptext
   $("-sharptexts").html("");
   console.log(data); //
  },
  error:function(msg){
   console.log(msg);//
  }
  });
 });
 });
</script>
</body>
</html>
Feb.28,2021

question 1, the password is not saved, and the new password is generated again when the password is verified, so it is always password error

.

question 2, $data=' {password: ". $password.'"}'; and echo json_encode ($data); repeat, json_encode is the

that generates json

question 3 when dataType: "json" is used by Ajax, jquery automatically parses the json string, eliminating the need for data = eval ("(" + msg+ ")");

question 4, in the click event to get the password, why submit the password data: {password: password}, / / the data submitted to password.php

when you haven't entered the password yet.
<?php
session_start();
function getRandPass()
{
    $chars    = ("0123456789abcdefghijklmnopqrstuvwxyz");
    $min      = 6;//
    $max      = 9;//
    $len      = mt_rand($min,$max);//
    $password = '';
    $a_len    = strlen($chars);
    for($i = 0;$i < $len;$iPP)
    {
        $password .= $chars[mt_rand(0,$a_len - 1)];//
    }
    return $password;
}
if(isset($_POST['password']))
{
    if($_SESSION['password'] == $_POST['password'])//
    {
        $data['code'] = 1;
    }
    else
    {
        $data['code'] = 0;
    }
}
else
{
    $password = getRandPass();
    $_SESSION['password'] = $password;//
    $data['password'] = $password;
}
echo json_encode($data);//json
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
    </head>
    <body>
        <input type="text" id="password">
        <button id="sub">
            
        </button>
        <button id="gopass">
            
        </button>
        <input type="text" id="text">
        <span id="texts">
        </span><!--  -->
        <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js">
        </script>
        <script>
            $('-sharpgopass').click(function()
                {
                    var password = $('-sharppassword').val();
                    if(password == '')
                    {
                        $('-sharptexts').html('');
                        return false;
                    }
                    $.ajax(
                        {
                            url: 'password.php',
                            type: 'POST',
                            dataType: 'json',
                            data:
                            {
                                password: password
                            },
                            beforeSend: function()
                            {
                                $('-sharptexts').html('');//
                            },
                            success: function(data)
                            {
                                if(data.code == 1)
                                {
                                    //
                                    $('-sharptexts').html('' );
                                }else
                                {
                                    $('-sharptexts').html('');
                                }
                            }
                        });
                });
            $('-sharpsub').click(function()
                {
                    var password = $('-sharppassword').val();
                    $.ajax(
                        {
                            type: "get",
                            url: "password.php",//password.php
                            dataType: "json", //
                            success: function(data)
                            {
                                $('-sharptext').empty(); //Text
                                $('-sharptext').val(data.password); //-sharptext
                                $('-sharptexts').html('');
                                console.log(data); //
                            },
                            error:function(msg)
                            {
                                console.log(msg);//
                            }
                        });
                });
        </script>
    </body>
</html>

ultimate goal: get the password and enter the password for authentication.

backend code changed to

<?php

if ($_POST['action'] == 'getPassword') {
    $data['password'] = getRandPass();
} else if ($_POST['action'] == 'check') {
    if($_POST['password'] == $_POST['inputPassword']){
        $data['code'] = 1;
    }else{
        $data['code'] = 0;    
    }
}

function getRandPass()
{
    $chars = ("0123456789abcdefghijklmnopqrstuvwxyz");
    $min = 6;//
    $max = 9;//
    $len = mt_rand($min,$max);//
    $password= '';
    $a_len = strlen($chars);
    
    for($i=0;$i<$len;$iPP){
        $password.= $chars[mt_rand(0,$a_len-1)];//
    }
    
    return $password;
}

echo json_encode($data);//json

?>

Front end code

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<input type="text" id="password">
 <button id="sub"></button>
  <button id="gopass"></button>
 <input type="text" id="text">
 <span id="texts"></span><!--  -->
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
     $('-sharpgopass').click(function(){
  var password=$('-sharppassword').val();
  if(password==''){
      $('-sharptexts').html('');
      return false;
  }
        $.ajax({
            url: 'password.php',
            type: 'POST',
            dataType: 'json',
            data: {action:'check',password: password,inputPassword:$('-sharptext').val()},
            beforeSend: function(){
                $('-sharptexts').html('');//
            },
            success: function(data){
                if(data.code==1){ //
                    $('-sharptexts').html('' );
                }else{
                    $('-sharptexts').html('');
                }
            }
        });

 });
</script>
<script>
$(function(){
$('-sharpsub').click(function(){
  var password=$('-sharppassword').val();
  $.ajax({
  type: "post",
  url: "password.php",
  data: {action:'getPassword', password: password}, //password.php
  dataType: "json", //

  success: function(msg){
   $('-sharptext').empty(); //Text
   var data='';
   if(msg!=''){
   data = eval("("+msg+")"); //jsondata
   }
   $('-sharptext').val(data.password); //-sharptext
   $('-sharptexts').html('');
   console.log(data); //
  },
  error:function(msg){
   console.log(msg);//
  }
  });
 });
 });
</script>
</body>
</html>

then try again, the main problem is that the final output is stitched manually.

Menu