简体   繁体   中英

javascript post to .php file one server does not work - manual entry of URL in browser to same .php works

i had some issues with understanding how to get javascript (client) variables transferred so they were acessible from php (serverside) as session : get an iframe's "src" value in PHP?

Now im in a situation where i use firebug to try to debug whats going on, but it just doesnt make sense :

i have this function to update an iframe and i want to pass on the page that that iframe is displaying :

function frameclick(pageurl)
 {
        $.post("session_write.php?",
    {
            frameurl : pageurl             
    }

    $("#iFrame1").attr('src', pageurl);
    console.log  ('<?php echo "logout:".$langpath.$_SESSION['frameurl'];?>');
 }

pageurl is ex. "/lang/en/new.htm" - and if i inspect it with firebug i also can see it says that it passes it correctly ( also with conversion of /).

my script serverside that its posted to is like this :

#session_write.php
<?php
session_start();
print_r($_GET['frameurl']);
if (isset($_GET['frameurl'])) 
{
$_SESSION['frameurl'] = $_GET['frameurl'];
print_r($_SESSION);
}
?>

Posting to that php script on the server will fail via the javascropt - $_SESSION['frameurl'] will be '', but if i ex. do it manually like this : (http):

//localhost/phpmenu/session_write.php?frameurl=lang%2Fen%2Fnew.htm

then it will be correctly set in the $_SESSION["frameurl"] variable.

I simply cannot understand whats different between doing the javascript post and doing it manually in the browser and why its causing me this problem ?

anyone with an idea ? thanks

You are using .post , which executes a POST request, but when you type in the URL in the address bar, that is a GET request.

$_GET retrieves any params passed through GET, while $_POST retrieves any params passed through POST. So if you use .post with Javascript but try to retrieve with $_GET in PHP, it wouldn't work.

When you POST variables to a PHP file, $_GET is not set. Use $_POST['frameurl'] instead. Also, it looks like you're missing a close paren in frameclick to end the post call.

You are passing data via a POST request and retrieving for all the GET requests. Use $_POST instead. You may also be interested in $_REQUEST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM