•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,841 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,754 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 3010 | Replies: 6 | Solved
![]() |
•
•
Join Date: Sep 2004
Posts: 13
Reputation:
Rep Power: 5
Solved Threads: 0
Help with querystring error.
I am try to pass a query string to the same page but having problem
This is my code:
<HTML><BODY>
<div align=center>
<A href="?page=1">Page 1</A> -
<A href="?page=2">Page 2</A> -
<A href="?page=3">Page 3</A>
</div><hr>
<? switch($page) {
case "1" :?>
THis is page one
<? break; case "2" :?>
This is page two
<? break; case "3" :?>
This is page three
<? break; default:?>
This is the default page
?}
?>
</BODY></HTML>
Why am I having the following error:
Notice: Undefined variable: page in c:\inetpub\wwwroot\ex1\qstring.php on line 7
I appreciate any help
thanks
tip
I am try to pass a query string to the same page but having problem
This is my code:
<HTML><BODY>
<div align=center>
<A href="?page=1">Page 1</A> -
<A href="?page=2">Page 2</A> -
<A href="?page=3">Page 3</A>
</div><hr>
<? switch($page) {
case "1" :?>
THis is page one
<? break; case "2" :?>
This is page two
<? break; case "3" :?>
This is page three
<? break; default:?>
This is the default page
?}
?>
</BODY></HTML>
Why am I having the following error:
Notice: Undefined variable: page in c:\inetpub\wwwroot\ex1\qstring.php on line 7
I appreciate any help
thanks
tip
•
•
Join Date: Jul 2004
Posts: 218
Reputation:
Rep Power: 5
Solved Threads: 4
[PHP]<HTML><BODY>
<div align=center>
<A href="">Page 1</A> -
<A href="">Page 2</A> -
<A href="">Page 3</A>
</div><hr>
<? switch($page) {
case "1" :?>
THis is page one
<? break; case "2" :?>
This is page two
<? break; case "3" :?>
This is page three
<? break; default:?>
This is the default page
<? } ?>
</BODY></HTML>[/PHP]
You forgot to put "<" at this "<? } ?>"
<div align=center>
<A href="">Page 1</A> -
<A href="">Page 2</A> -
<A href="">Page 3</A>
</div><hr>
<? switch($page) {
case "1" :?>
THis is page one
<? break; case "2" :?>
This is page two
<? break; case "3" :?>
This is page three
<? break; default:?>
This is the default page
<? } ?>
</BODY></HTML>[/PHP]
You forgot to put "<" at this "<? } ?>"
•
•
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation:
Rep Power: 7
Solved Threads: 6
That is not the problem, at least with regard to the error specified. The problem is that you think that by visiting mysite.php?page=2, the variable $page will be set to 2. In the majority of circumstances (default since PHP 4.0.2, I believe), this (fortuantely, from the side of security) is false.
If you want to retrieve the variable page from your URL string, you need to access it with $_GET['page']. So, while PoA is correct in noticing the missing <?php } ?> towards the end of your script, you also need to change [php]switch( $page ) {[/php] to [php]switch( $_GET['page'] ) {[/php]
(Although, it's odd that the script should run at all - as it is apparently doing to produce a notice rather than a parse error - if there was a missing closing brace.)
If you want to retrieve the variable page from your URL string, you need to access it with $_GET['page']. So, while PoA is correct in noticing the missing <?php } ?> towards the end of your script, you also need to change [php]switch( $page ) {[/php] to [php]switch( $_GET['page'] ) {[/php]
(Although, it's odd that the script should run at all - as it is apparently doing to produce a notice rather than a parse error - if there was a missing closing brace.)
•
•
Join Date: Sep 2004
Location: in the j00-kay
Posts: 114
Reputation:
Rep Power: 5
Solved Threads: 5
should really have a break after the default: and before }
and no real need to use _GET with $page cos switch works on gets anyways :x
and no real need to use _GET with $page cos switch works on gets anyways :x
I'm pink, therefore, im spam.
http://www.vivaci.net - Quality Webhosting
http://gaming.vivaci.net - FAST UK Gaming servers
http://www.getsigned.org - Free Image Uploads
http://www.vivaci.net - Quality Webhosting
http://gaming.vivaci.net - FAST UK Gaming servers
http://www.getsigned.org - Free Image Uploads
•
•
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation:
Rep Power: 7
Solved Threads: 6
Sorry, what? I'm not quite sure what you mean there.
Unless your PHP.ini file has been explicitly modified to do this (register_globals = on), $_GET['page'] will NOT be the same as $page. $_GET is an array, containing the URI-passed values.
So, if you visit blah.php?foo=bar&bob=42
Then $_GET['foo'] will equal 'bar', and $_GET['bob'] will equal 42. $foo and $bob will be undefined; as $page was in Tip's original code.
Unless your PHP.ini file has been explicitly modified to do this (register_globals = on), $_GET['page'] will NOT be the same as $page. $_GET is an array, containing the URI-passed values.
So, if you visit blah.php?foo=bar&bob=42
Then $_GET['foo'] will equal 'bar', and $_GET['bob'] will equal 42. $foo and $bob will be undefined; as $page was in Tip's original code.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Working classes with JSP (JSP)
- help connecting a form to a database.. error message (ASP.NET)
- Page hangs or displays connection pooling max size error (ASP.NET)
Other Threads in the PHP Forum
- Previous Thread: uploading image in database
- Next Thread: Multiple PHP INCLUDES


Linear Mode