linda_v Posted December 14, 2008 Share Posted December 14, 2008 Hi everybody...I know that this issue was already discussed here...but I'm still trying to fix my problem (tried different codes)...but still getting empty emails ...WHY???....Please HELP!!!!!!!!! Here are the codes: Validation: <script language="JavaScript"> function formCheck(formobj){ var fieldRequired = Array("first_name", "last_name" , "address" , "city" , "province" , "postal" , "phone"); var fieldDescription = Array("First Name", "Last Name" , "Address" , "City" , "Province" , "Postal Code" , "Phone"); var alertMsg = "Please complete the following fields:\n"; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired]; if (obj){ switch(obj.type){ case "select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ alertMsg += " - " + fieldDescription + "\n"; } break; case "select-multiple": if (obj.selectedIndex == -1){ alertMsg += " - " + fieldDescription + "\n"; } break; case "text": case "textarea": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription + "\n"; } break; default: } if (obj.type == undefined){ var blnchecked = false; for (var j = 0; j < obj.length; j++){ if (obj[j].checked){ blnchecked = true; } } if (!blnchecked){ alertMsg += " - " + fieldDescription + "\n"; } } } } if (alertMsg.length == l_Msg){ return true; }else{ alert(alertMsg); return false; } } </script> The Form: <form name = "catalogue" action="catalogue_sent.php" method="post" onsubmit="return formCheck(catalogue)"> <td valign="top" rowspan="12" width="1"><br></td> <td colspan="2"><font face="Trebuchet MS" style="font-size: 9pt"><img border="0" src="images/buttons/catalogue_request.jpg" width="450" height="43"></font></p> </td> </tr> <tr> <td width="265"><b class="style3"> First Name*:</b></td> <td width="209"><input type="text" size="30" name="first_name" value=""></td> </tr> <tr> <td class="style3"><b> Last Name*:</b></td> <td><input type="text" size="30" name="last_name" value=""></td> </tr> <tr> <td class="style3"><b> Company Name:</b></td> <td><input type="text" size="30" name="company_name" value=""></td> </tr> <tr> <td class="style3"><b> Email:</b></td> <td><input type="text" size="30" name="email" value=""></td> </tr> <tr> <td class="style3"><b> Address*:</b></td> <td><input type="text" size="30" name="address" value=""></td> </tr> <tr> <td class="style3"> <b> City*:</b> </td> <td><input type="text" size="30" name="city" value=""></td> </tr> <tr> <td class="style3"> <b> Province*:</b> </td> <td align="left"><select name="province"><option value=""></option> <option value=""> - Canada</option> <option value="AB"> Alberta</option> <option value="BC"> British Columbia</option> <option value="MB"> Manitoba</option> <option value="NB"> New Brunswick</option> <option value="NF"> Newfoundland</option> <option value="NT"> North West Territory</option> <option value="NS"> Nova Scotia</option> <option value="ON"> Ontario</option> <option value="PE"> Prince Edward Island</option> <option value="PQ"> Quebec</option> <option value="SK"> Saskatchewan</option> <option value="YT"> Yukon Territory</option> </select></td> </tr> <tr> <td class="style3"> <b> Postal Code*:</b> </td> <td> <input type="text" size="8" name="postal" value=""><br></td> </tr> <tr> <td valign="top" class="style3"> <b> Phone:</b> </td> <td> <input type="text" size="14" name="phone" value=""><br> <br> <img src="../images/speck.gif" width="1" height="15" alt="-" border="0"><br><input type="image" src="images/buttons/button_submit.jpg" alt="submit" name="submit" width="133" height="44" border="0" value=""></td> </tr> </form> And finally...the PHP: <?php $emailsubject = 'Catalogue Request' ; $web = '[email protected]' ; $first_nameField = $_POST['first_name']; $last_nameField = $_POST['last_name']; $company_nameField = $_POST['company_name']; $emailField = $_POST['email']; $addressField = $_POST['address']; $cityField = $_POST['city']; $provinceField = $_POST['province']; $postalField = $_POST['postal']; $phoneField = $_POST['phone'] ; $body =" <br><hr><br> First Name: ".$first_nameField." <br> Last Name: ".$last_nameField." <br> Company Name: ".$company_nameField." <br> Email: ".$emailField."<br> Address: ".$addressField." <br> City: ".$cityField." <br> Province: ".$provinceField."<br> Postal: ".$postalField." <br> Phone Number: ".$phoneField." <br> "; $headers = "From:emailField\r\n"; $headers .= "Content-type: text/html\n\n"; $success = mail($web, $emailsubject, $body, $headers); if($success) {print "Thank you for requesting the Sweet Basket catalogue."; } else {print "We encountered an error sending your mail"; } ?> Thanks for your help!!!!!! Link to comment https://forums.phpfreaks.com/topic/136902-blank-emails-from-php-form/ Share on other sites More sharing options...
mmarif4u Posted December 14, 2008 Share Posted December 14, 2008 Please use code blocks, easy for all to read your code. Link to comment https://forums.phpfreaks.com/topic/136902-blank-emails-from-php-form/#findComment-715000 Share on other sites More sharing options...
linda_v Posted December 14, 2008 Author Share Posted December 14, 2008 Thanks! here are the codes: Validation: <script language="JavaScript"> function formCheck(formobj){ var fieldRequired = Array("first_name", "last_name" , "address" , "city" , "province" , "postal" , "phone"); var fieldDescription = Array("First Name", "Last Name" , "Address" , "City" , "Province" , "Postal Code" , "Phone"); var alertMsg = "Please complete the following fields:\n"; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired]; if (obj){ switch(obj.type){ case "select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ alertMsg += " - " + fieldDescription + "\n"; } break; case "select-multiple": if (obj.selectedIndex == -1){ alertMsg += " - " + fieldDescription + "\n"; } break; case "text": case "textarea": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription + "\n"; } break; default: } if (obj.type == undefined){ var blnchecked = false; for (var j = 0; j < obj.length; j++){ if (obj[j].checked){ blnchecked = true; } } if (!blnchecked){ alertMsg += " - " + fieldDescription + "\n"; } } } } if (alertMsg.length == l_Msg){ return true; }else{ alert(alertMsg); return false; } } </script> The Form: <form name = "catalogue" action="catalogue_sent.php" method="post" onsubmit="return formCheck(catalogue)"> <td valign="top" rowspan="12" width="1"><br></td> <td colspan="2"><font face="Trebuchet MS" style="font-size: 9pt"><img border="0" src="images/buttons/catalogue_request.jpg" width="450" height="43"></font></p> </td> </tr> <tr> <td width="265"><b class="style3"> First Name*:</b></td> <td width="209"><input type="text" size="30" name="first_name" value=""></td> </tr> <tr> <td class="style3"><b> Last Name*:</b></td> <td><input type="text" size="30" name="last_name" value=""></td> </tr> <tr> <td class="style3"><b> Company Name:</b></td> <td><input type="text" size="30" name="company_name" value=""></td> </tr> <tr> <td class="style3"><b> Email:</b></td> <td><input type="text" size="30" name="email" value=""></td> </tr> <tr> <td class="style3"><b> Address*:</b></td> <td><input type="text" size="30" name="address" value=""></td> </tr> <tr> <td class="style3"> <b> City*:</b> </td> <td><input type="text" size="30" name="city" value=""></td> </tr> <tr> <td class="style3"> <b> Province*:</b> </td> <td align="left"><select name="province"><option value=""></option> <option value=""> - Canada</option> <option value="AB"> Alberta</option> <option value="BC"> British Columbia</option> <option value="MB"> Manitoba</option> <option value="NB"> New Brunswick</option> <option value="NF"> Newfoundland</option> <option value="NT"> North West Territory</option> <option value="NS"> Nova Scotia</option> <option value="ON"> Ontario</option> <option value="PE"> Prince Edward Island</option> <option value="PQ"> Quebec</option> <option value="SK"> Saskatchewan</option> <option value="YT"> Yukon Territory</option> </select></td> </tr> <tr> <td class="style3"> <b> Postal Code*:</b> </td> <td> <input type="text" size="8" name="postal" value=""><br></td> </tr> <tr> <td valign="top" class="style3"> <b> Phone:</b> </td> <td> <input type="text" size="14" name="phone" value=""><br> <br> <img src="../images/speck.gif" width="1" height="15" alt="-" border="0"><br><input type="image" src="images/buttons/button_submit.jpg" alt="submit" name="submit" width="133" height="44" border="0" value=""></td> </tr> </form> And finally...the PHP: <?php $emailsubject = 'Catalogue Request' ; $web = '[email protected]' ; $first_nameField = $_POST['first_name']; $last_nameField = $_POST['last_name']; $company_nameField = $_POST['company_name']; $emailField = $_POST['email']; $addressField = $_POST['address']; $cityField = $_POST['city']; $provinceField = $_POST['province']; $postalField = $_POST['postal']; $phoneField = $_POST['phone'] ; $body =" <br><hr><br> First Name: ".$first_nameField." <br> Last Name: ".$last_nameField." <br> Company Name: ".$company_nameField." <br> Email: ".$emailField."<br> Address: ".$addressField." <br> City: ".$cityField." <br> Province: ".$provinceField."<br> Postal: ".$postalField." <br> Phone Number: ".$phoneField." <br> "; $headers = "From:emailField\r\n"; $headers .= "Content-type: text/html\n\n"; $success = mail($web, $emailsubject, $body, $headers); if($success) {print "Thank you for requesting the Sweet Basket catalogue."; } else {print "We encountered an error sending your mail"; } ?> Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/136902-blank-emails-from-php-form/#findComment-715164 Share on other sites More sharing options...
Mad Mick Posted December 14, 2008 Share Posted December 14, 2008 I didn't see the original post, are you saying the email is totally blank or just has no data only the fixed text e.g. First Name? Link to comment https://forums.phpfreaks.com/topic/136902-blank-emails-from-php-form/#findComment-715170 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2008 Share Posted December 14, 2008 The php code has absolutely no checking in it that any form was submitted. So, anytime that page is requested for any reason (search engine, bot script...), it will send an email. If there is no $_POST data, the email will be blank. At a minimum you need to check that the submit button $_POST variable is set, using the isset() function. Since you are using an image as the submit button, read this link to find out how to detect it in the php code - http://us3.php.net/manual/en/faq.html.php#faq.html.form-image You also need to validate the form data in the php code after it has been received on the server. Your From: address in the header is malformed (missing the $ in front of the emailField variable name) and you should be putting $emailField into a Reply-To: field, not a From: field. The From: field should be a valid email address at the domain of the sending mail server. Link to comment https://forums.phpfreaks.com/topic/136902-blank-emails-from-php-form/#findComment-715180 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.