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

显示数组中的数据

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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改