php - How can i add file upload to my existing contact form -
this question has answer here:
- send attachments php mail()? 11 answers
i have existing html contact form, add feature upload photo sent attachment. how can add php script. have added file upload field in html form name=datafile , have set attribute enctype="multipart/form-data"
<?php $youremailaddress = '123@123.com'; //put own email address here. //check make sure name field not empty if(trim($_post['contactname']) == '') { $haserror = true; } else { $name = trim($_post['contactname']); } //check make sure subject field not empty if(trim($_post['subject']) == '') { $haserror = true; } else { $subject = trim($_post['subject']); } //check make sure sure valid email address submitted if(trim($_post['email']) == '') { $haserror = true; } else if (!preg_match("/^[_\.0-9a-za-z-]+@([0-9a-za-z][0-9a-za-z-]+\.)+[a-za-z]{2,6}$/i", trim($_post['email']))) { $haserror = true; } else { $email = trim($_post['email']); } //check make sure comments entered if(trim($_post['message']) == '') { $haserror = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_post['message'])); } else { $comments = trim($_post['message']); } } //if there no error, send email if(!isset($haserror)) { $emailto = $youremailaddress; $body = "name: $name \n\nemail: $email \n\nsubject: $subject \n\nmessage:\n $comments"; $headers = 'from: interion template <'.$emailto.'>' . "\r\n" . 'reply-to: ' . $email; mail($emailto, $subject, $body, $headers); echo'<div id="success" class="sent success"><p><strong>email sent!</strong><br>thanks contacting us. email sent , \'ll in touch soon.</p></div>'; } else { //if errors found echo '<p class="error">please check if you\'ve filled fields valid information , try again. thank you.</p>'; } ?>
here html part
<form method="post" action="sendmail.php" enctype="multipart/form-data" id="contactform"> <div class="response"> </div> <p ><label for="contactname">what full legal name?<span>*</span></label> <input id="contactname" type="text" value="" name="contactname" placeholder="full name" class="textflied"> <i class="icon fa fa-user"></i></p> <p><label for="email" >what best email address contact on?<span>*</span></label> <input id="email" type="text" value="" name="email" placeholder="email address" class="textflied"> <i class="icon fa fa-envelope"></i></p> <p><label for="subject" >now tell best phone number reach at:<span>*</span></label> <input id="subject" type="text" value="" name="subject" placeholder="your phone number including area code" class="textflied"> <i class="icon fa fa-phone "></i></p> <p><label for="message" >in short paragraph how discribe self?<span>*</span></label> <textarea id="message" type="text" name="message" value="" placeholder="your short paragraph" rows="8" class="texttextarea"></textarea> <i class="icon fa fa-comments"></i></p> <label for="datafile" >finally lets match name face, upload recent photo<span>*</span></label> <input type="file" name="datafile" size="40"> <br> <br> <p> <button type="submit" name="submit" id="submitbutton" title="click here submit message!" class="btn btn-disabled">send message</button> </p> </form>
please add code , check may you
if(trim($_files['datafile']['name'])==''){ $haserror = true; } else { $target='upload/';/*give directory name wfere want save it*/ move_uploaded_file($_files['datafile']['tmp_name'],$target); } <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <?php $youremailaddress = '123@123.com'; //put own email address here. if(trim($_files['datafile']['name'])==''){ $haserror = true; } else { $target='upload/';/*give directory name wfere want save it*/ move_uploaded_file($_files['datafile']['tmp_name'],$target); } //check make sure name field not empty if(trim($_post['contactname']) == '') { $haserror = true; } else { $name = trim($_post['contactname']); } //check make sure subject field not empty if(trim($_post['subject']) == '') { $haserror = true; } else { $subject = trim($_post['subject']); } //check make sure sure valid email address submitted if(trim($_post['email']) == '') { $haserror = true; } else if (!preg_match("/^[_\.0-9a-za-z-]+@([0-9a-za-z][0-9a-za-z-]+\.)+[a-za-z]{2,6}$/i", trim($_post['email']))) { $haserror = true; } else { $email = trim($_post['email']); } //check make sure comments entered if(trim($_post['message']) == '') { $haserror = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_post['message'])); } else { $comments = trim($_post['message']); } } //if there no error, send email if(!$haserror) { $emailto = $youremailaddress; $body = "name: $name \n\nemail: $email \n\nsubject: $subject \n\nmessage:\n $comments"; $headers = 'from: interion template <'.$emailto.'>' . "\r\n" . 'reply-to: ' . $email; mail($emailto, $subject, $body, $headers); echo'<div id="success" class="sent success"><p><strong>email sent!</strong><br>thanks contacting us. email sent , \'ll in touch soon.</p></div>'; } else { //if errors found echo '<p class="error">please check if you\'ve filled fields valid information , try again. thank you.</p>'; } ?> </head> <body> <form method="post" action="" enctype="multipart/form-data" id="contactform"> <div class="response"> </div> <p > <label for="contactname">what full legal name?<span>*</span></label> <input id="contactname" type="text" value="" name="contactname" placeholder="full name" class="textflied"> <i class="icon fa fa-user"></i></p> <p> <label for="email" >what best email address contact on?<span>*</span></label> <input id="email" type="text" value="" name="email" placeholder="email address" class="textflied"> <i class="icon fa fa-envelope"></i></p> <p> <label for="subject" >now tell best phone number reach at:<span>*</span></label> <input id="subject" type="text" value="" name="subject" placeholder="your phone number including area code" class="textflied"> <i class="icon fa fa-phone "></i></p> <p> <label for="message" >in short paragraph how discribe self?<span>*</span></label> <textarea id="message" type="text" name="message" value="" placeholder="your short paragraph" rows="8" class="texttextarea"></textarea> <i class="icon fa fa-comments"></i></p> <label for="datafile" >finally lets match name face, upload recent photo<span>*</span></label> <input type="file" name="datafile" size="40"> <br> <br> <p> <button type="submit" name="submit" id="submitbutton" title="click here submit message!" class="btn btn-disabled">send message</button> </p> </form> </body> </html>
Comments
Post a Comment