javascript - Enter id to query related information, but undefined is displayed
PHP中文网
PHP中文网 2017-06-30 09:55:16
0
3
836

The information can be queried on the PHP page, but undefined is displayed on the front-end page

<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
</head>
<body>
    <p>
        <input type="text" name="idNum" id="idNum">
        <input type="button" value="查询" id="btnSearch">
        <ul></ul>
    </p>
    <p id="myp"></p>

    <script type="text/javascript">
        $(function(){
            $("#btnSearch").bind("click",function(){
                //ajax
                $.post(
                    "search.php",
                    {num:$("#idNum").val()},
                    function (data) {
                        console.log(data);
                        $("ul").append(
                            "<li>mac:"+data.mac+"</li>"+"<li>name:"+data.name+"</li>"+"<li>password:"+data.password+"</li>"
                            );
                    }
                );
            })
        });

    </script>
</body>
</html>

PHP code is as follows



<?php
header("Content-type:text/json;charset=utf-8");
$num = filter_input(INPUT_GET, 'num', FILTER_SANITIZE_STRING);
//$num=$_GET["num"];
class p_mysqli extends mysqli {
    public function __construct($host, $user, $pass, $db) {
        parent::__construct($host, $user, $pass, $db);
        if (mysqli_connect_error()) {
            die('连接数据库发生错误咯: (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
        }
    }
}

$mysqli=new mysqli('127.0.0.1', 'root', 'wl123456','proxys');
if ($mysqli->connect_errno) {
    die('Connect Error:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql = "SELECT id,mac,name,password FROM s_user  WHERE id = ?";
//$sql = "SELECT mac,name,password FROM s_user  WHERE id = '207545'";
// 获取预处理
$stmt = $mysqli->prepare($sql);
// 绑定参数
$stmt->bind_param("s", $num);
// 执行
$stmt->execute();
// 获取结果集
$result = $stmt->get_result();
// 获取数据
$data = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($data);
?>

console.log(data) found that the array is empty

The HTML page displays the result like this: mac:undefined

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
某草草

It feels like the request did not return data. Use browser debugging tools to see the return value of the request

typecho

You have stated all the problems. The data returned by the background is empty. Judge the data. If it is empty, do not follow the following logic! You used the non-existent key value mac in js, it must be undefined

曾经蜡笔没有小新

Print the variable data before the last echo on the PHP page to see if there is a value. If there is no value, print the value of result on it to locate the problem step by step

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!