Jump to content

Form Validation


kreut

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/228336-form-validation/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/228336-form-validation/#findComment-1177424
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.