Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用jQuery和JSON加载数据?_Php_Jquery_Ajax_Json - Fatal编程技术网

Php 如何使用jQuery和JSON加载数据?

Php 如何使用jQuery和JSON加载数据?,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我想使用jQuery和JSON将数据从MySQL数据库加载到PHP页面。当用户从选择框中选择姓名时,它会将此人的数据加载到文本字段中 这是我的HTML代码选择 <select name='name' id='name'> <option value='1'>John</option> <option value='2'>Marco</option> <option value='3'>Charles&

我想使用jQuery和JSON将数据从MySQL数据库加载到PHP页面。当用户从选择框中选择姓名时,它会将此人的数据加载到文本字段中

这是我的HTML代码选择

<select name='name' id='name'>
    <option value='1'>John</option>
    <option value='2'>Marco</option>
    <option value='3'>Charles</option>
</select>

我应该使用什么代码来实现success函数?

我在这里猜测您的DB列名,但可能是这样的

$('#firstname').val(data.firstname);
$('#phonenum').val(data.phonenum);

首先,在getpersonData.php中回显json_编码之前,应该设置内容类型标题:

在您的成功功能中,您将执行以下操作:

$("#firstname").val(data.firstname); //firstname is the key of the user's firstname from your mysql query
getpersonData.php

<?php
include('dbconfig.php');
$id = $_POST['id'];
$query = "select * from person where ID='$id'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);

header("Content-type: application/json");
echo json_encode($data); ?>
主文件

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Load JSON data through jQuery and PHP </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
        load_data($('#name').val());
    $('#name').change(function(){
        load_data($('#name').val());
    });
});
function load_data(personid)
{
       $.getJSON("getpersonData.php", {id: personid}, function(data){
           $('#firstname').val(data['firstname']);
        $('#phonenum').val(data['phonenum']);
     });    
}
</script>
</head>
<body>
<select name="name" id="name">
<option value='1'>John</option>
    <option value='2'>Marco</option>
    <option value='3'>Charles</option>
</select>
   <input type='text' name='firstname' value='' id='firstname'/>
<input type='text' name='phonenum' value='' id='phonenum'/>
</body>
</html>

不要将任何用户数据直接插入到代码中,就像将$\u POST['id']插入select*from person,其中id='$id'一样。这将使您的代码对用户开放。从过时的mysql驱动程序切换到和。另外,您也可以在这里。结果行应作为关联数组而不是关联数组和数字数组的组合来获取。这两种方法都会产生更长的回复信息。谢谢你的建议。我现在会读到:@outis虽然SELECT*并不美妙,但我想你的意思是不要使用mysql\u fetch\u数组。它应该是mysql_fetch_array$result、mysql_ASSOC或只是mysql_fetch_ASSOC$resulti如果您需要PDO教程,请尝试。但是,不要因此而停止寻找其他人。谢谢。现在开始测试!:DDon别忘了按照Calvin指出的那样设置内容类型标题谢谢您发布答案!请务必仔细阅读本手册。还请注意,每次链接到自己的网站/产品时,都需要发布免责声明。
header("Content-type: application/json");
echo json_encode($data);
$("#firstname").val(data.firstname); //firstname is the key of the user's firstname from your mysql query
<?php
include('dbconfig.php');
$id = $_POST['id'];
$query = "select * from person where ID='$id'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);

header("Content-type: application/json");
echo json_encode($data); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Load JSON data through jQuery and PHP </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
        load_data($('#name').val());
    $('#name').change(function(){
        load_data($('#name').val());
    });
});
function load_data(personid)
{
       $.getJSON("getpersonData.php", {id: personid}, function(data){
           $('#firstname').val(data['firstname']);
        $('#phonenum').val(data['phonenum']);
     });    
}
</script>
</head>
<body>
<select name="name" id="name">
<option value='1'>John</option>
    <option value='2'>Marco</option>
    <option value='3'>Charles</option>
</select>
   <input type='text' name='firstname' value='' id='firstname'/>
<input type='text' name='phonenum' value='' id='phonenum'/>
</body>
</html>