我正在 php 和 jquery 项目中工作,我使用 ajax 向服务器端发出请求现在我的 php 文件的 ajax 响应有问题我有以下 php 代码<?phprequire 'db.php';$code = $_POST['code'];$status = 0;$count = 0;$name = "";$guarantors = 0; $sql = "SELECT * FROM clients WHERE code='$code'";$result = mysqli_query($link, $sql);if(mysqli_num_rows($result) > 0){ $row = mysqli_fetch_array($result); $name = $row['name']; $status = 1; $sqli = "SELECT * FROM guarantors WHERE client_code='$code'"; $resulti = mysqli_query($link, $sqli); if(mysqli_num_rows($resulti) > 0){ $guarantors = '['; while($rowi = mysqli_fetch_array($resulti)){ $count ++; $guarantors .= '{id:'.$rowi['id'].', name:'.$rowi['guarantor_name'].'},'; } $guarantors .= ']'; }else{ $guarantors = "0"; }}else{ $status = 0; $returnText = "إسم المستخدم تم إستخدامه من قبل !";}echo json_encode(array("status"=>$status,"name"=>$name,"guarantors"=>$guarantors));?>我有这个 jquery 代码 $.ajax({ url:"read_client.php", method:"POST", data: {code:$('.code').val()}, dataType: 'json', success:function(response){ if(response.status == 1){var result = $.parseJSON(response);console.log(result.guarantors.name) YAFloader.close() }else{ Swal.fire( 'error' ) YAFloader.close() } } });现在我想获取每个id和namefrom guarantors,但我做不到我需要帮助,请快点,有人可以帮助我吗???谢谢你们
1 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
$guarantors应该是一个数组:
$guarantors = [];
if (mysqli_num_rows($resulti) > 0) {
while ($rowi = mysqli_fetch_array($resulti)) {
$count++;
$guarantors[] = [
'id' => $rowi['id'],
'name' => $rowi['guarantor_name'],
];
}
}
然后在js端:
result.guarantors.map(guarantor => {
console.log(guarantor.id, guarantor.name);
})
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报
0/150
提交
取消