Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

validation code

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

validation code

Postby aelynne7 » Tue Aug 20, 2013 1:09 am

I have this code, but I need to add validation code, so that users will not submit a blank form or leaving a field blank. How should I do that?

Code: Select all
<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = a@domain.com";
    $email_subject = "Feedback";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['address']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
   
   
    $first_name = $_POST['name']; // required
    $last_name = $_POST['address']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
     
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "Name: ".clean_string($first_name)."\n";
    $email_message .= "Address: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>
 
<!-- include your own success html here -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
}
?>

aelynne7
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Aug 19, 2013 11:55 pm

Re: validation code

Postby cualquiera » Fri Aug 23, 2013 12:46 am

Hello,

I don't see that complex, as far as I undestand correctly. One basic option is validate after the //required //not required fields, by doing:

if ($first_name == '' || $last_name == '' || ....

Then, with the e-mail you can do regexp like this:

if (!preg_match('\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b',$first_name, $matches))....

Note: read further here: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address

Regards
cualquiera
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Aug 22, 2013 6:52 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron