witt Posted June 13, 2006 Share Posted June 13, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/11824-get-and-isset/ Share on other sites More sharing options...
toplay Posted June 13, 2006 Share Posted June 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44796 Share on other sites More sharing options...
kenrbnsn Posted June 13, 2006 Share Posted June 13, 2006 The empty() function doesn't always work when deciding whether a field is filled in.I've been using something like this:[code]<?phpif (strlen(trim(stripslashes($fld))) == 0) echo 'Field hasn't been filled in'; ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44844 Share on other sites More sharing options...
d_barszczak Posted June 13, 2006 Share Posted June 13, 2006 [!--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]<?phpif (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. Quote Link to comment https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44879 Share on other sites More sharing options...
joquius Posted June 13, 2006 Share Posted June 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44899 Share on other sites More sharing options...
wildteen88 Posted June 13, 2006 Share Posted June 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/11824-get-and-isset/#findComment-44974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.