2015-11-05 100 views
0

我有2個問題:阿賈克斯多行

1. 我得到我的Ajax從query.php這樣的:

echo json_encode($records, JSON_UNESCAPED_UNICODE); 

,給了我這樣的事情:

[{"cinfo_id":"25","fullName":"علی علوی","phone":"123456","mail":"[email protected]","description":"توضیحات علی علوی","jinfo_id":"2","jobTitle":"شرکت","jobName":"گلخانه","city":"مشهد"},{"cinfo_id":"26","fullName":"مصطفی کلانتر","phone":"09153040406","mail":"[email protected]","description":"توضیحات مصطفی","jinfo_id":"3","jobTitle":"شرکت","jobName":"مهندسی","city":"تهران"}] 

傾銷的是這樣的:

Array 
(
    [0] => Array 
     (
      [cinfo_id] => 25 
      [fullName] => علی علوی 
      [phone] => 123456 
      [mail] => [email protected] 
      [description] => توضیحات علی علوی 
      [jinfo_id] => 2 
      [jobTitle] => شرکت 
      [jobName] => گلخانه 
      [city] => مشهد 
     ) 

    [1] => Array 
     (
      [cinfo_id] => 26 
      [fullName] => مصطفی کلانتر 
      [phone] => 09153040406 
      [mail] => [email protected] 
      [description] => توضیحات مصطفی 
      [jinfo_id] => 3 
      [jobTitle] => شرکت 
      [jobName] => مهندسی 
      [city] => تهران 
     ) 

) 

一切看起來都很好,Ajaxy!但在我的PHP文件,我不能閱讀:

var key = { 
    'command': 'search', 
    'data': $("#inputsearch").val() 
}; 
$.ajax({ 
    type: 'POST', 
    url: 'query2.php', 
    data: key, 
    dataType: 'json', 
    success: function (msg) { 

     $('#jobtitle').html(msg.jobTitle); 
     $('#jobname').html(msg.jobName); 
     $('#fullname').html(msg.fullName); 
     $('#phone').html(msg.phone); 
     $('#mail').html(msg.mail); 
     $('#city').html(msg.city); 
     $('#description').html(msg.description); 

    } 
}) 

但是,當我替換 「[]」 與 「」 返回的JSON,

{"cinfo_id":"26","fullName":"مصطفی کلانتر","phone":"09153040406","mail":"[email protected]","description":"توضیحات مصطفی","jinfo_id":"3","jobTitle":"شرکت","jobName":"مهندسی","city":"تهران"} 

它得到正確的。問題是什麼?

  1. 如何顯示多行?在返回的json數組中,我擁有所有數據。像:
[{"cinfo_id":"25","fullName":"علی علوی","phone":"123456","mail":"[email protected]","description":"توضیحات علی علوی","jinfo_id":"2","jobTitle":"شرکت","jobName":"گلخانه","city":"مشهد"},{"cinfo_id":"26","fullName":"مصطفی کلانتر","phone":"09153040406","mail":"[email protected]","description":"توضیحات مصطفی","jinfo_id":"3","jobTitle":"شرکت","jobName":"مهندسی","city":"تهران"}]
 
Array 
(
    [0] => Array 
     (
      [cinfo_id] => 25 
      [fullName] => علی علوی 
      [phone] => 123456 
      [mail] => [email protected] 
      [description] => توضیحات علی علوی 
      [jinfo_id] => 2 
      [jobTitle] => شرکت 
      [jobName] => گلخانه 
      [city] => مشهد 
     ) 

    [1] => Array 
     (
      [cinfo_id] => 26 
      [fullName] => مصطفی کلانتر 
      [phone] => 09153040406 
      [mail] => [email protected] 
      [description] => توضیحات مصطفی 
      [jinfo_id] => 3 
      [jobTitle] => شرکت 
      [jobName] => مهندسی 
      [city] => تهران 
     ) 

) 

,但我不知道如何向他們展示。 在此先感謝。

+0

數組,AJAX代碼是JavaScript的不是PHP。 –

回答

2

如果您msg變量是對象的數組,你應該訪問它的對象

$('#jobtitle').html(msg[0].jobTitle); 
$('#jobname').html(msg[0].jobName); 
$('#fullname').html(msg[0].fullName); 
$('#phone').html(msg[0].phone); 
$('#mail').html(msg[0].mail); 
$('#city').html(msg[0].city); 
$('#description').html(msg[0].description);