kreut Posted February 21, 2011 Share Posted February 21, 2011 Hello! I was wondering if I could have some help with some form validation. In my insert script, I have: <label for="text"><br /> <br /> Question Text:</label> <textarea name="text" id="text" cols="45" rows="5"> </textarea> Then in a separate script, I input the data into a database: $data = array('text' => $_POST['text'] 'question_type' => $_POST['question_type'], 'solution' => $_POST['solution'], 'author_id' => $_POST['author'], 'public_access' => $_POST['public_access']); In this database script, I'd like to check if the text field was "posted": if it's empty I'd then like to print "Required field" on my original script. First, if there's nothing to be posted from the text field, I want to stop the database script and return before entering a blank field into my database. Then, it would be great if I could print a "Required field" message in the original script. Thanks in advance for your help! Quote Link to comment https://forums.phpfreaks.com/topic/228336-form-validation/ Share on other sites More sharing options...
bbram Posted February 21, 2011 Share Posted February 21, 2011 Can't you do it like this if (isset$_post['textboxname'] { do something } else { redirect back to main page } Quote Link to comment https://forums.phpfreaks.com/topic/228336-form-validation/#findComment-1177404 Share on other sites More sharing options...
kreut Posted February 21, 2011 Author Share Posted February 21, 2011 I could....but truth be told, I need to check a couple of different fields. Is there a more "general" approach? In other words, if I'm checking for question text, question type, and solution, generate some sort of errors array to print out? Quote Link to comment https://forums.phpfreaks.com/topic/228336-form-validation/#findComment-1177413 Share on other sites More sharing options...
Pikachu2000 Posted February 21, 2011 Share Posted February 21, 2011 Here's an example I keep handy to demonstrate how to store validation errors in an array, and redisplay the form to the user. Paste it into a file an mess around with it so you can get familiar with the logic. <?php if( $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted $errors = array(); // initialize an array to hold validation errors $_POST = array_map('trim', $_POST); // trim all $_POST array values if( !empty($_POST['name']) ) { // validate the name field if( !ctype_alpha($_POST['name']) ) { $errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error } if( strlen($_POST['name']) < 3 || strlen($_POST['name'] > 20) ) { $errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error } } else { $errors[] = 'Name is a required field.'; // if name is empty, store error } if( !empty($_POST['number']) ) { // same validations as in name, above. if( !ctype_digit($_POST['number']) ) { $errors[] = 'Number must be numeric.'; } if( strlen($_POST['number']) < 5 || strlen($_POST['number']) > 20 ) { $errors[] = 'Number must be from 5 to 20 digits. It is currently ' . strlen($_POST['number']) . ' digits'; } } else { $errors[] = 'Number is a required field.'; } if( !empty($errors) ) { // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form echo "<font color=\"red\">The following errors were detected<br>"; echo implode('<br>', $errors); echo '</font>'; } } ?> <form method="post"> Name (3-20 letters): <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>"><br> Number (5-10 numbers): <input type="text" name="number" value="<?php echo isset($_POST['number']) ? $_POST['number'] : ''; ?>"><br> <input type="hidden" name="submitted" value="yes"> <input type="submit" name="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/228336-form-validation/#findComment-1177424 Share on other sites More sharing options...
cssfreakie Posted February 21, 2011 Share Posted February 21, 2011 oh wow! first time i see array map that's an awesome function, thanks pikachu for the above Quote Link to comment https://forums.phpfreaks.com/topic/228336-form-validation/#findComment-1177458 Share on other sites More sharing options...
kreut Posted February 21, 2011 Author Share Posted February 21, 2011 Thanks so much for such a detailed reply! Quote Link to comment https://forums.phpfreaks.com/topic/228336-form-validation/#findComment-1177595 Share on other sites More sharing options...
d3rck Posted February 21, 2011 Share Posted February 21, 2011 very very nice. I copied it for learning purposes. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/228336-form-validation/#findComment-1177635 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.