PHP,AJAX,JQuery获得返回值

时间:2015-12-26 14:21:15

标签: javascript php jquery ajax

我试图在返回到PHP页面的AJAX调用时获取div中的值。

我尝试了以下内容:

 class Directory
    {
        public string name { get; set; }
        public string address { get; set; }
        public string email_id { get; set; }
        public double phone_number { get; set; }

        public Directory(string n, string a, string e, double p)
        {
            this.name = n;
            this.address = a;
            this.email_id = e;
            this.phone_number = p;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string searchName;
            List<Directory> infoList = new List <Directory>();
            infoList.Add(new Directory("Rohan ", "Pashan ", "rohan.nevrikar@gmail.com ", 9974177791));
            infoList.Add(new Directory("Prithvi  ", "Naroda ", "prithvi.nirban@gmail.com ", 9974177792));
            infoList.Add(new Directory("Aagam  ", "Akshardham ", "aagam.nevrikar@gmail.com ", 9974177793));
            Console.Write("Which person's info do you want to search?? \n");
            searchName = Console.ReadLine();
            for(int i=0;i<infoList.Count;i++)
            {
                if (string.Compare(searchName, "Rohan") == 0)   
                {
                    Console.Write("Name : " + infoList[i].name + "\n");
                    Console.Write("Address : " + infoList[i].address + "\n");

                    Console.Write("Email id : " + infoList[i].email_id + "\n");

                    Console.Write("Phone number : " + infoList[i].phone_number + "\n");
                    break;
                }
            }                 
            Console.ReadKey();
        }
    }
}

returneye.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",
        url: "returneye.php",
        success: function (data, textStatus) {
            alert(data);
            var eye = $('#valueeye').html(data);
            alert(eye);
        }
    });
});
</script>

但我没有得到<!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> 的价值。

我试过

i

我在提醒框中看到$('#valueeye').html(data).val();

我如何实现这一目标。

2 个答案:

答案 0 :(得分:1)

似乎你试图测试使用ajax ..

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;
}
?>
  

注意请确保您的index.php和returneye.php在同一路径中

此代码输出

01234567891011121314151617181920

最后工作代码

$(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

答案 1 :(得分:0)

如果您要将来自returneye.php的请求发送到returneye.php,请使用以下代码

<?php
    if(!empty($_GET['target'])){
        for ($i = 0; $i <= 20; $i++)
        {
            echo $i;
        }
        die();
    }
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Test PHP Return</title>

    <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?target=getdata",
              success: function (data, textStatus) {
                  alert(data);
                  var eye = $('#valueeye').html(data);
                  alert(eye);
              }
           });
        });
</script>

</head>
<body>

    <div id="valueeye" ></div>
</body>
</html>

根据Maanish的评论进行编辑

JS

$.ajax({
      type: "get",
      url: "some_page.php",
      success: function (data, textStatus) {
          alert(data);                  
      }
});

PHP

some_page.php

<?php  
    $some_var = 1245;
    echo $some_var;
    die();

?>