Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14943

Re: Passing jquery variable from one php page to another php page using jquery and ajax is not working

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: Passing jquery variable from one php page to another php page using jquery and ajax is not working
Date 2015-02-18 14:53 +0000
Organization A noiseless patient Spider
Message-ID <mc291r$2iq$4@dont-email.me> (permalink)
References <adf3d6dc-194f-4d98-9a2f-b9a10d4fb3a0@googlegroups.com> <mbvaeg$tkc$3@dont-email.me> <9d87d6a8-c59d-42ca-a8cd-fd4d8d1f5a6d@googlegroups.com>

Show all headers | View raw


On Tue, 17 Feb 2015 20:35:46 -0800, bhk567 wrote:

> On Tuesday, February 17, 2015 at 5:30:00 PM UTC+5:30, Denis McMahon
> wrote:
>> On Mon, 16 Feb 2015 23:54:42 -0800, bhk567 wrote:
>> 
>> > In the first php page ....
>> 
>> Your output from the second php page is probably received in the ajax
>> handler of jquery. You probably need to send it in a format that
>> handler can use, and tell that handler what to do with it.

> Thanks Denis.. You are saying to pass the data in format like json or
> html..right?

I have no idea what if anything $.ajax is expecting back from the web 
server, what it expects to do with it, or how it is going to process it. 
You need to look at the api for $.ajax and then make that ahtever your 
web server sends back to $.ajax is something that $.ajax can do something 
useful with.

What I am guessing is that you're using jquery (which I have never used) 
and that $.ajax is a mechanism in jquery to make a request to a web serevr 
and receive a response from that server behind the scenes of the 
currently displayed page. I'm guessing that the jquery $.ajax api has a 
mechanism to receive a response from the server into a variable, and then 
use the content of that variable. I'm further guessing that the 
underlying data is text, but that it might be text in any one of a number 
of 'formats', eg json, html, xml, xhtml etc depending on what the web 
server sends.

When I want to send a json string from php as a response to a request, I 
do something like this:

$value_1 = 'This is some text';
$value_2 = array(1,2,3,4,5);
$value_3 = array('a'=>1, 'b'=>array(6,7,8), 'c'=>array
('d'=>9,'e'=>'z','f'=>18.6));
$value_4 = 42;

$arr = array(
  'id1' => $value_1,
  'id2' => $value_2,
  'id3' => $value_3,
  'id4' => $value_4,
);
$data = json_encode($arr);
header('Content-type: application/json');
echo $data;

Make absolutely sure that <?php is right at the start of your php script, 
and that you have no "non php" sections in the script. The only thing you 
want to send from the script is usually the content type header and the 
json string. If you have anything, even a blank line or space at the 
start of the file before the <?php, or any non php sections, these will 
usually trigger the web server into sending html headers which may break 
the api at the client end.

Normally I wouldn't actually nest arrays inside arrays unless it was 
appropriate to the data being transferred.

In the javascript, once the string is decoded to some object called eg x, 
I can access the values as: x.id1, x.id2[2], x.id3.b[1], x.id3.c.f etc.

So what you need to do is (a) work out what you're using $.ajax for, (b) 
read the api, (c) work out that it expects to receive from the server, 
(d) work out what php you need to send that response, (e) get the 
response back from the $.ajax in your javascript and presumably (f) use 
the data embedded in the response to update something on the web page.

If you're just trying to send form data to the server and display a "data 
accepted" web page, you might as well skip all the ajax stuff and just 
submit the form, responding with normal html after processing the form 
data.

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.php | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Passing jquery variable from one php page to another php page using jquery and ajax is not working bhk567@gmail.com - 2015-02-16 23:54 -0800
  Re: Passing jquery variable from one php page to another php page using jquery and ajax is not working Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-17 11:59 +0000
    Re: Passing jquery variable from one php page to another php page using jquery and ajax is not working bhk567@gmail.com - 2015-02-17 20:35 -0800
      Re: Passing jquery variable from one php page to another php page using jquery and ajax is not working Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-18 14:53 +0000

csiph-web