wright67uk Posted December 22, 2012 Share Posted December 22, 2012 I'm trying to process a form. I want the message: "You haven't filled out all of the fields" to appear when 1 or more fields have been left empty when the user submits the form. I want the form to be processed and for the message: "A scorecard for $location, has now been added. You can use the form again to enter a new card." to be displayed. Ive tried to piece the code below together. The problem I have, is that even If NO fields are left blank, "You haven't filled out all of the fields" is displayed. Would anyone be kind enough to help me? <?php if(isset($_POST['processForm'])) { foreach ($_POST as $value) { if ( $value = " ") { echo "You haven't filled out all of the fields"; exit(); } } $user_id = 7; # PASSWORD BLOCK # $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR); @mysql_select_db($database_connect) or die (mysql_error()); $sql = "INSERT INTO snag_score_cards (user_id, location, par1, par2, par3, par4, par5, par6, par7, par8, par9) VALUES ('$user_id', '$_POST[location]', '$_POST[par1]', '$_POST[par2]', '$_POST[par3]', '$_POST[par4]', '$_POST[par5]', '$_POST[par6]', '$_POST[par7]', '$_POST[par8]', '$_POST[par9]')"; mysql_query($sql); echo $location = $_POST['location']; echo "<br/> A scorecard for $location, has now been added <br/> You can use the form again to enter a new card."; }; ?> <p>Enter your location</p> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="hidden" name="processForm" value="1" /> <input autocomplete="off" name="location" id="location" size="18" value=""/><br /><br /> <input type="text" autocomplete="off" name="Scores" id="Score" value="Score" readonly style="border:0px" /> <input type="text" autocomplete="off" name="Par1" id="Par" value=" Par" readonly style="border:0px" /> <input name="Par Score" id="ParScore" value=" Par Score" readonly style="border:0px" /><br> <input type="text" autocomplete="off" name="sum1" id="hole1A" readonly value="" /> <input type="text" autocomplete="off" name="par1" value="" id="hole1B" /> <input type="text" name="sum" value="" id="hole1result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole2A" readonly value="" /> <input type="text" autocomplete="off" name="par2" value="" id="hole2B" /> <input type="text" name="sum2T" value="" id="hole2result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole3A" readonly value="" /> <input type="text" autocomplete="off" name="par3" value="" id="hole3B" /> <input type="text" name="sum3" value="" id="hole3result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole4A" readonly value="" /> <input type="text" autocomplete="off" name="par4" value="" id="hole4B" /> <input type="text" name="sum4" value="" id="hole4result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole5A" readonly value="" /> <input type="text" autocomplete="off" name="par5" value="" id="hole5B" /> <input type="text" name="sum5" value="" id="hole5result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole6A" readonly value="" /> <input type="text" autocomplete="off" name="par6" value="" id="hole6B" /> <input type="text" name="sum6" value="" id="hole6result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole7A" readonly value="" /> <input type="text" autocomplete="off" name="par7" value="" id="hole7B" /> <input type="text" name="sum7" value="" id="hole7result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole8A" readonly value="" /> <input type="text" autocomplete="off" name="par8" value="" id="hole8B" /> <input type="text" name="sum8" value="" id="hole8result" readonly style=""> <br> <input type="text" autocomplete="off" name="sum1" id="hole9A" readonly value="" /> <input type="text" autocomplete="off" name="par9" value="" id="hole9B" /> <input type="text" name="sum9" value="" id="hole9result" readonly style=""> <br> <input type="text" name="" id="" value="" readonly style="border:0px" /> <input type="text" name="" id="" value="" readonly style="border:0px" /> <input type="submit" name="submit" value="Create"/> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/ Share on other sites More sharing options...
Barand Posted December 22, 2012 Share Posted December 22, 2012 = is an assignment == is a comparison Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400927 Share on other sites More sharing options...
wright67uk Posted December 22, 2012 Author Share Posted December 22, 2012 Oops... I have changed the code to reflect this, however now I get the opposite... the "You haven't filled out all of the fields" message doesn't display at all . Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400931 Share on other sites More sharing options...
Barand Posted December 22, 2012 Share Posted December 22, 2012 Maybe because you test for a space instead of empty field Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400932 Share on other sites More sharing options...
wright67uk Posted December 22, 2012 Author Share Posted December 22, 2012 Thanks for the reply... So maybe I should be using something like: if (isset($value)) [code/] ? this still gives me the same problem :-( Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400933 Share on other sites More sharing options...
DavidAM Posted December 22, 2012 Share Posted December 22, 2012 INPUT text fields will always be set in the POST array. You can use empty. However, if they put a space in the field, the field will not be empty. So, you have to trim the data and then test for empty. But you can't use if (empty(trim($value))) (unless you are using PHP 5.5 or later) because empty() is a construct not a function. You can use: foreach ($_POST as $value) { $test = trim($value); if (empty($test)) { echo "You haven't filled out all of the fields"; exit(); } } Note that this does not solve some other problems. 1) It is trivial for a user to modify a form to submit fewer or different field names than what you expect. Which would leave you with missing data. It is always better to specifically check to see if the fields you expect are set and have valid values. 2) If the user misses just one field, your design will leave them with a blank page. They will have to go back to the form and re-enter ALL of the information again. It is usually better to use a "sticky form". That is, if the fields are not valid or are missing, re-display the form with the data they did provide already filled in so they only have to enter the missing/invalid data. Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400935 Share on other sites More sharing options...
wright67uk Posted December 22, 2012 Author Share Posted December 22, 2012 Hi, and thank you for your reply! I see what your saying about the modifying options, however this is for a scorecard where the user MUST input all fields. I'm still getting the same issue of only having the You haven't filled out all of the field message display wether or not the fields are empty or filled in. Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400936 Share on other sites More sharing options...
DavidAM Posted December 22, 2012 Share Posted December 22, 2012 <input type="text" name="sum" value="" id="hole1result" readonly style=""> This field is empty, and it is readonly, so the user can not put anything in it. Since you are using the loop to check all fields on the form, this field will trigger the message you are getting. Also, the suggestion in my previous post might not work if any of the fields might contain the number zero. The empty() construct considers a zero to be "empty". It might be better to check if trim($value) == "". However, this still leaves the empty readonly fields in your form. This is another reason for checking each field specifically rather than just testing all "submitted" fields. If these fields are populated by some Javascript function, it may be failing. Try printing the $_POST array to see what you are receiving when the form is submitted: if(isset($_POST['processForm'])) { printf('<PRE>%s</PRE>', htmlspecialchars(print_r($_POST, true))); die(); You might also post your code as modified so we can see what it looks like, now. Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400942 Share on other sites More sharing options...
wright67uk Posted December 23, 2012 Author Share Posted December 23, 2012 Oh yes, I didn't even think about the non input fields! As you said I will validate the fields one by one. Just out of interest... If I had 100 fields and 1 of which was a readonly, what would be the best thing to do then? Ps. Thanks for your help. Array ( [processForm] => 1 [location] => steg [scores] => Score [Par1] => Par [Par_Score] => Par Score [sum1] => [par1] => 2 [sum] => [par2] => 5 [sum2T] => [par3] => 2 [sum3] => [par4] => 2 [sum4] => [par5] => 1 [sum5] => [par6] => 3 [sum6] => [par7] => 4 sum7] => [par8] => 2 [sum8] => [par9] => 1 [sum9] => [submit] => Create Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1400969 Share on other sites More sharing options...
DavidAM Posted December 23, 2012 Share Posted December 23, 2012 There are various ways to work with that, depending on the application. For instance, in this particular case, I would be tempted to use an array for the fields: <input type="text" autocomplete="off" name="par[2]" value="" id="hole2B" /> Then you can check each hole in a for loop. for ($hole = 1; $hole <= 9; $hole++) { if (trim($_POST['par'][$hole]) == '') { // An error, this value is required } } If the fields were vastly different, say like a user profile page, I might check for required fields like this: // Create an array of the required field names $required = array('DisplayName', 'EmailAddress', 'IQ', 'Gender'); // Create an empty array to hold the error messages $errors = array(); foreach ($required as $field) { $test = (isset($_POST[$field]) ? trim($_POST[$field]) : null); if (empty($test)) $error[] = $field . ' is a required field'; } Or something along those lines. Generally, the validation is different enough for each field that I just validate one field at a time. Quote Link to comment https://forums.phpfreaks.com/topic/272296-if-statements-when-processing-a-form/#findComment-1401043 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.