How ajax get data from another page in php?

I am trying to get data from one php page and pass it to another page using Ajax.

JS :

$.ajax({
      url: "action.php",
      success: function(data){           
        $.ajax({
             url: "data.php?id=data"
      }
});

action.php :


data.php :




asked Jan 28, 2016 at 10:26

1

First of all, you need to echo your data in action.php, and second, use data parameter of AJAX request to send data to data.php.

Here's the reference:

  • jQuery.ajax()

So the organization of pages should be like this:

JS :

$.ajax({
    url: "action.php",
    success: function(data){ 
        $.ajax({
            url: "data.php",
            data: {id: data},
            success: function(data){ 
                // your code
                // alert(data);
            }
        });
    }
});

action.php :


data.php :


answered Jan 28, 2016 at 10:44

How ajax get data from another page in php?

Rajdeep PaulRajdeep Paul

16.8k3 gold badges17 silver badges37 bronze badges

10

Try to use $.get() method to get/send data :

$.get("action.php",{}, function(data){
    //data here contain 1
    $.get("data.php", {id: data}, function(id){
         alert(id);
    }
});

Just echo $test since just the data printed in page will return as responce to the query request.

action.php :


Hope this helps.

answered Jan 28, 2016 at 10:35

Zakaria AcharkiZakaria Acharki

65.8k15 gold badges71 silver badges97 bronze badges

1

For example,

Test



action.js

$('.dataClass').click(function(){
    var value=$(this).attr('data-value');
    $.ajax({url:"Ajax_SomePage.php?value="+value,cache:false,success:function(result){
        alert("success");
    }});
}); 

Ajax_SomePage.php


answered Jan 28, 2016 at 10:37

How ajax get data from another page in php?

Nana PartykarNana Partykar

10.4k10 gold badges45 silver badges76 bronze badges

To get data as response in ajax call, you need to echo the result from your php page; action.php page.

echo $test =  1;

In your provided code

$.ajax({
    url: "data.php?id=data"
}       // closing bracket is missing

you are sending the string data as id to data.php page. Instead you have to append the result with the url using + symbol like shown in the below code.

 $.ajax({
   url: "action.php",
   success: function(data){           
    $.ajax({
         url: "data.php?id="+data
    })
  }
});

answered Jan 28, 2016 at 10:29

How ajax get data from another page in php?

JenzJenz

8,1747 gold badges41 silver badges76 bronze badges

3

How do I pass a value from one page to another in AJAX?

The values of the TextBox and DropDownList will be sent to another page in four different ways using jQuery..
.

.

How pass data from one page to another in php using AJAX?

Use this : data:{primary_cat:val,category-select:cat_val} – RJParikh. ... .
The value 'category-select' still does not get outputted. To transfer it from the first page I am using $_POST['category_select'] = 'private'; ... .
@user6043723, Try to change your variable from category-select to category_select. – Nehal..

How AJAX call redirect to another page in php?

$. ajax({ type: "POST", url: "ajax. php", data: dataString, success: function(r) { $("#div"). html(r); } });

How does AJAX work with php?

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.