$GET problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pstrg
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 9:36 am

$GET problem

Post by pstrg »

I'm stuck with something that may have a very simple solution but I have been unable to find it.

I start with an index.htm page which contains

Code: Select all

<head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=main.php?pg=home">
</head>
On main.php, in order to check whether the value for pg ('home') has been correctly passed, I have this code

Code: Select all

<head>
    <title>AAA<?php echo $_GET['pg'] ; ?>BBB</title>
</head>
Everything else is OK but as I check the title of the page, it's simply AAABBB thus meaning that echo returned an empty string.
Why is $_GET['pg'] failing to extract the value ('home') of parameter pg?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: $GET problem

Post by superdezign »

The GET array is populated via the query string. The Refresh HTTP header will redirect the browser, which should change the URL in your address bar. Does the URL have a query string?
pstrg
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 9:36 am

Re: $GET problem

Post by pstrg »

URL of the page (whose title should be AAAhomeBBB) is http://localhost/06a/[b]main.php?pg=home[/b]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: $GET problem

Post by superdezign »

print_r($_GET) on main.php and tell us what it shows. If it is empty but your query string is not, then you may have a server configuration problem or at some point prior to this, the $_GET array is emptied (which is impossible if you don't have any includes/requires).
pstrg
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 9:36 am

Re: $GET problem

Post by pstrg »

Firstly, thank you for the help.
I tried to simplify my initial question but it seems that there may be additional details I should have added.
I have an INCLUDE; not on main.php but on a page within one of its frames - main-left.php (however, this seems no to be the problem).
What I'm trying to do is to pass values through URLs so that main.php renders differently according to them.

index.htm is supposed simply to set the initial value of pg ('home').

Code: Select all

<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=main.php?pg=home">
</head>
main.php should render different frames depending on the value of pg

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<?php
    session_start() ;
?>
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>AAA<?php echo $_GET['pg'] ; ?>BBB</title>
</head>
 
<FRAMESET ROWS="*,2,500,*" BORDER="0" FRAMEBORDER="no" FRAMESPACING="0">    
         <FRAME SRC="top.htm" NAME="top" SCROLLING="no" NORESIZE MARGINWIDTH=0 MARGINHEIGHT=0>
         <FRAME SRC="h.htm" NAME="top" SCROLLING="no" NORESIZE MARGINWIDTH=0 MARGINHEIGHT=0>
             <FRAMESET COLS="758,2,*" BORDER="0" FRAMEBORDER="no" FRAMESPACING="0">
             <FRAME SRC="main-left.php?pg=<?php echo $_GET['pg'] ; ?>" NAME="main" SCROLLING="no" NORESIZE MARGINWIDTH=0 MARGINHEIGHT=0>             
                  <FRAME SRC="v.htm" NAME="main" SCROLLING="no" NORESIZE MARGINWIDTH=0 MARGINHEIGHT=0>
                  <FRAME SRC="main-right.php?pg=<?php echo $_GET['pg'] ; ?>" NAME="main" SCROLLING="no" NORESIZE MARGINWIDTH=0 MARGINHEIGHT=0>
             </FRAMESET>    
         <FRAME SRC="bottom.htm" NAME="bottom" SCROLLING="no" NORESIZE MARGINWIDTH=0 MARGINHEIGHT=0>
</FRAMESET>
 
<noframes>
</noframes>
</html>
main-left.php has on its left side a menu (which renders OK) and an image on the right.
The problem is that the image does not appear - just the string image (because of the ALT)

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<?php
session_start() ;
/* require('txt.php') ; */
$image['home']          = "img/ines558.gif" ;
$image['history']       = "img/ines.jpg" ;
$image['extra']         = "img/ARM-BEMF-1999.jpg" ;
$image['ensembles']     = "img/CMDH.jpg" ;
$image['teaching']      = "img/flautas_em_manuais.gif" ;
$image['music']         = "img/RehearsalGerman18thC.gif" ;
$image['contact']       = "img/suonatrice2.jpg" ;
?>
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="style.php" title="inesdavena_css" media="screen,projection" />
</head>
 
<body>
<div id="wrap">
    <div id="leftside">
    <img src="img/ines_logo220-C0.gif" alt="Inês" width="220" height="124" border="2" align="top"/>
        <div id="menu">
            <?php if ($pg == "history") echo $menu[$pg][1]; else echo $menu['history'][0]; ?>
            <?php if ($pg == "extra") echo $menu[$pg][1]; else echo $menu['extra'][0]; ?>
            <?php if ($pg == "ensembles") echo $menu[$pg][1]; else echo $menu['ensembles'][0]; ?>
            <?php if ($pg == "teaching") echo $menu[$pg][1]; else echo $menu['teaching'][0]; ?>
            <?php if ($pg == "music") echo $menu[$pg][1]; else echo $menu['music'][0]; ?>
            <?php if ($pg == "contact") echo $menu[$pg][1]; else echo $menu['contact'][0]; ?>
            <?php if ($pg == "home"); else echo $menu['home'][0]; ?>
        </div>
    </div>
    <div id=image-main>
        <?php echo $_GET['pg']; ?>
        <img src="<?php echo $image[$pg] ; ?>" alt="legend"/>
        <!-- <img src="img/ines558.gif" alt="<?php echo $pg ; ?>aaa"/> -->
    </div>
</div>
</body>
</html>
 
Note that I outcommented the INCLUDE and set all values of $image[] for testing but nothing happened.

The include is used to fill different values of $image[]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: $GET problem

Post by califdon »

I don't see anything obviously wrong with what you're doing, but as a way to figure out what's happening, I'd suggest that at the top of main.php you assign the value of the $_GET parameter to a variable (always a good thing to do!) and then echo it out to see what you're getting.

Code: Select all

<?php
     session_start();
     $pg=isset($_GET['pg']) ? $_GET['pg'] : "NO GET VARIABLE!";
     echo $pg;       // debug
?>
pstrg
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 9:36 am

Re: $GET problem

Post by pstrg »

With your code on main.php I just get a white page with an error message:

NO GET VARIABLE! PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at L:\inesdavena.com\06a\main.php:3) in L:\inesdavena.com\06a\main.php on line 4 PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at L:\inesdavena.com\06a\main.php:3) in L:\inesdavena.com\06a\main.php on line 4 PHP Notice: Undefined index: pg in L:\inesdavena.com\06a\main.php on line 11 PHP Notice: Undefined index: pg in L:\inesdavena.com\06a\main.php on line 18 PHP Notice: Undefined index: pg in L:\inesdavena.com\06a\main.php on line 20

Edited: Had mistakenly described the effect of the suggested debug code on main-left.php instead
Last edited by pstrg on Sun Sep 13, 2009 3:17 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: $GET problem

Post by califdon »

OK, that shows that you're not getting a $_GET variable at all. It sounds like superdezign's comment about your server configuration is a suspect.
pstrg
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 9:36 am

Re: $GET problem

Post by pstrg »

I'm testing locally with PHP 5.2.10
This means, I think, that server configuration is defined by php.ini at the same folder as php.exe; I left it as installed.
Used both Abyss Web Server X1 and BRS Webweaver - same results.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: $GET problem

Post by peterjwest »

Have you tried this on a non-frames page? e.g.

Code: Select all

<html><head><title>AAA<?php echo $_GET['pg'] ; ?>BBB</title></head></html>
Probably a dumb idea, but frames do weird things.
Why are you using frames anyway?
pstrg
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 9:36 am

Re: $GET problem

Post by pstrg »

@peterjwest
A bit off topic :wink: but be welcome - I want to have:
- vertically centered, a region with a fixed number of pixels;
- separating the left and right part of the center region a vertical color strip;
- on its top and bottom a colored strip;
- the top and bottom of the page should vary in size and contain only color.
I have been unable to make things work (strips changed places) with both Firefox and IE8 but noted that if the strips were frames (color is background) things went OK.
Frames are not recommended, I've been reading, but simplified everything.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: $GET problem

Post by califdon »

pstrg wrote:With your code on main.php I just get a white page with an error message:

NO GET VARIABLE! PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at L:\inesdavena.com\06a\main.php:3) in L:\inesdavena.com\06a\main.php on line 4 PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at L:\inesdavena.com\06a\main.php:3) in L:\inesdavena.com\06a\main.php on line 4 PHP Notice: Undefined index: pg in L:\inesdavena.com\06a\main.php on line 11 PHP Notice: Undefined index: pg in L:\inesdavena.com\06a\main.php on line 18 PHP Notice: Undefined index: pg in L:\inesdavena.com\06a\main.php on line 20

Edited: Had mistakenly described the effect of the suggested debug code on main-left.php instead
Since you are receiving PHP warnings and notices, it's hard to tell what may be happening to the usual PHP parsing. I'd recommend you forget about your pg parameter until you resolve those issues.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: $GET problem

Post by califdon »

pstrg wrote:Frames are not recommended, I've been reading, but simplified everything.
Until browsers and servers discontinue supporting frames, which will eventually happen. That's the reason to not use them.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: $GET problem

Post by peterjwest »

You might be able to find a suitable css/javascript solution online instead of frames.

That error you're getting is because you've started the session after outputting to the browser, the http headers are sent with the first browser output. You need to start the session before any output e.g.

Code: Select all

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
pstrg
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 9:36 am

Re: $GET problem

Post by pstrg »

peterjwest wrote:That error you're getting is because you've started the session after outputting to the browser, the http headers are sent with the first browser output. You need to start the session before any output
It's finally working; made my weekend!

Will look into replacing frames...
Post Reply