0

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.

3
  • 1
    You are doing it wrong as the data obtained from ajax call is not yet added to the current DOM. Do you have div with name valueeye on calling html page? Commented Dec 26, 2015 at 14:30
  • 2
    The returneye.php script will return the entire html document, including anything generated by PHP. I guess this is not what you want ( unless you are sending this response to an iFrame ) ~ change the php script to be only PHP. Commented Dec 26, 2015 at 14:32
  • I want to get the value specific PHP variable from a different page. Is this possible ? Commented Dec 26, 2015 at 15:58

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I want to get the value specific PHP variable from a different page. Is this possible ?
@Maanish I can't got your point , please explain well what you trying to do
I have two pages one that does the MYSQL QUERY and the other does ajax call to that page. The MYSQL query returns ids from the database. I wan't to display the IDs in the ajax call page and also keep in ajax call page memory the last ID. So that when I call to the MYSQL page next time I could send the last ID and retrieve data greater than the last ID.
@Maanish I think you trying to make a load more content and want to keep the last one to start from it in a next time .. right?
@Maanish I add how to use ajax in that with type:'post and how you pass variables to php jsfiddle.net/mohamedyousef1980/1xam3au5/8 and please read all the comments I typed in the fiddle
|
0

if you are sending the request from returneye.php to returneye.php then use following code

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

Edit according to comment made by 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();

?>

1 Comment

I want to get the value specific PHP variable from a different page. Is this possible ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.