PHP:如何使用ajax用查询结果填充html表单字段
在执行简单的搜索之后,我试图用值填充表单,但是当我运行脚本时,当我发出警报(数据)时,我会得到“未定义”或空白响应 search.phpPHP:如何使用ajax用查询结果填充html表单字段,php,jquery,json,ajax,Php,Jquery,Json,Ajax,在执行简单的搜索之后,我试图用值填充表单,但是当我运行脚本时,当我发出警报(数据)时,我会得到“未定义”或空白响应 search.php <?php include_once 'config/config.php'; include_once 'config/connect.php'; $conn = dbconn(); if(isset($_POST['btn-search'])) { $sub_id = mysqli_real_escape_string($conn, $_P
<?php
include_once 'config/config.php';
include_once 'config/connect.php';
$conn = dbconn();
if(isset($_POST['btn-search']))
{
$sub_id = mysqli_real_escape_string($conn, $_POST['user_id']);
$sql = "SELECT * FROM users WHERE user_id = '".$sub_id."'";
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($res);
$found = array();
while ($row = mysqli_fetch_array($res))
{
$found[] = array(
'name' => $row['full_name'],
'init_date' => $row['init_date'],
'position' => $row['position'],
'email' => $row['email'],
);
echo json_encode($found);
}
}
mysqli_close($conn);
?>
剧本中可能有很多错误,我愿意接受任何可以得到的反馈和帮助。如果需要任何其他细节,请告诉我
提前感谢,只要快速查看PHP代码,它就会返回嵌套数组:
[
[
'name' => 'whatever',
'init_date' => 'date',
...
]
]
这是因为您首先定义了$found=array()
然后使用$found[]=array(…)
向其追加更多数组
但是,javascript部分尝试只访问不存在的data.name
。可能数据[0]。如果数据库中至少有一条记录,则名称应存在
是否要将数据
作为数组进行迭代
但问题可能在于PHP部分,在该部分中,您调用了两次mysqli\u fetch\u array()
,因此覆盖$row
变量并跳过找到的第一个结果:
$row = mysqli_fetch_array($res);
$found = array();
while ($row = mysqli_fetch_array($res))
{
...
我看到的最后一件事是,您可能想调用echo json\u encode($found)收集所有搜索结果后,在while
循环之后执行code>。顺便说一下,这也会产生无效的JSON。只要快速查看PHP代码,它就会返回嵌套数组:
[
[
'name' => 'whatever',
'init_date' => 'date',
...
]
]
这是因为您首先定义了$found=array()
然后使用$found[]=array(…)
向其追加更多数组
但是,javascript部分尝试只访问不存在的data.name
。可能数据[0]。如果数据库中至少有一条记录,则名称应存在
是否要将数据
作为数组进行迭代
但问题可能在于PHP部分,在该部分中,您调用了两次mysqli\u fetch\u array()
,因此覆盖$row
变量并跳过找到的第一个结果:
$row = mysqli_fetch_array($res);
$found = array();
while ($row = mysqli_fetch_array($res))
{
...
我看到的最后一件事是,您可能想调用echo json\u encode($found)收集所有搜索结果后,在while
循环之后执行code>。顺便说一下,这也会产生无效的JSON。要获得单个输出,您只需要一个$row=mysqli\u fetch\u数组($res);not loop..它将返回单参数输出
Search.php
<?php
include_once 'config/config.php';
include_once 'config/connect.php';
$conn = dbconn();
if(isset($_POST['btn-search']))
{
$sub_id = mysqli_real_escape_string($conn, $_POST['user_id']);
$sql = "SELECT * FROM users WHERE user_id = '".$sub_id."'";
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($res);
$found = array();
$found[] = array(
'name' => $row['full_name'],
'init_date' => $row['init_date'],
'position' => $row['position'],
'email' => $row['email'],
);
echo json_encode($found);
}
mysqli_close($conn);
?>
Search.php
为了获得单个输出,您只需要一个$row=mysqli\u fetch\u数组($res);not loop..它将返回单参数输出
Search.php
<?php
include_once 'config/config.php';
include_once 'config/connect.php';
$conn = dbconn();
if(isset($_POST['btn-search']))
{
$sub_id = mysqli_real_escape_string($conn, $_POST['user_id']);
$sql = "SELECT * FROM users WHERE user_id = '".$sub_id."'";
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($res);
$found = array();
$found[] = array(
'name' => $row['full_name'],
'init_date' => $row['init_date'],
'position' => $row['position'],
'email' => $row['email'],
);
echo json_encode($found);
}
mysqli_close($conn);
?>
Search.php
可以尝试将echo json_编码($found);从search.phpcan退出循环可以尝试将echo json_编码($found);从search.phpThank you@Kamlesh Gupta退出循环。你的建议取得了一些进展。这次我得到了一个结果,但是我得到了所有的空值。[{“name”:null,“init_date”:null,“position”:null,“email:null}]。如果我用我正在测试的示例用户id查询数据库,我会得到我想要的结果,所以我认为这与查询无关。有什么想法吗?好的,我在对data:data:{user\u id:$(''user\u id').val()}的ajax调用中修改data:data,并将php脚本中的POST从if(isset($\u POST['btn-search'])修改为if(isset($\u POST['user\u id'))。现在如何将值输入到输入字段中?好的。我让它工作了。我使用的是JSON.stringify(数据),我得到的是数组结果,但我删除了数据类型:“JSON”,并且did result=jQuery.parseJSON(数据);现在我可以使用result.user_id从数组中获取值。so$(“#user_id”).val(result.user_id);现在可以了。谢谢你@Kamlesh Gupta。你的建议取得了一些进展。这次我得到了一个结果,但是我得到了所有的空值。[{“name”:null,“init_date”:null,“position”:null,“email:null}]。如果我用我正在测试的示例用户id查询数据库,我会得到我想要的结果,所以我认为这与查询无关。有什么想法吗?好的,我在对data:data:{user\u id:$(''user\u id').val()}的ajax调用中修改data:data,并将php脚本中的POST从if(isset($\u POST['btn-search'])修改为if(isset($\u POST['user\u id'))。现在如何将值输入到输入字段中?好的。我让它工作了。我使用的是JSON.stringify(数据),我得到的是数组结果,但我删除了数据类型:“JSON”,并且did result=jQuery.parseJSON(数据);现在我可以使用result.user_id从数组中获取值。so$(“#user_id”).val(result.user_id);好了,谢谢你,马丁。我按照Kamlesh Gupta的建议移除了这个循环,它取得了一些进展。我不一定需要将数据作为数组进行迭代,但我认为这是一种很好的方法。我不知道如何传递php变量的值来填充预期字段。如果我使用一个正则变量,比如$user\u id=$row['user\u id'];如何将值加载到表单中?谢谢Martin。我按照Kamlesh Gupta的建议移除了这个循环,它取得了一些进展。我不一定需要将数据作为数组进行迭代,但我认为这是一种很好的方法。我不知道如何传递php变量的值来填充预期字段。如果我使用一个正则变量,比如$user\u id=$row['user\u id'];如何将值加载到表单中?