在ajax中击退两个成功

时间:2018-08-04 00:30:54

标签: javascript php html ajax

我想在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)

1 个答案:

答案 0 :(得分:2)

将其发送为JSON,这样您就可以将其分解为服务器端,使其可作为javascript对象客户端读取,而不必解析服务器发送的字符串

在php中将类似于:

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

echo json_encode($outputArray);

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

success:function(responseObject){
     var infoid = responseObject.id;
     $('#stud_id').val(infoid );
     var studname = responseObject.name;
     $('#studentname').val(studname);
}