html - php mail form w/upload option not working correctly -
i trying create quote form basic contact info, checkbox option , option upload image email complete form email address. have html portion have problem php part. when form emails me sends contact info , fails send option check checkbox , image uploaded. here code form html portion:
<form action="quote_form.php" method="post" enctype="multipart/form-data" name="quoteform" id="quoteform" onsubmit="mm_validateform('name','','r','lastname','','r','email','','risemail','phone','','risnum','textfield','','risnum');mm_validateform('name','','r','lastname','','r','phone','','risnum','email','','risemail','info-about-item','','r');return document.mm_returnvalue"> <p> <label for="name">name*</label> <input name="name" type="text" id="name" size="60" maxlength="60" /> </p> <p> <label for="phone">phone #*</label> <input name="phone" type="text" id="phone" size="30" maxlength="30" /> </p> <p> <label for="email">email* </label> <input name="email" type="text" id="email" size="30" maxlength="30" /> </p> <p> <label> <input type="checkbox" name="servicetype[]" value="pawn" /> pawn</label> <br /> <label> <input type="checkbox" name="servicetype[]" value="buy" /> buy</label> <br /> </p> <p><br /><br /> <span style="font-style: italic">(the more information better --- description i.e. brand, model , condition of item.)</span></p> <p> <textarea name="comments" id="comments" cols="50" rows="10"></textarea> </p> <p> <label for="file">select file upload*</label> <input type="file" name="file" id="file" size="30" maxlength="30" /> </p> <p> <input type="submit" name="submit" id="sumbit" value="submit" /> <input name="reset" type="reset" id="reset" value="reset form" /> </p> <p> <input name="recipient" type="hidden" id="recipient" value="isabelpolanco23@gmail.com" /> <input name="redirect" type="hidden" id="redirect" value="thankyou-pg.html" /> <br /> </p> </form>
and here php portion:
<?php if (isset($_post['email'])) { $email_to = "isabelpolanco23@gmail.com"; $email_subject = "quote form"; function died($error) { echo "we sorry, there error(s) found form submitted. "; echo "these errors appear below.<br /><br />"; echo $error . "<br /><br />"; echo "please go , fix these errors.<br /><br />"; die(); } if ($_files["file"]["error"] > 0) { echo "error: " . $_files["file"]["error"] . "<br>"; } else { echo "upload: " . $_files["file"]["name"] . "<br>"; echo "type: " . $_files["file"]["type"] . "<br>"; echo "size: " . ($_files["file"]["size"] / 1024) . " kb<br>"; echo "stored in: " . $_files["file"]["tmp_name"]; } if (!isset($_post['name']) || !isset($_post['email']) || !isset($_post['phone']) || !isset($_post['comments']) ) { died('we sorry, there appears problem form submitted.'); } $name = $_post['name']; // required $email_from = $_post['email']; // required $phone = $_post['phone']; // not required $comments = $_post['comments']; // required foreach ($_post["servicetype"] $value) { if ($value=="pawn") $servicetype["pawn"] = true; elseif ($value=="buy") $servicetype["buy"] = true;
} }
$error_message = ""; $email_exp = '/^[a-za-z0-9._%-]+@[a-za-z0-9.-]+\.[a-za-z]{2,4}$/'; if (!preg_match($email_exp, $email_from)) { $error_message .= 'the email address entered not appear valid.<br />'; } $string_exp = "/^[a-za-z .'-]+$/"; if (!preg_match($string_exp, $name)) { $error_message .= 'the name entered not appear valid.<br />'; } if (strlen($comments) < 2) { $error_message .= 'the comments entered not appear 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 .= "name: " . clean_string($name) . "\n"; $email_message .= "email: " . clean_string($email_from) . "\n"; $email_message .= "comments: " . clean_string($comments) . "\n"; $email_message .= "pawn: " . clean_string(($checkboxgroup1["pawn"]) ? "yes" : "no") . "\n"; $email_message .= "buy: " . clean_string(($checkboxgroup2["buy"]) ? "yes" : "no") . "\n"; $headers = 'from: ' . $email_from . "\r\n" . 'reply-to: ' . $email_from . "\r\n" . 'x-mailer: php/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); $allowedexts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_files["file"]["name"]); $extension = end($temp); if ((($_files["file"]["type"] == "image/gif") || ($_files["file"]["type"] == "image/jpeg") || ($_files["file"]["type"] == "image/jpg") || ($_files["file"]["type"] == "image/pjpeg") || ($_files["file"]["type"] == "image/x-png") || ($_files["file"]["type"] == "image/png")) && ($_files["file"]["size"] < 20000) && in_array($extension, $allowedexts) ) { if ($_files["file"]["error"] > 0) { echo "error: " . $_files["file"]["error"] . "<br>"; } else { echo "upload: " . $_files["file"]["name"] . "<br>"; echo "type: " . $_files["file"]["type"] . "<br>"; echo "size: " . ($_files["file"]["size"] / 3024) . " kb<br>"; echo "stored in: " . $_files["file"]["tmp_upload"]; } } else { echo "invalid file"; } ?> <?php header("location:thankyou-pg.html"); exit; ?> <?php } ?>
your html form has checkboxes name "checkboxgroup1", not referenced @ in php script. also, php script looking form element name "servicetype", not present in html form. might have name mismatch.
as file, should check part of validation fails seeing how far gets "if" statements.
edit
looking @ code more, have several different problems acting in unison.
the "name" of checkboxes mismatched php script. change names of checkboxes "servicetype" in html.
if intend on using checkboxes instead of radio buttons, either need assign different names each checkbox or need pass them script array. pass array, have make name of each checkbox "servicetype[]" instead. putting "[]" @ end signifies each element checked have value added element of array named "servicetype", in case. otherwise, value of 1 may overwrite other.
also, logic in php script seems flawed in handling servicetype. perhaps better solution be:
$servicetype = array("pawn" => false, "buy" => false); foreach ($_post["servicetype"] $value) { if ($value=="pawn") $servicetype["pawn"] = true; elseif ($value=="buy") $servicetype["buy"] = true; } ... $email_message .= "pawn: " . clean_string(($servicetype["pawn"]) ? "yes" : "no") . "\n"; $email_message .= "buy: " . clean_string(($servicetype["buy"]) ? "yes" : "no") . "\n";
additional notes:
it bad practice have same id on multiple elements. recommend changing id's of both of checkboxes different values.
this line not necessary (at least not according code you've provided):
$mail->body .= "servicetype: \r\n";
as file upload, not moving file permanent location. when file uploaded, temporarily stored in file path
$_files["file"]["tmp_name"]
. when script finishes, file deleted. so, must move file before script finishes using move_uploaded_file(), so:move_uploaded_file($_files["file"]["tmp_name"], "/home/user/pic");
some notes on this: destination directory must have proper write permissions php create files in it. second, highly advised move file outside of document root, not publicly accessible on web. if plan on making image accessible on web, highly suggest take precautions not fall victim malicious attacks.
i don't know how want include image in email (whether want show or show path), i'll leave you. hope points in right direction.
Comments
Post a Comment