tags:

views:

65

answers:

5

hi,

I am using jquery to post vars to a php script.

Is it possible to access these vars once the script has posted? If I post the data to the php script, is the only way to access it in the html/js that i posted it from, to send it back from the php script as json?

I cant seem to do it in JS, but even php will not work?, Sorry correction i can access the post vars in the php page, but not in the html/js page i posted from

Any ideas how to access posted vars from the page thats doing the posting?

update: yep to be a bit clearer, i am using a html page with javascript to post to a php page, i would like to access the posted vars in the html javascript page. I tried outputting $.post and $.ajax and these just show a long function.

Cheers

Ke

+2  A: 

In PHP, the data should be accessible through the $_POST array, just like if you posted to the script using a form (whether you make an AJAX request or a normal request through your browser, the server behaves the same). If they're not there, perhaps you actually sent your data by GET instead (you could check $_REQUEST, but it's better, and more secure, to know what method your data will be coming in), or else your AJAX request failed.

Daniel Vandersluis
im not trying to get the posted data in the php script, id like to use the posted data in the html/js script thats posted from, any idea how to do that?
Ke
+3  A: 

How are you submitting your elements to php page? If you are doing everything fine and using ajax (see jquery post method) for example, you can access the php variables normally with $_POST['var_name'].

Make sure that:

  • Your form method type is set to POST
  • Your ajax request goes successful
  • You have specified the correct path to your server side script
Sarfraz
yes this is all working fine, im trying to put the posted variables inside a link on the html/js page, the page that i posted from
Ke
A: 

I assume that you are using $.ajax or $.post to do this.

Just keep your data in local variables. There i sno reason why you should lose the posted data, or your php not being able to access it.

If you post code, maybe someone can help better.

Here Be Wolves
im using flexigrid, and its posting the data directly to the php script, im using firebug and all looks ok, im just having trouble using the posted data inside the html/js that i posted from
Ke
+1  A: 

I don't recommend using $_REQUEST to post something back to your site. If someone changes their $_REQUEST vars on you, then you have an opening for cross site scripting.

Push all your vars to $_SESSION and post them as you see fit but only after they have been purified. That way even if you make some modifications to them after the fact you can rely on the source, which is in the $_SESSION. However if you are trying to perform actions after a page has executed you are straying outside the boundaries of PHP's limitations. People do it all the time with things like Jquerry but it doesn't make it right.

Warning: if you allow accessing and process of vars after PHP has finished printing the page, then you run the risk of enabling attacks on your code.

Geekster
A: 

In your php function, you can use this:

    function foo() {
        //do something
        echo json_encode($var);
    }

I use .ajax, use dataType: "json". The success attribute would be:

$.ajax(
    {
        url: 'ajaxfile.php', 
        type: "POST",
        data: ($("#myForm").serialize()), 
        dataType: "json",
        success: function(data)
        {
                       //Insert your logic to handle the vars from php
        }
    });
David
the 2nd one doesnt make any sense to me
Ke
Edited to show the whole ajax function
David