amylou Posted October 25, 2007 Share Posted October 25, 2007 is there a simple code that will check to see if users have filled in required fields on a form? Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/ Share on other sites More sharing options...
teng84 Posted October 25, 2007 Share Posted October 25, 2007 no !!!! you can put all your required field in an array then loop it to see it it is set eg... //note this should be the name of the fields $required = array('chkname','dropage','teng',etc...) then loop your array foreach($required as $val){ if (isset($_POST[$val])){ ++$count; $missing .=$val; } } echo $count. $missing;// Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-378281 Share on other sites More sharing options...
PHP_PhREEEk Posted October 26, 2007 Share Posted October 26, 2007 PHP is a server-side script, which means that to check the validity of any input, the whole form must be sent in to be processed. Another part of the script (or a different script) then checks all of the input data. If an error exists, the form must be sent back to the client for update. There are several techniques for doing this cleanly. Javascript is a client-side script, which means it can check form input on-the-fly while it's in the browser. It can generate errors in real-time and refuse to send the script to the server until it's satisfied. So you need to choose which way you want to go here... there are TONS of Javascript routines that you can download and play with. If you need help with using PHP to check your forms, let us know! BTW, you can also use AJAX to accomplish this, but that might be a bit advanced for your level right now. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-378400 Share on other sites More sharing options...
amylou Posted October 26, 2007 Author Share Posted October 26, 2007 i would like to use php as i do not know how to do java script and the code is written in php thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-378510 Share on other sites More sharing options...
GingerRobot Posted October 26, 2007 Share Posted October 26, 2007 That rather depends on what validation you wish to do. If you are simply wanting to check if anything has been put into the fields, then using the empty() function would suffice. As was suggested, you could place all of the post fields into an array, and loop through, checking each in turn. There are plenty of other validation techniques you might want to use. Perhaps you want to check the length of certain fields? Try the strlen() function. Perhaps you want to check the form of certain strings (email addresses for example). If so, you'll need to use regular expressions. There's a forum on this site for those, so take a look there. The point im trying to make is that there's no set rules for this. It all depends on the specific situation - you'll need different validation rules for different things. Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-378513 Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 Use javascript so it does not have to post back, or AJAX as mentioned above. Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-378611 Share on other sites More sharing options...
PHP_PhREEEk Posted October 26, 2007 Share Posted October 26, 2007 The first thing to do is the HTML for the form, so that you can capture the data to begin with. You can't process anything in PHP until it exists and is sent in for processing. If you are not thoroughly versed in HTML forms, you might want to become familiar with a 'help' site and use their examples to guide you. Point is, there are form properties that you can set that will allow you to control the data a little bit up front, before it is processed. You need to make decisions here... For example, if any data is completely predictable or stringent, we would want to offer those choices in a drop-down or radio button configuration versus a standard input box where they can type in anything. Allowing the choice of two or more options in the same field also dictates a certain direction to go. Creating the Form is in fact an artform unto itself, and should be considered 100% independent of the processing stage. So let's get you started there... get your form 100% setup. Post any questions or code here. Having the form properly setup up front will save you processing time when it comes time to check the data. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-378637 Share on other sites More sharing options...
amylou Posted October 27, 2007 Author Share Posted October 27, 2007 i have the form and the code for the form all done and it works, i just need the validation so that the user doesn't send an empty form. Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379060 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 example: <?php if($_POST['fname']=="") { echo " Please Fill in a first name";}?> Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379062 Share on other sites More sharing options...
PHP_PhREEEk Posted October 27, 2007 Share Posted October 27, 2007 Ok, please post the form HTML, or if you prefer one-on-one help with a little less side-chatter, feel free to PM me. If you post it in this thread, please tell us how you want each field validated, or if you just simply want to verify that something has been put into each field (not very safe, of course...). PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379080 Share on other sites More sharing options...
phpSensei Posted October 27, 2007 Share Posted October 27, 2007 There is no complication here, and most of you make it seem like he wants this done with AJAX. one of the methods that were probably mentioned before is this: <?php if(empty($_POST['field'])){ //run message } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379081 Share on other sites More sharing options...
teng84 Posted October 27, 2007 Share Posted October 27, 2007 i would like to use php as i do not know how to do java script and the code is written in php thank you for your help i guess this is solved Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379083 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 ^ what he said Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379086 Share on other sites More sharing options...
PHP_PhREEEk Posted October 27, 2007 Share Posted October 27, 2007 Thanks for marking this as 'solved' for the original poster, but there are other things to be considered, such as how she wants errors handled, and how the form should go back to the client (with correct form fields pre-filled in, or starting from scratch). There are details... (assuming she wants a nice form and not a hack). I'll wait to hear from the OP. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379093 Share on other sites More sharing options...
teng84 Posted October 27, 2007 Share Posted October 27, 2007 is there a simple code that will check to see if users have filled in required fields on a form? Thanks for marking this as 'solved' for the original poster, but there are other things to be considered, such as how she wants errors handled, and how the form should go back to the client (with correct form fields pre-filled in, or starting from scratch). There are details... (assuming she wants a nice form and not a hack). I'll wait to hear from the OP. PhREEEk read the tread it sould be another topic Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379095 Share on other sites More sharing options...
PHP_PhREEEk Posted October 27, 2007 Share Posted October 27, 2007 is there a simple code that will check to see if users have filled in required fields on a form? Thanks for marking this as 'solved' for the original poster, but there are other things to be considered, such as how she wants errors handled, and how the form should go back to the client (with correct form fields pre-filled in, or starting from scratch). There are details... (assuming she wants a nice form and not a hack). I'll wait to hear from the OP. PhREEEk read the tread it sould be another topic I can read fine, thanks. I was offering extended help, and also offered for the OP to Private Message me directly if she wanted further help. YOU should learn to read better. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74812-solved-validation/#findComment-379098 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.