Jump to content

$_GET and URL parameters


cheesybiscuits

Recommended Posts

I got two links with a parameter with a value. When I click either link it will send me to the same challenge.php page, I'm wanting to find the value of the parameter and display the page according to what value the parameter has in the url.

This script doesn't seem to be working.

<a href="http://www.site.com/challenge.php?id=1">Complete Challenge 1</a>
<a href="http://www.site.com/challenge.php?id=2">Complete Challenge 2</a>

$id = $_GET['id'];
if ($id=1) {
        echo "This is challenge 1 completed";
} elseif ($id=2) {
echo "This is challenge 2 completed";
else {
echo "The URL parameters didn't work";
}

Any help would be appreciated thanks.

Link to comment
https://forums.phpfreaks.com/topic/256226-_get-and-url-parameters/
Share on other sites

When comparing two values, use the comparison operator == not the assignment operator =. Your code should looks like this.

 

$id = (int)$_GET['id'];
if ($id == 1) {
        echo "This is challenge 1 completed";
} elseif ($id == 2) {
echo "This is challenge 2 completed";
else {
echo "The URL parameters didn't work";
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.