Jump to content

Archived

This topic is now archived and is closed to further replies.

witt

GET and isset

Recommended Posts

If I have a script like this

<form action="search_results.php" method="get">
<p><b>Title:</b> <input type="text" name="title" size="30" maxlength="60" /></p>
<input type="submit" name="submit" value="Submit" />

to submit a form and this

if (isset($_GET['title'])) {
$title = $_GET['title'];
echo "the title is $title";
}

to show the result, why does isset($_GET['title']) always evaluate to true even if the textbox is left blank?

Share this post


Link to post
Share on other sites
Read up why here:
[a href=\"http://us2.php.net/manual/en/function.isset.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.isset.php[/a]

You probably want to look into using empty():
[a href=\"http://us2.php.net/manual/en/function.empty.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.empty.php[/a]

Share this post


Link to post
Share on other sites
The empty() function doesn't always work when deciding whether a field is filled in.

I've been using something like this:
[code]<?php
if (strlen(trim(stripslashes($fld))) == 0) echo 'Field hasn't been filled in'; ?>[/code]

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=383086:date=Jun 13 2006, 05:09 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 13 2006, 05:09 AM) [snapback]383086[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The empty() function doesn't always work when deciding whether a field is filled in.

I've been using something like this:
[code]<?php
if (strlen(trim(stripslashes($fld))) == 0) echo 'Field hasn't been filled in'; ?>[/code]

Ken
[/quote]

As far as i am aware if you have a field and submit the form with the field black the variable is still set (just with no data in it) which is why your result page always comes back as true.

Try:

if ($_GET['title'] <> NULL )) {
$title = $_GET['title'];
echo "the title is $title";
}

This checks to see is any data exists in the variable not if the variable exists.

Share this post


Link to post
Share on other sites
Form data is always posted (as blank if empty) so you're better off doing this:
[code]
if (isset ($_GET['submit']))
{
  foreach ($_GET as key => $value)
  {
    if ($key != "submit" && $value == NULL)
    {
      $error = "fields missing";
      break;
    }
  }
}
[/code]

Share this post


Link to post
Share on other sites
isset doesn't check whether the form field is filled in. It checks whether the variable iexits, ie it checks whether $_GET['title'] exists.

If you want to check the contents of the variable you'll want to use empty but empty will still return true if there is a space in the form field. If you want to check that they have fillled in the form field correctly, such as they have entered at least 4 or more characters into the form field you'll want do this
[code]if (isset($_GET['title']) && (strlen($_GET['title']) >= 4))
{
    $title = $_GET['title'];
    echo "the title is $title";
}[/code]

Share this post


Link to post
Share on other sites

×

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.