Jared
Jared

Reputation: 1793

How do I pass a variable in URL using PHP?

I have a variable called $tags, which just references from a database field. When the user clicks a link with the given $tags output in it, I want that data to be stored in a variable on the target page.

For instance, the user is on a todolist.php, that contains several tasks. They can see the parts associated with this task by clicking a link that goes to a partslist.php page. In the link, I need to contains the $tags data, so javascript on the partslist.php page knows what part to highlight.

So I need to know 1) how do I output $tags in the link of todolist.php, and 2) how do I receive that output and store it on a variable on the partslist.php page?

I have done similar POST and GET commands, but I can't quite figure this out.

Upvotes: 0

Views: 344

Answers (4)

Stewie
Stewie

Reputation: 3131

partslist.php?tags=<?php echo urlencode($tags) ?> .. is this what you are asking ? Or there are multiple tags ? Please give examples / code blocks / links

On the partslists side you can do this :

$tags  = $_GET['tags']; // You don't need urldecode here, because $_GET is a supergobal and it is already decoded. 

Upvotes: 4

Nathan Long
Nathan Long

Reputation: 126112

If the URL for the link they click on contains URL parameters, those will be available on the target page. For instance,

// Generate a link like this with PHP
<a href="somepage.php?name=bob&weight=150">

// On somepage.php
$name = $_GET['name'];
$weight = $_GET['weight'];

Note: for security, you may want to encrypt this data before putting it in the link, and decrypt it on the other end.

For the part where you hand some data to Javascript, you can interpolate PHP in your Javascript the same as your HTML. So, you could say:

//JS
var foo = <?PHP echo $foo ?>;

Alternately, you could store data in an HTML element:

<div id="foo" data="<?PHP echo $data ?>">

... and then grab that using Javascript.

Upvotes: 0

ChrisR
ChrisR

Reputation: 14467

is $tags an array?

If so you can output the $tags array in the following format

partslist.php?tags[]=tag1&tags[]=tag2&tags[]=tag3

if you would then output the $_GET global in the partslist.php script you would end up with an array like this:

Array
(
    [tags] => Array
        (
            [0] => "tag1"
            [1] => "tag2"
            [2] => "tag3"
        )

)

Upvotes: 1

methodin
methodin

Reputation: 6710

You pass URLs to pages in one of two ways: $_GET or $_POST. $_POST requires that you have a form setup like:

<form method="post" action="page.php">
<input type="text" name="myvariable" value="test">
<input type="submit">
</form>

And $_GET requires that you link to the page like:

<a href="page.php?myvariable=test">click me</a>

In the first scenario, when the user hits submit, page.php will have access to the variables submitted from the $_POST array. In this case, $_POST['myvariable'] will equal "test";

In the second, $_GET['myvariable'] will equal test.

Upvotes: 2

Related Questions