astralaaron Posted May 17, 2007 Share Posted May 17, 2007 how can I check if input fields are blank, and if they are stop the code on the page from executing?okay well i figured out if ($username == null) {echo "null";} actually I am not sure if the above code actually works.. i thought it did at first but I filled in the fields and it still said nullwhat is the code to kill all the code execution below that? Link to comment Share on other sites More sharing options...
zppblood Posted May 17, 2007 Share Posted May 17, 2007 I use empty() and isset to check fields, and use exit or die() to stop execution. Link to comment Share on other sites More sharing options...
astralaaron Posted May 17, 2007 Author Share Posted May 17, 2007 I use empty() and isset to check fields, and use exit or die() to stop execution.how do you write that?like this? if (empty($user_name)) {exit();} Link to comment Share on other sites More sharing options...
astralaaron Posted May 17, 2007 Author Share Posted May 17, 2007 also can you check if multiple fields are empty?how would you write that? Link to comment Share on other sites More sharing options...
astralaaron Posted May 17, 2007 Author Share Posted May 17, 2007 I put this, it seemed to work..but then when I fill the username field it still exits the code below if (empty($user_name)){exit('(*) Required fields');} else if (!preg_match('{[0-9a-zA-Z]}', $user_name)) {echo "Username may only contain letters and numbers";} else if ($user_pw != $user_pw2) {echo "Password fields did non match";} else if ($user_email != $user_email2) {echo "Email fields did not match";} else if ($user_check > 0) {echo "Username already exists";} else if ($email_check > 0) {echo "Email address is being used already";} Link to comment Share on other sites More sharing options...
astralaaron Posted May 17, 2007 Author Share Posted May 17, 2007 this won't even workif (empty($user_name)){echo "null username field";} else {echo "not null";}when it is empty it will echo "null username field"but when I fill in the username field it says the same message.. it will not execute the else Link to comment Share on other sites More sharing options...
zppblood Posted May 17, 2007 Share Posted May 17, 2007 That looks right to me, check to see if you have $user_name set to something like $_POST or $_GET. Link to comment Share on other sites More sharing options...
astralaaron Posted May 17, 2007 Author Share Posted May 17, 2007 That looks right to me, check to see if you have $user_name set to something like $_POST or $_GET.$username = $_POST['username']i am getting it like that. Link to comment Share on other sites More sharing options...
astralaaron Posted May 17, 2007 Author Share Posted May 17, 2007 oh wow.. it was suposed to be $username = $_POST['user_name']haha...it works like this even if (!$username) {echo "null";} else { echo "not null";I have 1 more question.. how can I check for multiple variables?nevermind it works like this if (!$user_name or !$user_pw) {echo "null";} else { echo "not null"; Link to comment Share on other sites More sharing options...
zppblood Posted May 17, 2007 Share Posted May 17, 2007 EDIT: Problems Fixed. Link to comment Share on other sites More sharing options...
Anders Moen Posted May 17, 2007 Share Posted May 17, 2007 empty() is easy to use.Just put it under the ...variables like this: $var1 = $_POST['input_one'];$var2 = $_POST['input_two'];if (empty($var1) || empty($var2)) {echo 'Please fill out all fields!';die('');} I think you got it above, but I don't have time to read the whole thread now (Norway's birthday and it's boring ) Link to comment Share on other sites More sharing options...
reportingsjr Posted May 17, 2007 Share Posted May 17, 2007 You may also want to use trim() to trim off the whitespace on the right and left sides. Just to make sure they dont put in a bunch of spaces. Link to comment Share on other sites More sharing options...
Anders Moen Posted May 18, 2007 Share Posted May 18, 2007 But if you use trim() and a person has used 2 spaces after eachother, will both be deleted or just one? Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted May 20, 2007 Share Posted May 20, 2007 All leading and/or trailing whitespaces will be removed. Ex: $str1 = ' abcdef ';$str2 = "\t qwerty\n\t ";echo '_' . trim( $str1 ) . "_\n";echo '_' . trim( $str2 ) . '_';---- WILL OUTPUT: -----_abcdef__qwerty_ Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.