dongxi1965 2015-12-26 14:21
浏览 71

PHP,AJAX,JQuery获得返回值

I am trying to get a value in the div on a return to a AJAX call to PHP page.

I tried the following:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
    $.ajax({
        type: "get",
        url: "returneye.php",
        success: function (data, textStatus) {
            alert(data);
            var eye = $('#valueeye').html(data);
            alert(eye);
        }
    });
});
</script>

And the returneye.php has the following:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Test PHP Return</title>
    </head>
    <?php
        for ($i = 0; $i <= 20; $i++)
        {
            echo $i;
        }
    ?>
    <div id="valueeye" hidden><?php echo $i; ?></div>
    <body>
    </body>
</html>

But I do not get the value of i.

I tried with

$('#valueeye').html(data).val();

I see undefined in the alert box.

How do I achieve this.

  • 写回答

2条回答 默认 最新

  • doujianwei8217 2015-12-26 14:56
    关注

    seems you trying to test use ajax ..

    in index.php

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
    $(document).ready(function(e) {
        $.ajax({
            type: "get", // you may no need this line because ajax is set type get by default
            url: "returneye.php",
            success: function (data) {
                alert(data);
                $('#valueeye').html(data);
            }
        });
    });
    </script>
    <div id="valueeye"></div>
    

    in returneye.php

    <?php
    for ($i = 0; $i <= 20; $i++)
    {
        echo $i;
    }
    ?>
    

    Note be sure your index.php and returneye.php in the same path

    This code output

    01234567891011121314151617181920
    

    Finally The working code is

    $(document).ready(function(){
       // run a function to get new content onload
       getnewContent(0);
    
       // window scroll event
       $(window).on('scroll' , function(){
           var windowscrollTop = $(this).scrollTop(); // window scrollTop
           var windowHeight = $(this).height(); // window height
           var documentHeight = $(document).outerHeight(true); // document height
           // if scroll reach to the bottom
           if(windowscrollTop == documentHeight - windowHeight){
            // get last element 
            var lastElement = getlastelementdataattr();
            alert(lastElement);
    
            // run function to append new content and pass the last element 
            getnewContent(lastElement);
           }
       });
    });
    
    // function to get new content (in your case you will get this from ajax so each div you create in php should have data-getContent attribute )
    function getnewContent(lastNum){
        for(var i = lastNum ; i <= lastNum + 30 ; i++){
        $('#showContent').append('<div class="" data-getContent="'+i+'">Content '+i+'</div>');
      }
      // your ajax should be like this
      /*
      $.ajax({
            type: "post", // use post method here 
            url: "returneye.php",
            data : {lastElement : lastNum},
            success: function (data) {
                alert(data);
                $('#valueeye').html(data);
            }
       });
       // and in php get lastnum with
       <?php 
           if(isset($_POST['lastElement'])){ // from data : {lastElement : lastNum}, in js
               echo (data);
               // then get the data from data base here
           }
       ?>
       */
    }
    
    // function to get the last element number from #showContent in my html above .. change #showContent with your div id or class you append the data inside it
    function getlastelementdataattr(){
        return parseInt($('#showContent div:last').attr('data-getContent'), 10);
    }
    

    Working Demo

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行