|
Posted by Mike P2 on 05/09/07 23:42
On May 9, 7:20 pm, Mike P2 <sumguyovrt...@gmail.com> wrote:
> On May 9, 7:10 pm, DeanL <deanpmlonghu...@yahoo.com> wrote:
>
>
>
> > <html>
> > <head>
> > <title>Building a Form</title>
> > </head>
> > <body>
> > <?php
> > $search = $_GET["search"];
> > $self=$_SERVER['PHP_SELF'];
> > if ($search != NULL )
> > {
> > ('
> > <form action="'.$_SERVER["PHP_SELF"].'" method="GET">
> > <label>Search: <input type="text" name="search" />
> > </label>
> > <input type="submit" value="Go!:" />
>
> > </form>
> > ');}
>
> > ?>
> > </body>
> > </html>
>
> That's some pretty strange code. When was this book published?
>
> Whether or not that array index is set should be checked before use,
> try this instead:
>
> <html>
> <head>
> <title>Building a Form</title>
> </head>
> <body>
> <?php
>
> $self = '';
>
> if( isset( $_SERVER['PHP_SELF'] )
> {
> $self = $_SERVER['PHP_SELF'];
>
> }
>
> if( isset( $_GET["search"] ) )
> {
> echo "
> <form action="$self" method='get'>
> <label for='search'>Search:</label>
> <input type='text' name='search' id='search' />
> <input type='submit' value='Go!' />
> </form>";}
>
> ?>
> </body>
> </html>
>
> This will accomplish what it looks like the book is trying to do,
> however I don't see why you would do that. It will only display the
> search form if the form was already submitted. To show the form, you
> would need to type in whatever.php?search=yep or something instaed of
> just whatever.php. Perhaps the book means to /hide/ the form if it was
> submitted, in which case you can change this:
>
> if( isset( $_GET['search'] ) )
>
> To this:
>
> if( !isset( $_GET['search'] ) )
>
> Good luck,
> Mike PII
Oops, I forgot to close a parenthesis. Here it is again, even better
this time:
<html>
<head>
<title>Building a Form</title>
</head>
<body>
<?php
if( isset( $_GET['search'] ) )
{
$search = $_GET['search'];
$self = '';
if( isset( $_SERVER['PHP_SELF'] ) )
{
$self = $_SERVER['PHP_SELF'];
}
echo "
<form action='$self' method='get'>
<label for='search'>Search:</label>
<input type='text' name='search' id='search' value='$search' />
<input type='submit' value='Go!' />
</form>";
}
?>
</body>
</html>
Oh yea, and your error message from before was because PHP may or may
not report small errors, depending on the error reporting settings,
and referring to a variable that doesn't exist is considered bad, so
PHP will issue a notice if under the right conditions. That book may
be talking about an older version of PHP that just returned NULL and
ignored the issue, or is assuming that you are using PHP under a
lenient mode.
-Mike PII
Navigation:
[Reply to this message]
|