Javascript 如何提取/分割数据的一部分
我使用以下PHP代码从数据库中选择数据:Javascript 如何提取/分割数据的一部分,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我使用以下PHP代码从数据库中选择数据: <?php include './database_connect.php'; $ppid=$_POST['selectPatientID']; $query="SELECT * FROM patient WHERE p_Id='$ppid'"; $result= mysqli_query($conn, $query); while($row= mysqli_fetch_array($result)){ echo ($row['p
<?php
include './database_connect.php';
$ppid=$_POST['selectPatientID'];
$query="SELECT * FROM patient WHERE p_Id='$ppid'";
$result= mysqli_query($conn, $query);
while($row= mysqli_fetch_array($result)){
echo ($row['p_fname']);
echo ' ';
echo ($row['p_lname']);
}
?>
您必须
json\u编码
$row = mysqli_fetch_array($result)){
header('Content-Type: application/json');// header for json encode response
echo json_encode($row);
exit;
您的jQuery ajax代码将是:
$.ajax({
url: "UDPatient.php",
type: 'POST',
data: {selectPatientID: $('#selectPatientID').val()},
dataType: 'json',// to receive data into json format
success: function (result) {
alert(result.p_fname);
alert(result. p_lname);
}
});
你应该试着做一些类似的事情
<?php
include './database_connect.php';
$ppid=$_POST['selectPatientID'];
$query="SELECT * FROM patient WHERE p_Id='$ppid'";
$result= mysqli_query($conn, $query);
$row= mysqli_fetch_array($result);
echo json_encode($row);
?>
可以解析json结果(或者将数据类型设置为“json”)
json\u encode()
php中的结果数组,以及json.parse()
php中的结果javascript@chris85:是的,在我的php
文件中,有两个值是从数据库中获取的。我怎样才能把它们分开?@Manav:你能帮我吗,先生???从文件中提供XML或JSON。或者,如果您对SQL注入持开放态度,则应该参数化该查询。
<?php
include './database_connect.php';
$ppid=$_POST['selectPatientID'];
$query="SELECT * FROM patient WHERE p_Id='$ppid'";
$result= mysqli_query($conn, $query);
$row= mysqli_fetch_array($result);
echo json_encode($row);
?>
<script>
$(document).ready(function () {
$('#selectPatientID').on('change', function (event) {
event.preventDefault();
$.ajax({
url: "UDPatient.php",
type: 'POST',
data: {selectPatientID: $('#selectPatientID').val()},
dataType: 'html',
success: function (result) {
var jsonResult = JSON.parse(result);
var p_fname = jsonResult.p_fname;
var p_lname = jsonResult.p_lname;
// whatever else
}
});
});
});
</script>