How to Validate PHP Form Filed including Drop down and Text Area?

Discussion in 'PHP' started by moneyinhands, Jul 2, 2010.

  1. #1
    Dear Developers, I don't know PHP and MYSQL but i am trying to learn, i need your help. Kindly help me.
    I have designed a form field:
    
    <html>
    <head?
    <title>Form</title>
    </head>
    <body>
    <!--Start HTML Form--> 
    <form name='orderfrom' action='sendresults.php' method='post'>
    <table style="" cellpadding="2" cellspacing="0" border="0" bgcolor="#FFFFFF"> <tr><td style="" colspan="2"><font face="Verdana" size="2" color="#000000"><br /></font></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>First Name:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_0" name="element_0" value="" size="20" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left">
    <font face="Verdana" size="2" color="#000000"><b>Last Name:</b></font><span style="color:red;"><small>*</small></span></td><td style="">
    <input id="element_1" name="element_1" value="" size="20" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Date of Birth:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_2" name="element_2" value="" size="20" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Your Address:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_3" name="element_3" value="" size="30" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>State:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_4" name="element_4" value="" size="30" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Postal Code:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_5" name="element_5" value="" size="10" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Mobile No:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_6" name="element_6" value="" size="10" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Landline No:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_7" name="element_7" value="" size="10" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Email address:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_8" name="element_8" value="" size="30" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Confirm Email:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><input id="element_9" name="element_9" value="" size="30" class="validate[required]" type="text" /><div style="color:#000000"><small></small></div></td></tr><tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>Product You Are Ordering:</b></font><span style="color:red;"><small>*</small></span></td><td style=""><select id="element_10" name="element_10" class="validate[required]">
    <select name="order">
    <option selected="please_select">Please Select</option>
    <option value=">Rs.550/-(Data Entry Project with Training)">Rs.550/-(Data Entry Project with Training)</option>
    <option value=" Rs.550/-(Ad Posting Project with Training)"> Rs.550/-(Ad Posting Project with Training)</option>
    <option value="Rs.550/-(SMS Reading with Referral Program)">Rs.550/-(SMS Reading with Referral Program)</option>
    <option value=" Rs.1050/-(Google Online/AdSense Training Only)">Rs.1050/-(Google Online/AdSense Training Only)</option>
    <option value="Rs.2650/-(Multiple Project with Training)">Rs.2650/-(Multiple Project with Training)</option>
    <option value="Rs.10,000/-(Website Designing)">Rs.10,000/-(Website Designing)</option>
    </select>
    <tr valign="top"><td style="" align="left"><font face="Verdana" size="2" color="#000000"><b>I Agree to all Terms and Conditions</b></font><span style="color:red;"><small>*</small></span></td><td style=""><div style="width:100%;"><input id="element_11_0" name="element_11[]" value="I Confirm." class="validate[required]"  type="checkbox" /><font face="Verdana" size="2" color="#000000">I Confirm.&nbsp;</font></div><div style="clear:both;"></div><div style="color:#000000"><small></small></div></td></tr>
    <tr valign="top"><td style="" align="center"><font face="Verdana" size="2" color="#000000">
    <tr><td colspan="2" align="center"><input  value="Submit Order" type="submit" />
    <input  value="Clear" type="reset" /></td></tr></table>
    </form>
    <!--End HTML Form-->
    </body>
    </html>
    
    Code (markup):
    THE FINAL FIELDS ARE:

    First Name:*
    Last Name:*
    Date of Birth:*
    Your Address:*
    State:*
    Postal Code:*
    Mobile No:*
    Landline No:*
    Email address:*
    Confirm Email:*
    Product You Are Ordering:* (this is drop down menu)
    I Agree to all Terms and Conditions* (this is check box)

    And i have created a PHP File to validate, but this file is not working. Please help to find errors and resolve this.
    
    <?php
    if(isset($_POST['email'])) {
    	
    	// EDIT THE 2 LINES BELOW AS REQUIRED
    	$email_to = "order@moneyinhands";
    	$email_subject = "Feedback via the Contact Form";
    	$url = 'http://moneyinhands.com/thank-you.html'; 
    	
    	
    	function died($error) {
    		// your error code can go here
    		echo "We are very sorry, but there were error(s) found with the form your 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['first_name']) ||
    		!isset($_POST['last_name']) ||
    		!isset($_POST['date_of_birth']) ||
    		!isset($_POST['your_address']) ||
    		!isset($_POST['state']) ||
    		!isset($_POST['postal_code']) ||
    		!isset($_POST['mobile_no']) ||
    		!isset($_POST['landline_no']) ||
    		!isset($_POST['email_address']) ||
    		!isset($_POST['confirm_email']) ||
    		!isset($_POST['product_you_are_ordering']) ||
    		!isset($_POST['i_agree_to_all_terms_and_conditions'])) {
    		died('We are sorry, but there appears to be a problem with the form your submitted.');		
    	}
    	// validation code for drop down field only
    if (isset ($_POST['order']) && ($_POST['order'] != 'please_select')) {
        $order = mysql_real_escape_string ($_POST['order']);
    } else {
        $order = FALSE;
        $errMsg = "<p>Please select a Product which you are ordering.</p>";
    }
    	
    	$first_name = $_POST['first_name']; // required
    	$last_name = $_POST['last_name']; // required
    	$email_from = $_POST['date_of_birth']; // required
    	$telephone = $_POST['your_address']; // required
    	$comments = $_POST['state']; // required
    	$first_name = $_POST['postal_code']; // required
    	$last_name = $_POST['mobile_no']; // required
    	$email_from = $_POST['landline_no']; // required
    	$telephone = $_POST['email_address']; // not required
    	$comments = $_POST['confirm_email']; // required
    	$first_name = $_POST['product_you_are_ordering']; // required
    	$last_name = $_POST['i_agree_to_all_terms_and_conditions']; // required
    	
    	$string_exp = "^[a-z .'-]+$";
      if(!eregi($string_exp,$first_name)) {
      	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!eregi($string_exp,$last_name)) {
      	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(strlen($your_address) < 2) {
      	$error_message .= 'The Address you entered do not appear to be valid.<br />';
      }
      if(strlen($state) < 2) {
      	$error_message .= 'The State you entered do not appear to be valid.<br />';
      }
      $string_exp = "^[0-9 .-]+$";
      if(!eregi($string_exp,$postal_code)) {
      	$error_message .= 'The Postal Code you entered does not appear to be valid.<br />';
      }
      $string_exp = "^[0-9 .-]+$";
      if(!eregi($string_exp,$mobile_no)) {
      	$error_message .= 'The Mobile Number you entered does not appear to be valid.<br />';
      }
      $string_exp = "^[0-9 .-]+$";
      if(!eregi($string_exp,$landline_no)) {
      	$error_message .= 'The Landline Code you entered does not appear to be valid.<br />';
      }
      $error_message = "";
    	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
      if(!eregi($email_exp,$email_address)) {
      	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
      $error_message = "";
    	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
      if(!eregi($email_exp,$confirm_email)) {
      	$error_message .= 'The Confirm Email Address you entered does not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
      	died($error_message);
      }
    	$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 .= "First Name: ".clean_string($first_name)."\n";
    	$email_message .= "Last Name: ".clean_string($last_name)."\n";
    	$email_message .= "Date of Birth: ".clean_string($date_of_birth)."\n";
    	$email_message .= "Your Address: ".clean_string($your_address)."\n";
    	$email_message .= "State: ".clean_string($state)."\n";
    	$email_message .= "Postal Code: ".clean_string($postal_code)."\n";
    	$email_message .= "Mobile No: ".clean_string($mobile_no)."\n";
    	$email_message .= "Landline No: ".clean_string($landline_no)."\n";
    	$email_message .= "Email address: ".clean_string($email_address)."\n";
    	$email_message .= "Confirm Email: ".clean_string($confirm_email)."\n";
    	$email_message .= "Product You are Ordering: ".clean_string($product_you_are_ordering)."\n";
    	$email_message .= "I Agree to all Terms and Conditions: ".clean_string($i_agree_to_all_terms_and_conditions)."\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.
    
    <?
    }
    ?>
    
    Code (markup):
    Please help me to create better PHP file to send this order from to my email id.
    Thanks for help in advance.
     
    moneyinhands, Jul 2, 2010 IP
  2. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #2
    This script is overkill... You can do the same in 1/3 of the code. To find the error is super complicated without more information. What kind of error? Maybe you forgot a ";" or used a ' instead of ". Maybe you create an infinite loop or the validation is always wrong, no matter what you type in. The different amount of errors you can get is huge. I'm not going to read your whole script by the letter as that takes way too much time. At least tell what it is that doesn't work. What the error is. Why it doesn't work.
     
    SedNaX, Jul 4, 2010 IP