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


Groups > comp.lang.php > #14934 > unrolled thread

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

Started bybhk567@gmail.com
First post2015-02-16 23:54 -0800
Last post2015-02-18 14:53 +0000
Articles 4 — 2 participants

Back to article view | Back to comp.lang.php


Contents

  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

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

Frombhk567@gmail.com
Date2015-02-16 23:54 -0800
SubjectPassing jquery variable from one php page to another php page using jquery and ajax is not working
Message-ID<adf3d6dc-194f-4d98-9a2f-b9a10d4fb3a0@googlegroups.com>
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?

[toc] | [next] | [standalone]


#14935

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-02-17 11:59 +0000
Message-ID<mbvaeg$tkc$3@dont-email.me>
In reply to#14934
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, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#14940

Frombhk567@gmail.com
Date2015-02-17 20:35 -0800
Message-ID<9d87d6a8-c59d-42ca-a8cd-fd4d8d1f5a6d@googlegroups.com>
In reply to#14935
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.
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com
Thanks Denis.. You are saying to pass the data in format like json or html..right?

[toc] | [prev] | [next] | [standalone]


#14943

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-02-18 14:53 +0000
Message-ID<mc291r$2iq$4@dont-email.me>
In reply to#14940
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web