User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2004
Posts: 13
Reputation: tip is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tip tip is offline Offline
Newbie Poster

Help with Querystring error

  #1  
Oct 18th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Posts: 218
Reputation: PoA is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
PoA PoA is offline Offline
Posting Whiz in Training

Re: Help with Querystring error

  #2  
Oct 18th, 2004
[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 "<? } ?>"
Reply With Quote  
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation: Roberdin will become famous soon enough Roberdin will become famous soon enough 
Rep Power: 7
Solved Threads: 6
Colleague
Roberdin Roberdin is offline Offline
Supreme Evil Overlord

Re: Help with Querystring error

  #3  
Oct 18th, 2004
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.)
Reply With Quote  
Join Date: Sep 2004
Posts: 13
Reputation: tip is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tip tip is offline Offline
Newbie Poster

Re: Help with Querystring error

  #4  
Oct 18th, 2004
Thank you very much Roberdin and poA for your help. Now I can proceed

tip
Reply With Quote  
Join Date: Sep 2004
Location: in the j00-kay
Posts: 114
Reputation: ReDuX is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 5
ReDuX's Avatar
ReDuX ReDuX is offline Offline
Junior Poster

Re: Help with Querystring error

  #5  
Oct 18th, 2004
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
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
Reply With Quote  
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation: Roberdin will become famous soon enough Roberdin will become famous soon enough 
Rep Power: 7
Solved Threads: 6
Colleague
Roberdin Roberdin is offline Offline
Supreme Evil Overlord

Re: Help with Querystring error

  #6  
Oct 19th, 2004
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 13
Reputation: tip is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tip tip is offline Offline
Newbie Poster

Re: Help with Querystring error

  #7  
Oct 20th, 2004
Thanks for the help

tip
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:07 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC