在ajax中取消两次成功

问题描述 投票:0回答:1

我想在ajax中请求后分离结果

<input type="text" class="form-control" name="userno" id='stud_id' readonly > 
<input type="text"name="studentname" id='studentname' readonly >
<input type='submit' name='stud' onclick='showstudent_info()'>

function showstudent_info(){    
var  studid = $('#studid').val();
  console.log(studid);
    if(studid){
        $.ajax({
            type:'POST',
            url:'parentinfo.php',
            data: 'studid='+studid,
            success:function(html){
                var infoid = html
                $('#stud_id').val(info);
                var studname = html
                $('#studentname').val(studname);
            }
        }); 
    }
  } 

这是我的parentinfo.php页面

parentinfo.php
$stud_id = $_POST['studid'];
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl where 
stud_id = $stud_id";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
    extract($row);
    $info = $row['stud_id'];
    $studname = $row['Name'];
}
echo $info;
echo $studname;

我的问题是infoid的价值和studname加入,例如(1Albert Einstein)

javascript php html ajax
1个回答
2
投票

将其作为JSON发送,以便您可以将其分解为服务器端,使其作为javascript对象客户端可读,而不是解析从服务器发送的字符串

在PHP中将是这样的:

$outputArray = array(
   'id'=> $idVariable,
   'name'=> $nameVariable
);

echo json_encode($outputArray);

然后在js中将dataType:'json'添加到ajax选项中,成功回调将类似于:

success:function(responseObject){
     var infoid = responseObject.id;
     $('#stud_id').val(infoid );
     var studname = responseObject.name;
     $('#studentname').val(studname);
}
© www.soinside.com 2019 - 2024. All rights reserved.