weixin_33691700 2015-01-06 20:20 采纳率: 0%
浏览 29

PHP AJAX JSON迭代

I am familiarizing myself with ajax and json.

My page is addAccount.php and inside, I have a FORM with the INPUTs below:

 <input type="text" id="partnerCode" />
 <input type="button" id="pCodeSearch" value="Search" />

I have a JavaScript page called global.js the gets the information:

 $('input#pCodeSearch').on('click' , function()
 {
   var partnercode = $('input#partnerCode').val();
   if($.trim(partnercode) != '')
   {
     $.post('api/pCodeSearch.php', {partnercode: partnercode}, function(data)
     {
       $('div#partner-data').text(data);
     });
   }
 });

In the above code, you will see the processing page called pCodeSearch.php. Here is that code:

 <?php
   if(isset($_POST['partnercode']) === true && empty($_POST['partnercode']) === false)
   {
     require "../include/database.php";
     require "../include/sessions.php";

     $search = "SELECT FULL_NAME, PARTNER_CODE from partner WHERE PARTNER_CODE = '"htmlentities(stripslashes(trim($_POST['partnercode'])))."'";

     $query = mysqli_query($dbc, $search); // $dbc is the connection string

     if(mysqli_num_rows($query) !== 0)
     {
       $out = array();
       while($row = $query->fetch_assoc())
       {
         $out[] = $row;
       }
       echo json_encode($out);
       mysqli_free_result($query);
     }
     else
     {
       echo 'Partner not found';
     }
   }
 ?>

With all of the above code, I can display in a DIV tag called #partner-data, as you saw in my JavaScript code above.

But this is the output:

 [{"FULL_NAME":"PARTNER SA","PARTNER_CODE":"0000011182"}]

I would like to echo out just PARTNER SA and 0000011182.

I attempted this on my JavaScript page:

 $.each(data, function(index, item)
 {
   $('<div>').
     attr('value', item.PARTNER_CODE).
     text(item.FULL_NAME).
     appendTo($('#partner-data'));
 });

But nothing returns to the screen.

  • 写回答

3条回答 默认 最新

  • weixin_33712987 2015-01-06 20:25
    关注

    Instead of:

    $('div#partner-data').text(data);
    

    try:

     $('div#partner-data').text(data.d.["FULL_NAME"]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵