dmccabe Posted June 6, 2008 Share Posted June 6, 2008 I have a form on which some fields are mandatory. How can I make it so that when the Submit button is pressed, the mandatory fields are checked before the page reloads, so that the user does not have to fill in all the details. I have at the moment got it so they press the button then the page check $_POST['blah'] for a value if not displays an error, but then the user has to click back and the form is blanked out again. I have seen some forms, where it pops up a message box the instant the submit button is clicked, saying you must fill in XYZ. Hope this makes sense, if so please help. Link to comment https://forums.phpfreaks.com/topic/108999-checking-fields-have-been-filled-on-submit/ Share on other sites More sharing options...
RMcLeod Posted June 6, 2008 Share Posted June 6, 2008 One way is to have the form submit to itself. Your PHP checks to see if the form has been submitted, if not it displays the form. If it has been submitted then it does error checking, if there are any errors the form is displayed with an error message and the form values are filled out with the $_POST information. If everything is fine then the script does whatever instead. Link to comment https://forums.phpfreaks.com/topic/108999-checking-fields-have-been-filled-on-submit/#findComment-559169 Share on other sites More sharing options...
MichaelMackey Posted June 6, 2008 Share Posted June 6, 2008 I started work on some PHP stuff recently and most of what I've been doing is validation. The two ways that seem the best to get this done are to either keep on with the post system you currently use, then just refill the boxes when the validation issues occur, or you can use javascript to validate the page (not good for data that needs to be secure). The nice thing about javascript is it is very efficient and causes no page reload on generic validation items, you can use the basic alert popups for this to test it but I would suggest creating your own inscreen popups to deal with it later (which is a bit more than I can explain right now). Link to comment https://forums.phpfreaks.com/topic/108999-checking-fields-have-been-filled-on-submit/#findComment-559178 Share on other sites More sharing options...
aseaofflames Posted June 6, 2008 Share Posted June 6, 2008 an easy way to do this would be with javascript, <head> code: <script> <!-- function checkscript() { var r; //return value var a = "The following fields have not been completed:\r\n"; //alert text for(i=0;i<document.forms[0].elements.length;i++) { if(document.forms[0].elements[i].value=="") { r = 0; a += "-" + document.forms[0].elements[i].title + "\r\n"; document.forms[0].elements[i].style.backgroundColor="#FF0000"; } else { document.forms[0].elements[i].style.backgroundColor=""; } } if(r==0) { alert(a); } else { document.forms[0].submit(); } } --> </script> then change your submit button to and move it after the </form> <button name="send" value="Save Data" onclick="checkscript();">Save Data</button> also, make sure all <input's have a title attribut (title="Your Title") this code needs to be altered if you want it to work for checkboxes and radios however example: http://backup.aseaofflames.com/formexample.html Link to comment https://forums.phpfreaks.com/topic/108999-checking-fields-have-been-filled-on-submit/#findComment-559368 Share on other sites More sharing options...
JD* Posted June 6, 2008 Share Posted June 6, 2008 I have a PHP function that is half built that does this. What happens is when a user submits the form, in the processing logic it runs a check_required("field_name_1, field_name2, field_name3"). check_required sees if the $_POST['field_name'] is empty or not, and if it is, puts it into a session called "required" and returns 1; Back on the form logic, if check_required returns 1, it redirects them to the same page. Each of these pages has a function called display_error() that checks for the session['error'] and prints out a message and explodes the "required" session. One last function called "fill_in_field" will grab fields that are already filled and, when reloading the form, fills them back in. It's not as easy to use as javascript, but it's a lot more reliable for multiple browsers. If you're interested, I'll post the source. Link to comment https://forums.phpfreaks.com/topic/108999-checking-fields-have-been-filled-on-submit/#findComment-559371 Share on other sites More sharing options...
MatthewJ Posted June 6, 2008 Share Posted June 6, 2008 To be thorough, I test client side with javascript first to make the errors a bit "friendlier" and then still check with php once submitted... Yes, it may be overkill but users with javascript turned off bypass the client side validation. Link to comment https://forums.phpfreaks.com/topic/108999-checking-fields-have-been-filled-on-submit/#findComment-559375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.