Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/11824-get-and-isset/
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]
Link to comment
https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44796
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.
Link to comment
https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44879
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]
Link to comment
https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44899
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]
Link to comment
https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44974
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.