Hristian Yordanov
Hristian Yordanov

Reputation: 668

Using $_SERVER['QUERY_STRING'] to pass variables

I have a problem. A user is entering two dates in to a form. When the user presses submit, the date is passed to another opened page(1) and doing stuff for DB query.

Can I pass the variables to one more PHP file? To create a JSON file there. I need to create this JSON right after the user hits submit and then redirect to the page(1).

I'm using (GET method) this to redirect and pass the two dates:

    header("location:gentable_chart.php?" . $_SERVER['QUERY_STRING']);
    exit();

I need to pass the variables one more time to create the JSON before the redirect. Any idea how to do this?

Upvotes: 1

Views: 106

Answers (1)

IcedAnt
IcedAnt

Reputation: 454

When you say "JS that calls PHP file", I assume you mean AJAX. In that case, just pass the sessions variables as data:

$.ajax(
{
    url: 'yourphpfile.php',
    data: 
    {
        var1 : '<?php echo $_SESSION['var1']; ?>',
        var2 : '<?php echo $_SESSION['var2']; ?>'
    },
    type: 'GET',
    success: function(result)
    {   
        // Code
    }
})

Then on yourphpfile.php, you can catch the variables like so:

$var1 = $_GET['var1'];
$var2 = $_GET['var2'];

Upvotes: 2

Related Questions