duanlu0386 2015-06-16 08:04
浏览 61
已采纳

显示数组中的数据

In index.php page i have a script that fetches data from demo.php and displays the result in a div.

<div class="leftbox">
    <?php
        echo "<div id='proddisplay'>";
        echo "</div>";
    ?>
</div>

var onSubmit = function(e) {
    var txtbox = $('#txt').val();
    var hiddenTxt = $('#hidden').val();
    $.ajax({
        type: 'post',
        url: 'demo.php',
        data: {
            txt: txtbox,
            hidden: hiddenTxt
        },
        cache: false,
        success: function(returndata) {
            $('#proddisplay').html(returndata);
            console.log(returndata);
        },
        error: function() {
            console.error('Failed to process ajax !');
        }
    });
};

in demo.php script i get the result from

print_r($result);

the result that i get in the div is

Array ( [0] => 1st value [1] => 2nd value [2] => 3rd value [3] => 4th value )

I wish to get individial data from this array so that i can use each data seperately and wherever i want in the index.php page, but i am not able to do so. can anyone tell how can i fetch individual values from array for index page

P.S: i have an option to display the format in demo page in the form of result[0] and so on for every index value and call it as it is in the index page but that bounds me with the css. I wish to use any css in index page and just call the values in index page.

I tried using result[0] in index page but it displayed no result i tried assigning the values of an array to a variable and then call these variables to index page but that also didn't displayed any result

  • 写回答

5条回答 默认 最新

  • douzhi9478 2015-06-17 05:42
    关注

    convert your array to json

    echo json_encode($result);
    

    in your script

    var onSubmit = function(e) {
      var txtbox = $('#txt').val();
      var hiddenTxt = $('#hidden').val();
      $.ajax({
        type: 'post',
        url: 'test2.php',
        dataType: 'json',
        data: {
          txt: txtbox,
          hidden: hiddenTxt
        },
        cache: false,
        success: function(returndata) {
             $('#first').html(returndata[0]);
              $('#second').html(returndata[1]);
               $('#third').html(returndata[2]);
                $('#fourth').html(returndata[3]);
             console.log(returndata[0]);
        },
        error: function() { 
          console.error('Failed to process ajax !');
        }
      });
    };
    

    first,second,third,fourth will be the id of the div or the area where you wish to display the result

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线