Jump to content


Photo

GET and isset


  • Please log in to reply
5 replies to this topic

#1 witt

witt
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 13 June 2006 - 12:08 AM

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?

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 13 June 2006 - 12:15 AM

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]

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 June 2006 - 04:09 AM

The empty() function doesn't always work when deciding whether a field is filled in.

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

Ken

#4 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 13 June 2006 - 06:36 AM

[!--quoteo(post=383086:date=Jun 13 2006, 05:09 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 13 2006, 05:09 AM) View Post[/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:
<?php
if (strlen(trim(stripslashes($fld))) == 0) echo 'Field hasn't been filled in'; ?>

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.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#5 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 13 June 2006 - 07:48 AM

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

z..z..z..z..z..z..z..z..

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 June 2006 - 11:55 AM

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
if (isset($_GET['title']) && (strlen($_GET['title']) >= 4))
{
    $title = $_GET['title'];
    echo "the title is $title";
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users