chronister Posted November 9, 2008 Share Posted November 9, 2008 I dig coding, but the one thing I don't like is creating forms and the validation for them. I have spent WAY TOO MUCH time just messing with forms and getting them to work and validate properly. Is there a form class that you folks here use, or some method of creating forms quicker?? I have looked at CloneFish form class, and it looks alright. Is there something else that you guys and gals reccommend?? Nate Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/ Share on other sites More sharing options...
Lodius2000 Posted November 9, 2008 Share Posted November 9, 2008 everybody is gonna hate me for just giving you code, but you have a thousand posts, you could do this if you really wanted to, it doenst help with validation, but man does it make forms a whole lot easier formhelpers.php <?php //print a text box function input_text($element_name, $values){ print '<input type="text" name="' . $element_name .'" value="'; print htmlentities($values[$element_name]) . '"/>'; } //print a title box, like text box but as wide as a text area function input_title($element_name, $values){ print '<input type="text" size="76" name="' . $element_name .'" value="'; print htmlentities($values[$element_name]) . '"/>'; } //print a password box function input_password($field_name, $values) { print '<input type="password" name="' . $field_name .'" value="'; print htmlentities($values[$field_name]) . '"/>'; } //print a submit button function input_submit($element_name, $label){ print '<input type="submit" name="' . $element_name .'" value="'; print htmlentities($label) . '"/>'; } //print a textarea function input_textarea($element_name, $values){ print '<textarea cols="75" rows="40" name="' . $element_name .'">'; print htmlentities($values[$element_name]) . '</textarea>'; } //print a radio button or checkbox function input_radiocheck($type, $element_name, $values, $element_value){ print '<input type="' . $type . '" name="' . $element_name . '" value="' . $element_value . '" '; if ($element_value == $values[$element_name]){ print ' checked="checked"'; } print '/>'; } //print a select menu function input_select($element_name, $selected, $options, $multiple = false){ //print out the <select> tag print '<select name="' . $element_name; // if multiple choices are permitted, add the multiple attribute // and add a [] to the end of the tag name if ($multiple){ print '[]" multiple="multiple'; } print '">'; //set up the list of things to be selected $selected_options = array(); if ($multiple) { foreach ($selected[$element_name] as $val){ $selected_options[$val] = true; } } else { $selected_options[ $selected[$element_name] ] = true; } //print out the <option> tags foreach ($options as $option=>$label) { print '<option value="' . htmlentities($option) . '"'; if ($selected_options[$option]){ print ' selected="selected"'; } print '>' . htmlentities($label) . '</option>'; } print '</select>'; } ?> Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686276 Share on other sites More sharing options...
cooldude832 Posted November 9, 2008 Share Posted November 9, 2008 if u know how to use OOP classes that load in via MySQL to build forms can be very powerful Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686277 Share on other sites More sharing options...
Yesideez Posted November 9, 2008 Share Posted November 9, 2008 Lodius2000, I would probably never have thought of anything like that - such a simple solution! I've just spent a few minutes cutting the code down a few lines, will add it to my classes library and give it a go tomorrow. Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686292 Share on other sites More sharing options...
Lodius2000 Posted November 9, 2008 Share Posted November 9, 2008 yesideez, cant take credit for that one, got that code verbatim from learning php 5 by david sklar, but man does it help Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686321 Share on other sites More sharing options...
chronister Posted November 10, 2008 Author Share Posted November 10, 2008 Lodius2000 - Thanks for that code. I have thought of something like that but hesitated as that part of it is not too terribly bad. The creation of the actual form is not a terribly tedious part, it is the processing of it and handling the errors properly and re-filling the form that is a bitch. I have a HUGE employment application form in the forseeable future for the company I work for. This is going to be a pain in the ass because it will be IN DEPTH validation... things like if field a is yes, then fields b, c, d have to be filled out if field a is no, skip fields b, c, d ...... etc.. But I may use something like the functions you gave to save some coding and time. cooldude832 - I have thought about a generated form that uses a database to store the fields and such.... But again the validation is not accounted for... I am not real good with the whole OOP thing yet, working on it, but got a ways to go. I may just break down soon and make my own form class. It will be a good OOP practice. Anyone familiar with CloneFish?? It looks pretty good minus the price. Thanks for the input here folks.... Nate Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686440 Share on other sites More sharing options...
cooldude832 Posted November 10, 2008 Share Posted November 10, 2008 Think of it this way MySQL tables 1) Forms (PK = FormID, list the name of form last edit etc.) 2) Fields (PK FieldID, FK FormID list the fields, labels, type, default value etc.) 3) Validate (PK ValidateID, FK FieldID, FormID Contains validation snippette for forms) so example is you build a system that lets you do madlib fill in with like 6 selects if (This Field affected by These parameters ) IS (> < == === !=) (This Value/Field) (Halt/Display/Halt Display) (This Error Message) So you can take a field and do a regex to it/strip tags/strlen etc and verify it this way. Or you could even add on to the cookie cutter a way to add a completely unique error checking that runs it against a table for example to verify its not a duplicate name. Once you have the class/functions set up For error checking its super easy as you error check you just keep in mind that idea I present of Halt//Warn/Warn and Halt and build a function called error_report in the class that looks like function error_report($error,$method){ switch $method{ case 0: #Halt only break; case 1: #Warn Only break; default: #Halt and warn } ?> very easy Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686477 Share on other sites More sharing options...
Lodius2000 Posted November 10, 2008 Share Posted November 10, 2008 chronister, look into that book i talked about earlier 'learning php 5' his method is really nice, makes functions for form processing... simple validation lines and all sorts of goodies. heres the main page logic for all my form pages, just put this at the top and then build all the functions <?php if($_POST['_submit_check']){ //$_POST['_submit_check'] is a hidden field, makes redisplay possible if($form_errors = validate_form()){ show_form($form_errors); } else { process_form(); } } else { show_form(); } function show_form($errors = '') { // make the form here //start with if ($errors){ print 'Please correct these errors: <ul><li>'; print implode('</li><li>', $errors); print '</li></ul>'; } } function validate_form(){ //contains simple validation like //check that username is entered if (trim(strlen($username)) == 0) { $errors[]= "You must enter a username."; } } function process_form(){ //what do you want to do with the entered data, whatever it is, do it here } ?> that make sense? Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686509 Share on other sites More sharing options...
haku Posted November 10, 2008 Share Posted November 10, 2008 I feel your pain - I hate validating inputs. I actually created a few different classes to try to find something to make it easier, and while it did become easier, I still didn't come up with anything that was perfect. However, a few months ago I started learning Drupal. Huge learning curve, but once you've got it, building forms is as easy as pie. You just declare an array to build the forms, and drupal has automatic validation built into it, with the ability to add extra validation not built into the core. Link to comment https://forums.phpfreaks.com/topic/132066-quicker-form-building/#findComment-686605 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.