1

I try to pass a $_GET variable from js to php and return the content from php. I tried out the 1`st page: PHP CODE

<?php $page = $_GET[url];?>

JS CODE

var foo = '<?php echo $page ?>';
$.post('fbapp.php', {url: 'foo'});
$("#continut").load('fbapp.php', function () {
    $('.loader').fadeOut();
});

But every time the sended variable is empty and when i try to debug the $_POST variable from the fbapp.php file i get:

array (size=0)
  empty

Have any ideea ?

1
  • why are you using post on js and get on php? they are not the same. Commented May 4, 2016 at 6:36

5 Answers 5

2

You're using the string 'foo' when you mean to be using the JavaScript variable foo.

$.post('fbapp.php', {url: foo});
Sign up to request clarification or add additional context in comments.

Comments

1

You are sending the POST request and looking for variable in GET array. Try to use $_POST["url"]

Comments

1

You are doing wrong here,

  1. GET variable

    <?php $page = $_GET['url'];//Use quotes?>
    
  2. Use js variable proper

    $.post('fbapp.php', {url: foo}); // here 'foo' will be string
    

Comments

1

Try this:

<?php $page = $_GET['url'];?>

var foo = '<?php echo $page ?>';

$.post('fbapp.php', 
    {
        url: foo
    }
);

$("#continut").load('fbapp.php', function () {
    $('.loader').fadeOut();
});

Note: Send foo as variable not a string.

Comments

1

Change first line to

<?php $page = isset($_GET['url']) ? $_GET['url'] : '';?>

and use var instead of string in

var foo = '<?php echo $page ?>';
$.post('fbapp.php', {url: foo});
$("#continut").load('fbapp.php', function () {
    $('.loader').fadeOut();
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.