Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

21 views
Skip to first unread message

bhk...@gmail.com

unread,
Feb 17, 2015, 2:54:54 AM2/17/15
to
In the first php page I am calling jquery function get_ver() which will get the values from drop down box. I am able to get the value selected in the jquery variable "ver", but I am not able to pass and get it second php page.

First PHP Page and Jquery :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script language="javascript">
function get_ver() {
$(document).ready(function() {
var ver = $("#OSVER option:selected" ).text();
alert (ver);
$.ajax({
type : 'POST',
url: 'create_policy.php',
data: { 'OSVER' : ver }
});

});
}
</script>
<?php

<b><a href='index.php?xtype=create_policy&pol_id=$pol_id&pol_type=$pol_type' class='texthidevisi' onClick='get_ver()' >Edit |</a></b>


Second PHP Page :
<?php
$ver=$_POST['OSVER'];
echo "Version is $ver";
?>

Can any one help here?

Denis McMahon

unread,
Feb 17, 2015, 7:00:00 AM2/17/15
to
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.

--
Denis McMahon, denismf...@gmail.com

bhk...@gmail.com

unread,
Feb 17, 2015, 11:35:55 PM2/17/15
to
Thanks Denis.. You are saying to pass the data in format like json or html..right?

Denis McMahon

unread,
Feb 18, 2015, 9:54:31 AM2/18/15
to
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, denismf...@gmail.com
0 new messages