Jump to content

Contact Form Sendmail


tejasamrute

Recommended Posts

Problems i am facing with sendmail form are Validating Radio box, Check Box, Upload File (according to the formats mentioned) & sending their respective values to the email

 

 

This is the code i hav so far

<?php if(isset($_POST['txtName'])) {           // EDIT THE 2 LINES BELOW AS REQUIRED     $email_to = "abc@xyz.com";    $email_subject = "Enquiry";	                 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['txtName']) ||		!isset($_POST['txtContact']) ||		!isset($_POST['txtCategory']) ||		!isset($_POST['txtNewspaper']) ||        !isset($_POST['txtComments'])) {         died('We are sorry, but there appears to be a problem with the form you submitted.');            }               $name = $_POST['txtName']; // required  	$contact = $_POST['txtContact']; 	$category = $_POST['txtCategory']; // required 	$newspaper = $_POST['txtNewspaper']; // required     $comments = $_POST['txtComments']; // required           $error_message = "";      $string_exp = "/^[A-Za-z .'-=&%$#@{}^*|_!]+/i";   if(!preg_match($string_exp,$name)) {     $error_message .= 'The Name you entered does not appear to be valid.<br />';   }               if(strlen($comments) < 2) {     $error_message .= 'The Comments you entered do not appear to be valid.<br />';   }   if(strlen($error_message) > 0) {     died($error_message);   }     $email_message = "Below are the Form Details.nn";      $email_from = $full_name.'<'.$email_from.'>';     function clean_string($string) {       $bad = array("content-type","bcc:","to:","cc:","href");       return str_replace($bad,"",$string);     }           	 	    $email_message .= "Name: ".clean_string($name)."n"; 	$email_message .= "Contact: ".clean_string($contact)."n"; 		$email_message .= "Category: ".clean_string($category)."n";    $email_message .= "Comments: ".clean_string($comments)."n";    	            // create email headers $headers = 'From: '.$name."rn". //'Reply-To: '.$email."rn" . 'X-Mailer: PHP/' . phpversion();@mail($email_to, $email_subject, $email_message, $headers);   ?>   <!-- include your own success html here -->   <font face="Arial, Helvetica, sans-serif">Thank you for contacting us. We will be in touch with you very soon.</font><br><br><a href="ad-booking.html" style="background-color:#ed6124; color:#fff; padding:10px; text-decoration:none">Back to the Page</a> <?php } ?>

i am very rookie in PHP...so plz help

post-175359-0-00213100-1425302104_thumb.jpg

Link to comment
Share on other sites

There's a section on form validation here:http://www.w3schools.com/php/php_form_validation.aspThere's a brief section about handling file uploads here:http://www.sitepoint.com/file-uploads-with-php/You should be able to find several other examples of form processing or file uploads with PHP. If you want to send an email with attachments, I would recommend using something like PHPMailer or Pear Mail.https://github.com/Synchro/PHPMailer

Link to comment
Share on other sites

i am messing things up.....it seems the codes doesn't fit into the code in have......can you write the code according the code i have......Sorry for trouble

Link to comment
Share on other sites

i got this script for multiple check box.......but it i don't get multiple values and also it does'nt give error message when the form is sent without checking any box....what can be the prob??

if(isset($_POST['txtNewspaper'])){    if($_POST['txtNewspaper'] == 'TOI')    {       // the Purchase radio button was selected    }    elseif($_POST['txtNewspaper'] == 'ET')    {        // the Sell radio button was selected    }	 elseif($_POST['txtNewspaper'] == 'GS')    {        // the Sell radio button was selected    }	 elseif($_POST['txtNewspaper'] == 'BS')    {        // the Sell radio button was selected    }	 elseif($_POST['txtNewspaper'] == 'MT')    {        // the Sell radio button was selected    }	 elseif($_POST['txtNewspaper'] == 'NBT')    {        // the Sell radio button was selected    }    else    {       // the user submitted an unexpected value?    }}else{    $error_message .= 'Please Select a Newspaper.<br />';}
Link to comment
Share on other sites

 <form name="contactform" method="post" action="ad-booking-sendmail.php" enctype="multipart/form-data">       		<strong>Give Your Name</strong>:     <input name="txtName" type="text" class="input" id="txtName"><br>                        <strong>Your Tel No.</strong>:       <input name="txtContact" type="text" id="txtContact" class="input"><br>              		<strong>Select Ad Category</strong>: <br>		<input name="txtCategory" type="radio" value="Single Col."> Single Col.        <input name="txtCategory" type="radio" value="Double Col." class="radio"> Double Col.        <input name="txtCategory" type="radio" value="Display Col." class="radio"> Display Col.          <br><br>                                      <strong>Select News Paper</strong> :<br>            <input type="checkbox" name="txtNewspaper[]" id="txtNewspaper" value="TOI"> TOI  			<input type="checkbox" name="txtNewspaper[]" id="txtNewspaper" value="ET" class="radio"> ET            <input type="checkbox" name="txtNewspaper[]" id="txtNewspaper" value="GS" class="radio"> GS            <input type="checkbox" name="txtNewspaper[]" id="txtNewspaper" value="BS" class="radio"> BS            <input type="checkbox" name="txtNewspaper[]" id="txtNewspaper" value="MT" class="radio"> MT            <input type="checkbox" name="txtNewspaper[]" id="txtNewspaper" value="NBT" class="radio"> NBT       <br><br>                                   <!----   <strong>Attach Content</strong> <span class="format">(doc, docx)</span><br>               <input type="file" name="filename" id="filename"><br><br> ----->          <!---- <strong>Attach Photograph</strong> <span class="format">(jpg, png)</span><br>          <label for="txtResume"></label>     <input type="hidden" name="upload" value="1" /><input type="file" name="txtResume" id="txtResume"><br><br> ---->                                                            <strong>Message</strong>       <textarea name="txtComments" cols="58" rows="8" id="txtComments"></textarea><br>                        <input name="" type="submit" value="Send" class="button">                   </form>

i found some script for radio and check box. and i have added this under txtName validation...but they don't show error msgs

 if($_POST['txtCategory'] == "false") {    $error_message .= 'Please Select a Category.<br />'; } if($newspaper = nl2br(implode(',', $_POST['txtNewspaper'])));

Now the problem is.......if i don't select anything in Category & Newspaper.......the form doesn't go through....which is good.....but it doesn't even show

the error msg of the respective field.....inturn it shows error msg like this..

 

" We are very sorry, but there were error(s) found with the form you submitted. "

 

 

Probably i might be confusing you ...but i guess you will have to actually run my script to be able to understand.....

Link to comment
Share on other sites

$_POST['txtNewspaper'] will be an array if any checkbox was selected, or else it won't be set at all. You can just use isset to figure out if they checked anything:

if (!isset($_POST['txtNewspaper'])) {  echo 'no checkboxes selected';}else {  echo count($_POST['txtNewspaper']) . ' checkboxes selected';}
Link to comment
Share on other sites

This is the final lookout of the form..but there is 1 unusual problem.....i want the name of the sender to show in mail's sender column....if i write the name in the textfield without space...it shows up.....but if i use space to write full name....it shows (unknown sender).....i cant seem to understand the problem

<?php  if(isset($_POST['txtName'])) {     // EDIT THE 2 LINES BELOW AS REQUIRED     $email_to = "abc@xyz.com";    $email_subject = "Enquiry";$semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";     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['txtName']) ||		!isset($_POST['txtContact']) ||		!isset($_POST['txtCategory']) ||		!isset($_POST['txtNewspaper']) ||        !isset($_POST['txtComments'])) {         died('We are sorry, but there appears to be a problem with the form you submitted.');            }    $fname = $_POST['txtName']; // required  	$contact = $_POST['txtContact']; 	$category = $_POST['txtCategory'];	$newspaper = $_POST['txtNewspaper'];    $comments = $_POST['txtComments']; // required    $error_message = "";      $string_exp = "/^[A-Za-z .'-=&%$#@{}^*|_!]+/i";   if(!preg_match($string_exp,$fname)) {     $error_message .= 'The Name you entered does not appear to be valid.<br />';   }       $string_exp = "/^[0-9]+$/";   if(!preg_match($string_exp,$contact)) {     $error_message .= 'The Contact Number you entered does not appear to be valid.<br />';   }   if($_POST['txtCategory'] == "false") {    $error_message .= 'Please Select a Category.<br />'; }if($newspaper = nl2br(implode(', ', $_POST['txtNewspaper'])));    if(strlen($comments) < 2) {     $error_message .= 'The Comments you entered do not appear to be valid.<br />';   }   if(strlen($error_message) > 0) {     died($error_message);   }     $email_message = "Below are the Form Details.nn";      function clean_string($string) {       $bad = array("content-type","bcc:","to:","cc:","href");       return str_replace($bad,"",$string);     }	    $email_message .= "Name: ".clean_string($fname)."nn";	$email_message .= "Contact: ".clean_string($contact)."nn"; 		$email_message .= "Category: ".clean_string($category)."nn";	$email_message .= "Newspaper: ".clean_string($newspaper)."nn";    $email_message .= "Comments: ".clean_string($comments)."nn"; // multipart boundary $email_message .= "This is a multi-part message in MIME format.nn" . "--{$mime_boundary}n" . $email_message . "nn"; foreach($_FILES as $userfile){ // store the file information to variables for easier access $tmp_name = $userfile['tmp_name']; $type = $userfile['type']; $name = $userfile['name']; $size = $userfile['size']; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } $email_message .= "--{$mime_boundary}n" . "Content-Type: {$type};n" . " name="{$name}"n" . "Content-Disposition: attachment;n" . " filename="{$fileatt_name}"n" . "Content-Transfer-Encoding: base64nn" . $data . "nn"; } } 	// create email headers $headers = 'From: '.$fname."rn". //'Reply-To: '.$email."rn" . 'X-Mailer: PHP/' . phpversion();   // headers for attachment $headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary="{$mime_boundary}""; @mail($email_to, $email_subject, $email_message, $headers);   ?>   <!-- include your own success html here -->   <font face="Arial, Helvetica, sans-serif">Thank you for contacting us. We will be in touch with you very soon.</font><br><br><a href="ad-booking.html" style="background-color:#ed6124; color:#fff; padding:10px; text-decoration:none">Back to the Page</a> <?php } ?>
Edited by Tezzo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...