Jump to content

Using $GET to load sections.


spookztar

Recommended Posts

Hi guys,

 

I'm having a problem with $_GET coming up empty. I tried searching, but couldn't get to anything with the criteria I used.

 

I have a menu:

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=1">Product Handling</a><br />

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=2">Misc. Parameters</a><br />

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=3">Statistics</a><br />

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=4">Look 'n' Feel</a>

 

- and when a link is clicked I want the appropriate section of the index.php file to display to the user only using the one file. But when I try to pick up the value with: $sectionid = $_GET['sectionid']; I just get:

 

Notice: Undefined index: sectionid in /home/arioch/public_html/musiccenter.php on line 195

 

What do I need to do pick up the variable in the URL, so I can use it to load the correct section of the file to the user, using only the one page?

 

Bye,

Link to comment
https://forums.phpfreaks.com/topic/62363-using-get-to-load-sections/
Share on other sites

It looks to me like your error reporting is set very, very strictly....

 

Try doing:

 

$sectionid = (isset($_GET['sectionid']) && is_numeric($_GET['sectionid'])) ? $_GET['sectionid'] : 1;

 

isset doesn't flag an error if you try to check a non existant variable (or key) with it.  This will just check if it's set and numeric, and if it is, it will assign it to $sectionid, but if it's empty, it'll assign the default value of 1 to it.

no it does matter semicolons are used for the compiling purposes.  It is what tells the complier hey this is the end of this echo or the end of a variable otherwise what it is doing is its using the question marks as a crude if statement i think or classes, just use proper punctuation

try this

<?php
$links[1] = "Product Handling";
$links[2] =  "Misc. Paramenters";
$links[3] = "Statistics";
$links[4] = "Look \'n\' Feel";
foreach ($links as $key => $value){
echo "<a href=\"".$_SERVER['self']."?sectionid=".$key."\">".$value."</a>";
}
?>
//Page 2
<?php
if(ISSET($_GET['sectionid'])){$pageid = $_GET['sectionid'];}

?>

then you can add more by appending this array

"are you sure $sectionid is being set before you're calling it?"

 

Isn't that the whole point of: sectionid = $_GET['sectionid'];? I'm not in doubt as to whether it's set or not, but why it isn't.

 

I included the semicolons, didn't do a thing. Strict error reporting obviously didn't care either... ;)

When set very strictly, the PHP error handling will flag warnings when you try to use not-set variables....

 

<?php

$link = array();
$links[] = "Product Handling";
$links[] =  "Misc. Paramenters";
$links[] = "Statistics";
$links[] = "Look \'n\' Feel";
foreach ($links as $key => $value){
echo "<a href=\"page2.php?sectionid=".$key."\">".$value."</a>";
}
?>

 

Then page2:

 

<?php
$sectionid = (isset($_GET['sectionid'])) ? $_GET['sectionid'] : 0;
echo $sectionid;
?>

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.