0

My site is organized into topics. Users can switch between topics on any page, at any time. I would like to be able to pass this topic along from page to page. I am guessing this should be done in a php post or get variable. I can grab the topic from the post or get variable and then run the rest of my site. However, this seems like it requires a form on every page to pass along this variable. As of now, the only way I have passed post or get variables was from forms on the previous page. I have never passed along these variables over several pages. Will I need a form on every page to pass these variables? Also, is this the standard way of doing this?

4 Answers 4

1

You should probably use GET, because it sounds like you're just trying to display different information, and POST is supposed to be for performing changes or actions.

If you decide to use GET variables, all you have to do is append them to the end of the link's href:

<a href="something.php?topic=bananas">MORE BANANAS</a>
Sign up to request clarification or add additional context in comments.

2 Comments

but if the user is browsing the site and visiting different pages, how can i keep passing "bananas" around. To me, it seems like i would have to turn every link into a from so that the variable could continue.
Something like <a href="something.php?topic=<?php echo $_GET['topic']; ?>">MORE BANANAS</a>. Also, +1 for making me laugh about bananas. Bananas are good.
1

the least overhead method of doing this would be to add some javascript that sets a cookie each time someone navigates to a new topic. This would assume you can select somehow all links that match topics (presumably trough classes)

A better method - because of compatibility,reliability and overhead - but not necessarily feasible, if a large number of links needs changing, is to use GET requests, as another poster suggested

Comments

0

You can create a helper function to generate your anchor tags and just append any existing query string to it, so instead of this:

<a href="page2.php?foo=bar&baz=bat">Foobar</a>

you would do this:

<?php echo anchor('page2.php','Foobar'); ?>

where your function would look like this:

/**
 * Function creates an anchor tag and optionally 
 * appends an existing query string
 * @param string $url
 * @param string $txt
 * @param bool $attach_qs Whether or not to follow a query string
*/
function anchor($url, $txt, $attach_qs = true) 
{
    $qs = '';
    if ($attach_qs === true) {
        $qs = (!empty($_SERVER['QUERY_STRING'])) ? '?' . $_SERVER['QUERY_STRING'] : '';
    }
    return '<a href="' . $url . $qs . '">' . $txt . '</a>';
}

Comments

0

Kolink's suggestion of placing the topic in via a PHP echo statement for every URL would certainly work. There is however, another option that I am surprised hasn't come up yet.

You could use PHPs Session Manager to store the variable. It is similar to using cookies; however, it is only temporary (limited to the session). Where a cookie can be persistent over multiple sessions.

<?php
// use this code before the page is generated, before the topic is decided.
session_start();
if (isset($_GET['topic']) && $_GET['topic'] != $_SESSION['topic']) {
    // GET['topic'] is set, session variable does not match
    // you may want to sanitize or limit what can be passed via ?topic=
    $_SESSION['topic'] = $_GET['topic'];
} else if (isset($_SESSION['topic'])) {
    // Session topic is not empty, run code to display appropriate content
} else {
    // No topic is set, display default
}
?>

It's by no means the only solution, but it does give you an extra option.

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.