PHP form with checkboxes, radio buttons and file attachment -
i'm having nightmare php form user can fill in, attach doc/pdf , submit. after trying sort out previous code i've used, i've stripped out , think should start again in hope geniuses can help!
here html of contact.php:
<form action="" method="post" name="contact" id="contact"> <p>job title:*<br /> <input name="position" type="text" /></p> <p>nationality:*<br /> <select name="nationality"> <option value="">-- select 1 --</option> <option value="afghan">afghan</option> <option value="albanian">albanian</option> <option value="algerian">algerian</option> <option value="zambian">zambian</option> <option value="zimbabwean">zimbabwean</option> </select> </p> <p>which country living in?*<br /> <select name="country"> <option value="">-- select 1 --</option> <option value="united kingdom">united kingdom</option> <option value="afghanistan">afghanistan</option> <option value="africa">africa</option> <option value="zambia">zambia</option> <option value="zimbabwe">zimbabwe</option> </select> </p> <label class="radio" for="checkright">yes/no question?</label><br /> <input class="radio" type="radio" name="right" value="yes" /> yes <input class="radio" type="radio" name="right" value="no" /> no <input class="radio" type="radio" name="right" value="n/a" /> not applicable <p>yes/no question?<br /> <select name="continue"> <option value="">-- select 1 --</option> <option value="yes">yes</option> <option value="no">no</option> </select> </p> <p>select resorts:<br /> resort 1<input name="res1" type="checkbox" value="resort 1" /> resort 2<input name="res2" type="checkbox" value="resort 2" /> resort 3<input name="res3" type="checkbox" value="resort 3" /> resort 4<input name="res4" type="checkbox" value="resort 4" /> resort 5<input name="res5" type="checkbox" value="resort 5" /> resort 6<input name="res6" type="checkbox" value="resort 6" /> </p> <p>don't send form unless checked:* <input type="checkbox" name="parttime" value="yes" /></p> <p>date of arrival: <input name="arrive" id="datepick" /><br /> date of departure: <input name="depart" id="datepick2" /></p> <script type="text/javascript" src="assets/scripts/datepickr/datepickr.js"></script> <link href="assets/scripts/datepickr/datepickr.css" rel="stylesheet"> <script type="text/javascript"> new datepickr('datepick'); new datepickr('datepick2', { }); </script> <p>name:*<br /> <input name="name" type="text" /></p> <p>e-mail:*<br /> <input name="email" type="text" /></p> <p>telephone:*<br /> <input name="telephone" type="text" class="ctextfield" /></p> <p>upload cv (word of pdf formats only):<br /> <input type="file" name="cv" class="textfield"></p> <p><input name="submit" value="submit enquiry" class="submitbutton" type="submit" /><div style="visibility:hidden; width:1px; height:1px"><input name="url" type="text" size="45" id="url" /></div></p> </form>
by way, date boxes work excuse javascript in there!
to prevent spam i've used trick there's hidden url field must left blank form submit can see in php.
below i'm @ php placed above header of contact.php...
<?php if (array_key_exists('submit', $_post)) { $position = $_post['position']; $arrive = $_post['arrive']; $nationality = $_post['nationality']; $parttime = $_post['parttime']; $depart = $_post['depart']; $name = $_post['name']; $email = $_post['email']; $telephone = $_post['telephone']; $to = "me@mywebsite.com"; $subject = "recruitment application"; $message = $headers; $message .= "name: " . $_post["name"] . "\r\n"; $message .= "e-mail: " . $_post["email"] . "\r\n"; $headers = "mime-version: 1.0\r\n"; $headers .= "content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "reply-to: " . $_post["email"] . "\r\n"; $headers .= 'from: website <application@mywebsite.com>' . "\r\n"; $message= " "; $url = stripslashes($_post["url"]); if (!empty($url)) { header( 'location: http://www.go-away-spam-robots.com' ); exit(); } if (!isset($warning)) { mail($to, $subject, $message, $headers); header( 'location: http://www.mywebsite.co.uk/sent.php' ); } } ?>
i make pretty field compulsory if field left empty (other hidden url field), warning message displayed next field.
also file upload field attach email sent me , have results come through me in table format.
can me form working?
thank , hope hear you!
sm
✓ tested
there few errors , posted below, tested. file attachment part, need that. don't write code, fix given work with.
what happening you're overwriting message line.
$message= " ";
so gets thrown away previous $message
variable.
first, start removing line $message = $headers;
and replacing line $message ="";
then removing line
$message= " ";
✓ new body of code:
$message =""; $message .= "name: " . $_post["name"] . "\r\n"; $message .= "e-mail: " . $_post["email"] . "\r\n"; $headers = "mime-version: 1.0\r\n"; $headers .= "content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "reply-to: " . $_post["email"] . "\r\n"; $headers .= 'from: website <application@mywebsite.com>' . "\r\n";
✓ regarding file attachments
in order attach file, form not contain enctype="multipart/form-data"
it must included , looking following:
<form action="" method="post" name="contact" id="contact" enctype="multipart/form-data">
i suggest google "how attach file email in php" or "email file attachment in php".
you can have @ example , accepted answer here on stackoverflow on how achieve this.
✓ example file attachment php handler
here example php handler on how attach file.
source: http://webcheatsheet.com/php/send_email_text_html_attachment.php
<?php //define receiver of email $to = 'youraddress@example.com'; //define subject of email $subject = 'test email attachment'; //create boundary string. must unique //so use md5 algorithm generate random hash $random_hash = md5(date('r', time())); //define headers want passed. note separated \r\n $headers = "from: webmaster@example.com\r\nreply-to: webmaster@example.com"; //add boundary string , mime type specification $headers .= "\r\ncontent-type: multipart/mixed; boundary=\"php-mixed-".$random_hash."\""; //read atachment file contents string, //encode mime base64, //and split smaller chunks $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); //define body of message. ob_start(); //turn on output buffering ?> --php-mixed-<?php echo $random_hash; ?> content-type: multipart/alternative; boundary="php-alt-<?php echo $random_hash; ?>" --php-alt-<?php echo $random_hash; ?> content-type: text/plain; charset="iso-8859-1" content-transfer-encoding: 7bit hello world!!! simple text email message. --php-alt-<?php echo $random_hash; ?> content-type: text/html; charset="iso-8859-1" content-transfer-encoding: 7bit <h2>hello world!</h2> <p>this <b>html</b> formatting.</p> --php-alt-<?php echo $random_hash; ?>-- --php-mixed-<?php echo $random_hash; ?> content-type: application/zip; name="attachment.zip" content-transfer-encoding: base64 content-disposition: attachment <?php echo $attachment; ?> --php-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents $message variable , delete current output buffer $message = ob_get_clean(); //send email $mail_sent = @mail( $to, $subject, $message, $headers ); //if message sent print "mail sent". otherwise print "mail failed" echo $mail_sent ? "mail sent" : "mail failed"; ?>
Comments
Post a Comment