Jump to content

Form Validation


j05hr

Recommended Posts

Opps sorry missed removing that.

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

   // Minimal form validation:   
   if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
   && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

      function validateEmailAddr($email){
         return preg_match('/[^\x00-\x20()<>@,;:\\".
         [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)+/i', $email);
      }

      if(!validateEmailAddr($email)){
            echo '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>';
      }

      // Create the body:
      $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
      \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

      // Make it no longer than 70 characters long:
      $body = wordwrap($body, 70);

      // Send the email:
      mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

      // Print a message:
      $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

      // Clear $_POST (so that the form's not sticky):
      $_POST = array();

   } else {
      $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
   }
} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

ok i know what you need but i cant think of how to do it let me try something.

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

   // Minimal form validation:   
   if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
   && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

      function validateEmailAddr($email){
         return preg_match('/[^\x00-\x20()<>@,;:\\".
         [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)+/i', $email);
      }

      if(!validateEmailAddr($email)){
            $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>';
      }
      else
      {
            // Create the body:
            $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
            \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

            // Make it no longer than 70 characters long:
            $body = wordwrap($body, 70);

            // Send the email:
            mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

            // Print a message:
            $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

            // Clear $_POST (so that the form's not sticky):
            $_POST = array();
      }
   } else {
      $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
   }
} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

use die()

but before die() you have to close all your html tags with footer class also

then you have to redirect your page to quote.php

 

eg

echo "<meta http-equiv='Refresh' content='3; URL=quote.php'/>";
"</table><div class='warning'><p style='font-weight: bold; color: #C00'>Please use a valid email address</p></div></div></div>"
include('footer.php')
die();

 

//change </table><dive class='warning'> etc with your own tags

Link to comment
Share on other sites

Wolf that is exactly how I want it to be except it says please enter a valid email even if you do enter a real email address.

 

Vividona so do I put what you just said instead of

 

if(!validateEmailAddr($email)){
die("Invalid email");
}

 

I'm a little confused by what I need to do

Link to comment
Share on other sites

Using die() in a form validation process is the equivalent of slamming the door in your customer's face. It's neither graceful nor polite. What you need is to validate that the supplied data in the field is what is expected, and if not, present the form again with a specific error message and the opportunity to correct the entry.

Link to comment
Share on other sites

Using die() in a form validation process is the equivalent of slamming the door in your customer's face. It's neither graceful nor polite. What you need is to validate that the supplied data in the field is what is expected, and if not, present the form again with a specific error message and the opportunity to correct the entry.

 

I know that Pikachu2000

but he should slam the door if the sender fill wrong email or miss a field

Link to comment
Share on other sites

okay i got it now. try this out.

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

   // Minimal form validation:   
   if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
   && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

      function validateEmailAddr($email){
         return preg_match('/[^\x00-\x20()<>@,;:\\".
         [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)+/i', $email);
      }

      if(!validateEmailAddr($email) && !empty($_POST['eMail'])){
            $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>';
      }
      else
      {
            // Create the body:
            $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
            \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

            // Make it no longer than 70 characters long:
            $body = wordwrap($body, 70);

            // Send the email:
            mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

            // Print a message:
            $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

            // Clear $_POST (so that the form's not sticky):
            $_POST = array();
      }
   } else {
      $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
   }
} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

No, as I said, the form should be presented again with the incorrectly completed fields highlighted and an error message, and give the user the opportunity to correct the entry and proceed, not give them a blank screen with a message that effectively says "We don't want you here".

Link to comment
Share on other sites

No, as I said, the form should be presented again with the incorrectly completed fields highlighted and an error message, and give the user the opportunity to correct the entry and proceed, not give them a blank screen with a message that effectively says "We don't want you here".

 

I used redirect code before die() to transfer sender to the form page.

 

if he need to keep form with error message then he should use javascript as I told before.

Link to comment
Share on other sites

Um hi guys you know that I have already submitted a code that should fix all his problems. All i did was made it check to see if it was not vaild and not empty. if it was not valid and not empty it says invalid email. and if it is empty (not wrote in meaning they probably just opened it up) then it dont display it. but if it is valid it posts it. all done with a modification of the if command.

 

@vivi- his source code continues after that to display the form if not correct. all done in 1 script.

Link to comment
Share on other sites

If you redirect, you lose everything that was filled in the form. That's very inconvenient for the user. On the other hand, if you store the errors in an array while validating the form, you can then check the array before processing the data. If the array is empty, you process, if the array is not empty, you redisplay the form, with their previous entries already filled in, and display error messages for each field that needs correction. This entire process can be done in the same script. It's a much better, more convenient way to do it, and the end result is less people get frustrated and decide to just leave.

 

 

 

 

Link to comment
Share on other sites

ok this is probably gonna be my last go at it. if this don't work you will have to get someone else to start where i left off.

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

   // Minimal form validation:   
   if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
   && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

      function validateEmailAddr($email){
         return preg_match('/[^\x00-\x20()<>@,;:\\".
         [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)+/i', $email);
      }

      if(validateEmailAddr($email)){
            // Create the body:
            $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
            \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

            // Make it no longer than 70 characters long:
            $body = wordwrap($body, 70);

            // Send the email:
            mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

            // Print a message:
            $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

            // Clear $_POST (so that the form's not sticky):
            $_POST = array();
      } else {
            $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>';
      }
   } else {
      $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
   }
} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

If you redirect, you lose everything that was filled in the form. That's very inconvenient for the user. On the other hand, if you store the errors in an array while validating the form, you can then check the array before processing the data. If the array is empty, you process, if the array is not empty, you redisplay the form, with their previous entries already filled in, and display error messages for each field that needs correction. This entire process can be done in the same script. It's a much better, more convenient way to do it, and the end result is less people get frustrated and decide to just leave.

 

Ding ding ding!  We have a winner.

 

I strongly suggest everyone not named Pikachu do a Google search for PHP sticky forms.  For my own sanity, if not your own.

Link to comment
Share on other sites

The code I'm using doesn't have any die in it and the fields do remain once you submit it and the error messages come back

 

Does anyone know how to make this submit if there is a valid email address as it currently says invalid email even if it is valid.

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

   // Minimal form validation:   
   if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
   && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

      function validateEmailAddr($email){
         return preg_match('/[^\x00-\x20()<>@,;:\\".
         [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)+/i', $email);
      }

      if(validateEmailAddr($email)){
            // Create the body:
            $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
            \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

            // Make it no longer than 70 characters long:
            $body = wordwrap($body, 70);

            // Send the email:
            mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

            // Print a message:
            $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

            // Clear $_POST (so that the form's not sticky):
            $_POST = array();
      } else {
            $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>';
      }
   } else {
      $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
   }
} // End of main isset() IF.

// Create the HTML form:
?>

 

Link to comment
Share on other sites

Lmao see what I mean. 1 small error I didn't catch. I'm glad you found it and got it working. Wow this is actually the longest thread that I am posting in currently lol. Took awhile but we got it hehe. Have fun coding and it wasn't a problem. I got time on my hands. If you want to you can add my yahoo or if you don't got yahoo but like another email provider then email me which one and I'll send you my address.

Link to comment
Share on other sites

oh yea and if you could kindly mark this as solved it would be appreciated. cause then other people looking for things to solve don't read this whole thing and then find out it is solved. also it helps others that have the same problem or close to it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.