开发者

access post vars JS or PHP?

I am using j开发者_高级运维query 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


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


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.


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 Jquery 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.


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.


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
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜