welchyboy Posted March 19, 2009 Share Posted March 19, 2009 Hello everyone I could use some help. I have a form which I have on a site to you can see and I like it very much. However the validation I have I don't really like. I have it setup to check if certain fields are blank and if I hit submit it will check the first field to see if it is blank if it is it will take me to an error page. However if it is not it will go to the next field and check. However what I would like is it to check the whole form and display all the errors at once. I would also prefer to have the errors displayed on the page without having to refresh. I have spent many hours on this and just trying to perfect it. The way I have it set up it works and even sends the email just the way I like I just need some help with the validation. Please if anyone can help I would be extremely thankful. Here is some detailed information about what I am working with...I would also like to be able to have the sender be the name that is entered by the user. I have tried changing the headers and so forth but doesn't seem to work. My PHP looks like this <? $Name = $_POST["Name"]; $Address = $_POST["Address"]; $Address2 = $_POST["Address2"]; $City = $_POST["City"]; $State = $_POST["State"]; $Country = $_POST["Country"]; $Zip = $_POST["Zip"]; $Phone = $_POST["Phone"]; $Email = $_POST["Email"]; $today = date("M d, Y"); $recipient = "you@yourdomain.com"; $subject = "TEST SUBJECt"; $headers = 'From: <webmaster@example.com>' . "\r\n"; $forminfo = "Name: $Name\n Address: $Address\n Address2: $Address2\n City: $City\n State: $State\n Country: $Country\n Zip: $Zip\n Phone: $Phone\n Email: $Email\n Form Submitted: $today\n\n"; if (!empty($_POST['Name'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your first name.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['Address'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your Street Name.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['City'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your City.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['State'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your State.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['Country'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your Country.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } /* if (!empty($_POST['LastName'])){ $msg = ""; }else{ $LastName = NULL; echo "Please fill out your Last name.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=form.html\">"; exit; } */ if (preg_match ("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $Email)) { mail($toaddress,$subject,$headers,$message); //clear the variables $Name=''; $Email=''; echo ""; } else { echo nl2br ("Please enter correct email.\n"); echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } $formsend = mail("$recipient", "$subject", "$forminfo", "From: $email\r\nReply-to:$email"); ?> <!-- end PHP easy-form --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Make a Donation</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta http-equiv="refresh" content="5; URL=index.php?main_page=contact_us_1"> <style type="text/css"> <!-- .style1 { font-size: 24px; font-weight: bold; } body { background-color: #ade4b0; } --> </style> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td> <p align="left" class="style1">Thank you. You submission has been complete.Redirecting...</p> </tr> </table> </td> </body> </html> My HTML looks like this <!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 Form</title> <link rel="stylesheet" type="text/css" href="view.css" media="all"> <script type="text/javascript" src="view.js"></script> </head> <body id="main_body" > <br /> <br /> <div id="form_container"> <form id="form_14512" class="appnitro" method="post" action="myform.php"> <div class="form_description"> <h2>Request A Brochure</h2> </div> <ul > <li id="li_1" > <label class="description" for="Name">* Name </label> <div> <input id="Name" name="Name" class="element text medium" type="text" maxlength="255" value=""/> </div><p class="guidelines" id="guide_1"><small>Enter your name</small></p> </li> <li id="li_2" > <label class="description" for="element_2">* Mailing Address </label> <div><strong> <input id="Address" name="Address" class="element text large" value="" type="text"> <label for="Address">* Street Address</label> </strong></div> <div> <input id="Address2" name="Address2" class="element text large" value="" type="text"> <label for="Address2">Address Line 2</label> </div> <div class="left"><strong> <input id="City" name="City" class="element text medium" value="" type="text"> <label for="City">* City</label> </strong></div> <div class="right"><strong> <input id="State" name="State" class="element text medium" value="" type="text"> <label for="State">* State / Province / Region</label> </strong></div> <div class="left"><strong> <input id="Zip" name="Zip" class="element text medium" maxlength="15" value="" type="text"> <label for="Zip">* Postal / Zip Code</label> </strong></div> <div class="right"><strong> <select class="element select medium" id="Country" name="Country"> <option value="" selected="selected"></option> <option value="Afghanistan" >Afghanistan</option> <option value="Albania" >Albania</option> <option value="Algeria" >Algeria</option> <option value="Andorra" >Andorra</option> <option value="Antigua and Barbuda" >Antigua and Barbuda</option> <option value="Argentina" >Argentina</option> <option value="Armenia" >Armenia</option> <option value="Australia" >Australia</option> <option value="Austria" >Austria</option> <option value="Azerbaijan" >Azerbaijan</option> <option value="Bahamas" >Bahamas</option> <option value="Bahrain" >Bahrain</option> <option value="Bangladesh" >Bangladesh</option> <option value="Barbados" >Barbados</option> <option value="Belarus" >Belarus</option> <option value="Belgium" >Belgium</option> <option value="Belize" >Belize</option> <option value="Benin" >Benin</option> <option value="Bhutan" >Bhutan</option> <option value="Bolivia" >Bolivia</option> <option value="Bosnia and Herzegovina" >Bosnia and Herzegovina</option> <option value="Botswana" >Botswana</option> <option value="Brazil" >Brazil</option> <option value="Brunei" >Brunei</option> <option value="Bulgaria" >Bulgaria</option> <option value="Burkina Faso" >Burkina Faso</option> <option value="Burundi" >Burundi</option> <option value="Cambodia" >Cambodia</option> <option value="Cameroon" >Cameroon</option> <option value="Canada" >Canada</option> <option value="Cape Verde" >Cape Verde</option> <option value="Central African Republic" >Central African Republic</option> <option value="Chad" >Chad</option> <option value="Chile" >Chile</option> <option value="China" >China</option> <option value="Colombia" >Colombia</option> <option value="Comoros" >Comoros</option> <option value="Congo" >Congo</option> <option value="Costa Rica" >Costa Rica</option> <option value="Côte d'Ivoire" >Côte d'Ivoire</option> <option value="Croatia" >Croatia</option> <option value="Cuba" >Cuba</option> <option value="Cyprus" >Cyprus</option> <option value="Czech Republic" >Czech Republic</option> <option value="Denmark" >Denmark</option> <option value="Djibouti" >Djibouti</option> <option value="Dominica" >Dominica</option> <option value="Dominican Republic" >Dominican Republic</option> <option value="East Timor" >East Timor</option> <option value="Ecuador" >Ecuador</option> <option value="Egypt" >Egypt</option> <option value="El Salvador" >El Salvador</option> <option value="Equatorial Guinea" >Equatorial Guinea</option> <option value="Eritrea" >Eritrea</option> <option value="Estonia" >Estonia</option> <option value="Ethiopia" >Ethiopia</option> <option value="Fiji" >Fiji</option> <option value="Finland" >Finland</option> <option value="France" >France</option> <option value="Gabon" >Gabon</option> <option value="Gambia" >Gambia</option> <option value="Georgia" >Georgia</option> <option value="Germany" >Germany</option> <option value="Ghana" >Ghana</option> <option value="Greece" >Greece</option> <option value="Grenada" >Grenada</option> <option value="Guatemala" >Guatemala</option> <option value="Guinea" >Guinea</option> <option value="Guinea-Bissau" >Guinea-Bissau</option> <option value="Guyana" >Guyana</option> <option value="Haiti" >Haiti</option> <option value="Honduras" >Honduras</option> <option value="Hong Kong" >Hong Kong</option> <option value="Hungary" >Hungary</option> <option value="Iceland" >Iceland</option> <option value="India" >India</option> <option value="Indonesia" >Indonesia</option> <option value="Iran" >Iran</option> <option value="Iraq" >Iraq</option> <option value="Ireland" >Ireland</option> <option value="Israel" >Israel</option> <option value="Italy" >Italy</option> <option value="Jamaica" >Jamaica</option> <option value="Japan" >Japan</option> <option value="Jordan" >Jordan</option> <option value="Kazakhstan" >Kazakhstan</option> <option value="Kenya" >Kenya</option> <option value="Kiribati" >Kiribati</option> <option value="North Korea" >North Korea</option> <option value="South Korea" >South Korea</option> <option value="Kuwait" >Kuwait</option> <option value="Kyrgyzstan" >Kyrgyzstan</option> <option value="Laos" >Laos</option> <option value="Latvia" >Latvia</option> <option value="Lebanon" >Lebanon</option> <option value="Lesotho" >Lesotho</option> <option value="Liberia" >Liberia</option> <option value="Libya" >Libya</option> <option value="Liechtenstein" >Liechtenstein</option> <option value="Lithuania" >Lithuania</option> <option value="Luxembourg" >Luxembourg</option> <option value="Macedonia" >Macedonia</option> <option value="Madagascar" >Madagascar</option> <option value="Malawi" >Malawi</option> <option value="Malaysia" >Malaysia</option> <option value="Maldives" >Maldives</option> <option value="Mali" >Mali</option> <option value="Malta" >Malta</option> <option value="Marshall Islands" >Marshall Islands</option> <option value="Mauritania" >Mauritania</option> <option value="Mauritius" >Mauritius</option> <option value="Mexico" >Mexico</option> <option value="Micronesia" >Micronesia</option> <option value="Moldova" >Moldova</option> <option value="Monaco" >Monaco</option> <option value="Mongolia" >Mongolia</option> <option value="Montenegro" >Montenegro</option> <option value="Morocco" >Morocco</option> <option value="Mozambique" >Mozambique</option> <option value="Myanmar" >Myanmar</option> <option value="Namibia" >Namibia</option> <option value="Nauru" >Nauru</option> <option value="Nepal" >Nepal</option> <option value="Netherlands" >Netherlands</option> <option value="New Zealand" >New Zealand</option> <option value="Nicaragua" >Nicaragua</option> <option value="Niger" >Niger</option> <option value="Nigeria" >Nigeria</option> <option value="Norway" >Norway</option> <option value="Oman" >Oman</option> <option value="Pakistan" >Pakistan</option> <option value="Palau" >Palau</option> <option value="Panama" >Panama</option> <option value="Papua New Guinea" >Papua New Guinea</option> <option value="Paraguay" >Paraguay</option> <option value="Peru" >Peru</option> <option value="Philippines" >Philippines</option> <option value="Poland" >Poland</option> <option value="Portugal" >Portugal</option> <option value="Puerto Rico" >Puerto Rico</option> <option value="Qatar" >Qatar</option> <option value="Romania" >Romania</option> <option value="Russia" >Russia</option> <option value="Rwanda" >Rwanda</option> <option value="Saint Kitts and Nevis" >Saint Kitts and Nevis</option> <option value="Saint Lucia" >Saint Lucia</option> <option value="Saint Vincent and the Grenadines" >Saint Vincent and the Grenadines</option> <option value="Samoa" >Samoa</option> <option value="San Marino" >San Marino</option> <option value="Sao Tome and Principe" >Sao Tome and Principe</option> <option value="Saudi Arabia" >Saudi Arabia</option> <option value="Senegal" >Senegal</option> <option value="Serbia and Montenegro" >Serbia and Montenegro</option> <option value="Seychelles" >Seychelles</option> <option value="Sierra Leone" >Sierra Leone</option> <option value="Singapore" >Singapore</option> <option value="Slovakia" >Slovakia</option> <option value="Slovenia" >Slovenia</option> <option value="Solomon Islands" >Solomon Islands</option> <option value="Somalia" >Somalia</option> <option value="South Africa" >South Africa</option> <option value="Spain" >Spain</option> <option value="Sri Lanka" >Sri Lanka</option> <option value="Sudan" >Sudan</option> <option value="Suriname" >Suriname</option> <option value="Swaziland" >Swaziland</option> <option value="Sweden" >Sweden</option> <option value="Switzerland" >Switzerland</option> <option value="Syria" >Syria</option> <option value="Taiwan" >Taiwan</option> <option value="Tajikistan" >Tajikistan</option> <option value="Tanzania" >Tanzania</option> <option value="Thailand" >Thailand</option> <option value="Togo" >Togo</option> <option value="Tonga" >Tonga</option> <option value="Trinidad and Tobago" >Trinidad and Tobago</option> <option value="Tunisia" >Tunisia</option> <option value="Turkey" >Turkey</option> <option value="Turkmenistan" >Turkmenistan</option> <option value="Tuvalu" >Tuvalu</option> <option value="Uganda" >Uganda</option> <option value="Ukraine" >Ukraine</option> <option value="United Arab Emirates" >United Arab Emirates</option> <option value="United Kingdom" >United Kingdom</option> <option value="United States" >United States</option> <option value="Uruguay" >Uruguay</option> <option value="Uzbekistan" >Uzbekistan</option> <option value="Vanuatu" >Vanuatu</option> <option value="Vatican City" >Vatican City</option> <option value="Venezuela" >Venezuela</option> <option value="Vietnam" >Vietnam</option> <option value="Yemen" >Yemen</option> <option value="Zambia" >Zambia</option> <option value="Zimbabwe" >Zimbabwe</option> </select> <label for="Country">* Country</label> </strong></div> <p class="guidelines" id="guide_2"><small>Enter your mailing address</small></p> </li> <li id="li_3" > <label class="description" for="Phone">Phone Number </label> <div> <input id="Phone" name="Phone" class="element text medium" type="text" maxlength="255" value=""/> </div><p class="guidelines" id="guide_3"><small>Enter your phone number</small></p> </li> <li id="li_4" > <label class="description" for="Email">* Email </label> <div> <input id="Email" name="Email" class="element text medium" type="text" maxlength="255" value=""/> </div><p class="guidelines" id="guide_4"><small>Enter your email address</small></p> </li> <li class="buttons"> <input type="hidden" name="form_id" value="14512" /> <input value="Submit form" type="submit" /> </li> </ul> </form> <div id="footer"></div> </div> </body> </html> You can see the form in working order on the site located here http://www.+stxm+illing.com+/index.+...=conta+ct_us_1 PLEASE TAKE THE '+' OUT OF THE URL IN ORDER FOR IT TO WORK. Any feedback is greatly appreciated!! Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 20, 2009 Share Posted March 20, 2009 1. The URL you posted is not complete. Looks like part of the text after index. was replaced. 2. The "problem" you are having is related to your PHP code. Although you are asking for a JavaScript solution to validate before the data is posted, you still need to fix the PHP validation. The javascript validation is a nice to have feature, but you also need to validate server-side. I've made A LOT of changes. The page is called for displaying and processing the form, so if there is an error you can redisplay the form with the previosuly entered values. Here is the main page: <?php //Load the countries list require('email_countries.php'); function is_email($email) { $formatTest = '/^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } if (isset($_POST)) { //Create array variable for errors $errors = array(); $error_msg = ''; //Posted variables $Name = trim($_POST["Name"]); $Address = trim($_POST["Address"]); $Address2 = trim($_POST["Address2"]); $City = trim($_POST["City"]); $State = trim($_POST["State"]); $Country = trim($_POST["Country"]); $Zip = trim($_POST["Zip"]); $Phone = trim($_POST["Phone"]); $Email = trim($_POST["Email"]) ; //Validate the posted values if (empty($Name)) { $errors[] = "Name is required."; } if (empty($Address)) { $errors[] = "Address is required."; } if (empty($City)) { $errors[] = "City is required."; } if (empty($State)) { $errors[] = "State is required."; } if (!in_array($Country, $countries)) { $errors[] = "Country is required."; } if (empty($Email)) { $errors[] = "Email is required."; } else if (!is_email($Email)) { $errors[] = "Email is invalid."; } //Create error message if needed if (count($errors)>0) { $error_msg = "The following errors occured. Please correct and resubmit the form:<br />\n"; $error_msg .= "<ul><li>" . implode("</li>\n<li>", $errors) . "</li></ul>"; } else { //There were no errors send the data $recipient = "you@yourdomain.com"; $subject = "TEST SUBJECT"; $headers = 'From: <webmaster@example.com>' . "\r\n"; $forminfo = "Name: $Name\n"; $forminfo .= "Address: $Address\n"; $forminfo .= "Address2: $Address2\n"; $forminfo .= "City: $City\n"; $forminfo .= "State: $State\n"; $forminfo .= "Country: $Country\n"; $forminfo .= "Zip: $Zip\n"; $forminfo .= "Phone: $Phone\n"; $forminfo .= "Email: $Email\n"; $forminfo .= "Form Submitted: " . date('M d, Y') . "\n\n"; $formsend = mail("$recipient", "$subject", "$forminfo", "From: $email\r\nReply-to:$email"); //Create confirmation/error page if ($formsend) { $title = "Thank you"; $refresh = "<meta http-equiv=\"refresh\" content=\"5; URL=index.php?main_page=contact_us_1\">"; $content = "Thank you. You submission has been complete. Redirecting..."; } else //Problem sending email { $title = "Submission error"; $content = "There was a problem sending your submission."; $refresh = ""; } //Show email confirmation/error page require('email_confirm.php'); exit(); } } //Page was not posted or there was an error require('email_form.php'); ?> email_form.php <?php function createSelectOptions($options, $selectedOption) { foreach ($options as $option) { $selected = ($option == $selectedOption)? ' selected="selected"': ''; echo "<option value=\"$option\"$selected>$option</option>\n"; } } ?> <!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 Form</title> <link rel="stylesheet" type="text/css" href="view.css" media="all"> <script type="text/javascript" src="view.js"></script> <script type="text/javascript"> String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); } function validEmail(emailStr) { //Return true/false for valid/invalid email formatTest = /^[\w`\-=~!#$%^&*'+{}|'/?]+(\.[\w`\-=~!#$%^&*'+{}|'/?]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i lengthTest = /^(.{1,64})@(.{4,255})$/ return (formatTest.test(emailStr) && lengthTest.test(emailStr)); } function validateForm(formObj) { errors = new Array(); if(formObj.Name.value.trim()=='') { errors[errors.length] = 'Name is required'; } if(formObj.Address.value.trim()=='') { errors[errors.length] = 'Address is required'; } if(formObj.City.value.trim()=='') { errors[errors.length] = 'City is required'; } if(formObj.State.value.trim()=='') { errors[errors.length] = 'State is required'; } if(formObj.Country.value.trim()=='') { errors[errors.length] = 'Country is required'; } if(formObj.Email.value.trim()=='') { errors[errors.length] = 'Email is required'; } else if(!validEmail(formObj.Email.value.trim())) { errors[errors.length] = 'Email is invalid'; } if(errors.length>0) { errorMsg = 'The following errors occured:\n'; for (var i in errors) { errorMsg += ' - ' + errors[i] + '\n'; } alert(errorMsg); return false; } return true; } </script> </head> <body id="main_body" > <br /><br /> <?php echo $error_msg; ?> <div id="form_container"> <form id="form_14512" class="appnitro" method="post" action="" onsubmit="return validateForm(this)"> <div class="form_description"> <h2>Request A Brochure</h2> </div> <ul> <li id="li_1" > <label class="description" for="Name">* Name </label> <div> <input id="Name" name="Name" class="element text medium" type="text" maxlength="255" value="<?php echo $Name; ?>"/> </div> <p class="guidelines" id="guide_1"><small>Enter your name</small></p> </li> <li id="li_2" > <label class="description" for="element_2">* Mailing Address </label> <div> <input id="Address" name="Address" class="element text large" value="<?php echo $Address; ?>" type="text"> <strong><label for="Address">* Street Address</label></strong> </div> <div> <input id="Address2" name="Address2" class="element text large" value="<?php echo $Address2; ?>" type="text"> <label for="Address2">Address Line 2</label> </div> <div class="left"><strong> <input id="City" name="City" class="element text medium" value="<?php echo $City; ?>" type="text"> <strong><label for="City">* City</label></strong> </div> <div class="right"><strong> <input id="State" name="State" class="element text medium" value="<?php echo $State; ?>" type="text"> <label for="State">* State / Province / Region</label></strong> </div> <div class="left"><strong> <input id="Zip" name="Zip" class="element text medium" maxlength="15" value="<?php echo $Zip; ?>" type="text"> <strong><label for="Zip">* Postal / Zip Code</label></strong> </div> <div class="right"><strong> <select class="element select medium" id="Country" name="Country"> <option value="" selected="selected"></option> <?php createSelectOptions($countries, $Country); ?> </select> <strong><label for="Country">* Country</label></strong> </div> <p class="guidelines" id="guide_2"><small>Enter your mailing address</small></p> </li> <li id="li_3" > <label class="description" for="Phone">Phone Number </label> <div> <input id="Phone" name="Phone" class="element text medium" type="text" maxlength="255" value="<?php echo $Phone; ?>"/> </div> <p class="guidelines" id="guide_3"><small>Enter your phone number</small></p> </li> <li id="li_4" > <label class="description" for="Email">* Email </label> <div> <input id="Email" name="Email" class="element text medium" type="text" maxlength="255" value="<?php echo $Email; ?>"/> </div> <p class="guidelines" id="guide_4"><small>Enter your email address</small></p> </li> <li class="buttons"> <input type="hidden" name="form_id" value="14512" /> <input value="Submit form" type="submit" /> </li> </ul> </form> <div id="footer"></div> </div> </body> </html> email_confirm.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>$title</title>\n"; <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <?php echo $refresh; ?> <style type="text/css"> <!-- .style1 { font-size: 24px; font-weight: bold; } body { background-color: #ade4b0; } --> </style> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td><p align="left" class="style1"><?php echo $content; ?></p></td> </tr> </table> </body> </html> email_countries.php <?php $countries = array( 'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Brunei', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Central African Republic', 'Chad', 'Chile', 'China', 'Colombia', 'Comoros', 'Congo', 'Costa Rica', "Côte d'Ivoire", 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Fiji', 'Finland', 'France', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Greece', 'Grenada', 'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Honduras', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'North Korea', 'South Korea', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Mauritania', 'Mauritius', 'Mexico', 'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar', 'Romania', 'Russia', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia and Montenegro', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Taiwan', 'Tajikistan', 'Tanzania', 'Thailand', 'Togo', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican City', 'Venezuela', 'Vietnam', 'Yemen', 'Zambia', 'Zimbabwe' ); ?>[code] Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 20, 2009 Author Share Posted March 20, 2009 Wow!! First off thanks for the help. Looks like a LOT of work. Here is the link again sorry about that. http://www.stxm+illing.com/index.php?main_page=cont+act_us_1 Take the two '+' signs out and it should work. I have modified the code a little more to ALMOST get what I am happy with and it looks like this. <? $Name = $_POST["Name"]; $Address = $_POST["Address"]; $Address2 = $_POST["Address2"]; $City = $_POST["City"]; $State = $_POST["State"]; $Country = $_POST["Country"]; $Zip = $_POST["Zip"]; $Phone = $_POST["Phone"]; $Email = $_POST["Email"]; $today = date("M d, Y"); $recipient = "me@me.com.com"; $subject = "Request a Brochure"; $headers = 'From: <webmaster@example.com>' . "\r\n"; $forminfo = "Name: $Name\n Address: $Address\n Address2: $Address2\n City: $City\n State: $State\n Country: $Country\n Zip: $Zip\n Phone: $Phone\n Email: $Email\n Form Submitted: $today\n\n"; if (!empty($_POST['Name'])){ $msg = "Name; $_POST[Name] "; }else{ $Name = NULL; echo "Please fill out your first name.<br />"; } if (!empty($_POST['Address'])){ $msg = "Address; $_POST[Address] "; }else{ $Address = NULL; echo "Please fill out your Address.<br />"; } if (!empty($_POST['City'])){ $msg = "City; $_POST[City] "; }else{ $City = NULL; echo "Please fill out your City.<br />"; } if (!empty($_POST['State'])){ $msg = "State; $_POST[state] "; }else{ $State = NULL; echo "Please fill out your State.<br />"; } if (!empty($_POST['zipcode'])){ $msg = "zipcode; $_POST[zipcode] "; }else{ $zipcode = NULL; echo "Please fill out your zipcode.<br />"; } if (!empty($_POST['Country'])){ $msg = "Country; $_POST[Country] "; }else{ $Country = NULL; echo "Please fill out your Country.<br />"; } /* if (!empty($_POST['Name'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your first name.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['Address'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your Street Name.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['City'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your City.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['State'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your State.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } if (!empty($_POST['Country'])){ $msg = ""; }else{ $Name = NULL; echo "Please fill out your Country.<br />"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; exit; } */ if (preg_match ("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $Email)) { mail($toaddress,$subject,$headers,$message); //clear the variables $Name=''; $Email=''; echo ""; } else { echo nl2br ("Please enter correct email.\n"); echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php?main_page=contact_us_1\">"; } $formsend = mail("$recipient", "$subject", "$forminfo", "From: $email\r\nReply-to:$email"); ?> <!-- end PHP easy-form --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Make a Donation</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta http-equiv="refresh" content="5; URL=index.php?main_page=page_2"> <style type="text/css"> <!-- .style1 { font-size: 24px; font-weight: bold; } body { background-color: #ade4b0; } --> </style> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td> <p align="left" class="style1">Thank you. You submission has been complete.Redirecting...</p> </tr> </table> </td> </body> </html> There is a lot of extra code in here that I have commented out because I have been testing a lot of things. But I am "OK" with the form going to another page to validate and want I want it to do is check for empty fields and report an error if there are any. The way I have it now it does that but if I do not put an exit; at the end of the else on any of them it will report an error and go ahead and send the email with a blank form. It works best if i put exit; on the email because that is the last field so it works OK but if someone chooses to put there email and nothing else the form will go ahead and send. I see the validation that you have setup on this form seems to be javascript and a box comes up notifying the user that a field is missing information. I do not mean to be picky but I would rather it either be displayed on the same page in words instead of a pop up box or the way I have it setup now where it needs to go to another page to validate. I am just needing the required fields to work just a "little" better. Out of curiousity why do you have it with so many different php pages? Is it easier that way or what. Again thanks for all the help please don't think I do not appreciate the help I do not want to be "one of those guys" who gives you a hard time I am just trying to make it the best I can. One more point is if you go to the link I have put on the page. I love the way the form is positioned if I can keep the positioning of the form like this that would be great as well. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 20, 2009 Share Posted March 20, 2009 Did you even try the code I provided? There were a lot of issues in your code and it was easier to fix them than to explain it all. I really think you will be happier with the process flow I provided. Here is a short description of it: 1. When page loads, check to see if data was posted. If not, form is displayed. If yes, then go to step 2. 2. Validate ALL the posted data. The original code would stop on the first invalid piece of data. That is a major improvement in the code I provided. 3a. If validation passes, attempt to send the email. If successful go to confirmation page that then redirects. If send fails go to a friendly error page. 3b. If validation fails, display the form with the posted data The validation in the above process is the PHP validation. However, for users that have JavaScript enabled that should always pass since the form has JavaScript validation that will occur before the form is submitted. Now to address your questions: Changing the JavaScript validation to show an error on-sceen is simple enough. In fact, you can get really fancy with showing in-place errors next to each field. It's totally up to you how elaborate you want to make it. I made changes to the JS validatioin to show a simple alert and then display the errors on the page itself. You can remove the alert if need be. The reason I broke the code up into multiple files is for several reasons: 1. It is a best practice method. You should always break up logical sections of your code. Just as it is preferred to have your CSS in an external file so you can change the display for your site with one edit instead of having to esit each page individually. 2. Makes it easier to maintain. For example, I made the list of countries into an array insterad of writing out each <OPTION> tag directly in the form page. You can edit the list of countries by modifying that array. Or you could change to getting your listing of countries from a database or XML feed without changing your form. Also, if you wanted a confirmation or error page for the email submission you could do it much easier with having an external page than having to wade through all the code on one long page. If it were for my use I would have broken it up even further. I made some changes indicated below. Also, you ca see it all workign here http://damato.net/contact Note: I hard coded the path to the external js and css files on your site so I didn't have to copy them down. You should remove those hard-coded paths. index.php <?php //Load the countries list require('email_countries.php'); function is_email($email) { $formatTest = '/^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } if (isset($_POST['form_id'])) { //Create array variable for errors $errors = array(); $error_msg = ''; //Posted variables $Name = trim($_POST["Name"]); $Address = trim($_POST["Address"]); $Address2 = trim($_POST["Address2"]); $City = trim($_POST["City"]); $State = trim($_POST["State"]); $Country = trim($_POST["Country"]); $Zip = trim($_POST["Zip"]); $Phone = trim($_POST["Phone"]); $Email = trim($_POST["Email"]) ; //Validate the posted values if (empty($Name)) { $errors[] = "Name is required."; } if (empty($Address)) { $errors[] = "Address is required."; } if (empty($City)) { $errors[] = "City is required."; } if (empty($State)) { $errors[] = "State is required."; } if (!in_array($Country, $countries)) { $errors[] = "Country is required."; } if (empty($Email)) { $errors[] = "Email is required."; } else if (!is_email($Email)) { $errors[] = "Email is invalid."; } //Create error message if needed if (count($errors)>0) { $error_msg = "The following errors occured. Please correct and resubmit the form:<br />\n"; $error_msg .= "<ul><li>" . implode("</li>\n<li>", $errors) . "</li></ul>"; } else { //There were no errors send the data $recipient = "you@yourdomain.com"; $subject = "TEST SUBJECT"; $headers = 'From: <webmaster@example.com>' . "\r\n"; $forminfo = "Name: $Name\n"; $forminfo .= "Address: $Address\n"; $forminfo .= "Address2: $Address2\n"; $forminfo .= "City: $City\n"; $forminfo .= "State: $State\n"; $forminfo .= "Country: $Country\n"; $forminfo .= "Zip: $Zip\n"; $forminfo .= "Phone: $Phone\n"; $forminfo .= "Email: $Email\n"; $forminfo .= "Form Submitted: " . date('M d, Y') . "\n\n"; $formsend = mail("$recipient", "$subject", "$forminfo", "From: $email\r\nReply-to:$email"); //Create confirmation/error page if ($formsend) { $title = "Thank you"; $refresh = "<meta http-equiv=\"refresh\" content=\"5; URL=index.php?main_page=contact_us_1\">"; $content = "Thank you. You submission has been complete. Redirecting..."; } else //Problem sending email { $title = "Submission error"; $content = "There was a problem sending your submission."; $refresh = ""; } //Show email confirmation/error page require('email_confirm.php'); exit(); } } //Page was not posted or there was an error require('email_form.php'); ?> email_form.php <?php function createSelectOptions($options, $selectedOption) { foreach ($options as $option) { $selected = ($option == $selectedOption)? ' selected="selected"': ''; echo "<option value=\"$option\"$selected>$option</option>\n"; } } ?> <!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 Form</title> <link rel="stylesheet" type="text/css" href="http://www.stxmilling.com/view.css" media="all"> <script type="text/javascript" src="http://www.stxmilling.com/view.js"></script> <script type="text/javascript"> String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); } function validEmail(emailStr) { //Return true/false for valid/invalid email formatTest = /^[\w`\-=~!#$%^&*'+{}|'/?]+(\.[\w`\-=~!#$%^&*'+{}|'/?]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i lengthTest = /^(.{1,64})@(.{4,255})$/ return (formatTest.test(emailStr) && lengthTest.test(emailStr)); } function validateForm(formObj) { var errors = new Array(); if(formObj.Name.value.trim()=='') { errors[errors.length] = 'Name is required'; } if(formObj.Address.value.trim()=='') { errors[errors.length] = 'Address is required'; } if(formObj.City.value.trim()=='') { errors[errors.length] = 'City is required'; } if(formObj.State.value.trim()=='') { errors[errors.length] = 'State is required'; } if(formObj.Country.value.trim()=='') { errors[errors.length] = 'Country is required'; } if(formObj.Email.value.trim()=='') { errors[errors.length] = 'Email is required'; } else if(!validEmail(formObj.Email.value.trim())) { errors[errors.length] = 'Email is invalid'; } if(errors.length>0) { errorMsg = 'The following errors occured:\n'; errorMsg += '<ul><li>' + errors.join('</li><li>') + '</li></ul>'; document.getElementById('errors').innerHTML = errorMsg; alert('There were errors. Please correct and resubmit.'); return false; } return true; } </script> </head> <body id="main_body" > <br /><br /> <div id="form_container"> <div id="errors"><?php echo $error_msg; ?></div> <form id="form_14512" class="appnitro" method="post" action="" onsubmit="return validateForm(this)"> <div class="form_description"> <h2>Request A Brochure</h2> </div> <ul> <li id="li_1" > <label class="description" for="Name">* Name </label> <div> <input id="Name" name="Name" class="element text medium" type="text" maxlength="255" value="<?php echo $Name; ?>"/> </div> <p class="guidelines" id="guide_1"><small>Enter your name</small></p> </li> <li id="li_2" > <label class="description" for="element_2">* Mailing Address </label> <div> <input id="Address" name="Address" class="element text large" value="<?php echo $Address; ?>" type="text"> <strong><label for="Address">* Street Address</label></strong> </div> <div> <input id="Address2" name="Address2" class="element text large" value="<?php echo $Address2; ?>" type="text"> <label for="Address2">Address Line 2</label> </div> <div class="left"><strong> <input id="City" name="City" class="element text medium" value="<?php echo $City; ?>" type="text"> <strong><label for="City">* City</label></strong> </div> <div class="right"><strong> <input id="State" name="State" class="element text medium" value="<?php echo $State; ?>" type="text"> <label for="State">* State / Province / Region</label></strong> </div> <div class="left"><strong> <input id="Zip" name="Zip" class="element text medium" maxlength="15" value="<?php echo $Zip; ?>" type="text"> <strong><label for="Zip">* Postal / Zip Code</label></strong> </div> <div class="right"><strong> <select class="element select medium" id="Country" name="Country"> <option value="" selected="selected"></option> <?php createSelectOptions($countries, $Country); ?> </select> <strong><label for="Country">* Country</label></strong> </div> <p class="guidelines" id="guide_2"><small>Enter your mailing address</small></p> </li> <li id="li_3" > <label class="description" for="Phone">Phone Number </label> <div> <input id="Phone" name="Phone" class="element text medium" type="text" maxlength="255" value="<?php echo $Phone; ?>"/> </div> <p class="guidelines" id="guide_3"><small>Enter your phone number</small></p> </li> <li id="li_4" > <label class="description" for="Email">* Email </label> <div> <input id="Email" name="Email" class="element text medium" type="text" maxlength="255" value="<?php echo $Email; ?>"/> </div> <p class="guidelines" id="guide_4"><small>Enter your email address</small></p> </li> <li class="buttons"> <input type="hidden" name="form_id" value="14512" /> <input value="Submit form" type="submit" /> </li> </ul> </form> <div id="footer"></div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 21, 2009 Author Share Posted March 21, 2009 OK I actually really like this code and I appreciate you explaining the code. I am trying to learn more and more everyday. There is a slight problem which is not your fault I just failed to mention it. The site this form is going on is a zen cart e commerce site. I don't think straight php can be added into the pages. The form I have on there now is HTML and it calls the php files. Is this possible? Other than that I really do love it and appreciate your help. I could have never gotten this on my own. I have tried adding the code from the email_form.php into the zen cart page and it showed up blank. If i took just the information between the HTML tags it showed part of the form but not the entire thing. Any thoughts on working around this? Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 24, 2009 Author Share Posted March 24, 2009 does anybody know if there is a way to change this code to where it is pretty much the same but the actual form is in html so it can be put in a zen cart page? Please let me know. Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 24, 2009 Share Posted March 24, 2009 Although I've never worked with the Zen Cart framework, I have to believe there is a way to create dynamic pages with PHP as opposed to just flat HTML pages. But, to change it as you request is simple, just go with the format you had previously where you have a flat HTML file and a separate PHP page for processing. On the email_form.php code, strip out all the PHP code. You will then have to hard-code the select list and set the action of the form to the PHP processing page. The index.php page would be your processing page. Just remove the last line and instead include code to present an error page to the user (using the $error_msg string) and a link to return back to the form. Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 24, 2009 Author Share Posted March 24, 2009 OK I have spent quite a bit of time working with your script and I have got it to show in zen cart now. Which is GREAT news!!! However I had to do it a special way and so I am not sure exactly where the other files should go so that it can access them. Let me explain a little more. Zen cart is created in php but you can not go into the zen cart admin portion of the site and just copy and paste php code into the page. It only accepts html for the most part. So I created a new page with a program specifically for zen cart. Then I pasted the email_form.php code into it. The form shows up and the errors are displayed which is GREAT!! However the additional files like email_countries and so forth are not being located. Because if I tried to click on the drop down menu for the countries none are displayed. I have put the main code in the same directory but like I said the structure and the way to create a page in zen cart is very confusing. So I am not sure exactly where these additional files need to go. I am not sure if you could even help me further on this issue since you are not very familiar with zen cart. Sorry if i confused you it was rather hard to explain Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 24, 2009 Author Share Posted March 24, 2009 Maybe if you could tell me what is actually calling the certain files maybe that will help. I see that index.php calls all the files. Like require('email_confirm.php'); and so forth. I see it calls all of the except index.php which of course that is the file. I changed the email_form.php to tpl_request_a_brochure_default.php which is the name of the new email_form.php just to see if that helped but it didn't make a difference. I am so close I just need to figure out where to put these files in the zen cart structure so it can see them and work correctly. Any thoughts on this? I will continue to search around and try different things. I will update if I have anything new. Thanks again for all the help. Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 24, 2009 Author Share Posted March 24, 2009 Sorry for a THIRD POST IN A ROW lol but I got it!!! I needed to put index.php as the primary code...I did try that once but got a blank page but that was because I didn't have email_form.php uploaded because I was already using that code for the primary code. But anyways I got it all to work just the way I want and I can't Thank You enough! Not only did you help me but it forced me to learn more and more which is always a good thing. You can see the full working code here... stxm+illing.com/index.php?main_page=requ+est_a_broc+hure I know the redirect and so forth is not correct yet because the page name has been changed but I believe I can work out those minor changes. Again thank you so much for all your help!! Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 24, 2009 Share Posted March 24, 2009 OK, glad you kept working on it. I saw you responses and was going to rewite the code I laready provided when I actually "read" your third post. Glad it's all working. Please mark the thread as solved! Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 25, 2009 Author Share Posted March 25, 2009 OK I will mark the thread as solved although if I could ask a few more minor questions. I needed to add some check boxes for users to choose certain brochures. I have created the check boxes and spend quite a while trying to figure out ow to get it to send an email and show YES as in it is checked. But I have accomplished this. My only issue now is that I need to make sure at least one checkbox is selected. I do not know how to do this? I have searched but I am spending a lot of time and thought maybe you could quickly help. Also this is minor and not necessarily something that needs to be fixed but if simple I could use the help. The thank you message is kind of weird..for some reason it takes my right hand sidebar off completely. Is there an easier way to do a successful message other than this. And one more at the top of the page I have the word code in brackets I couldn't put what it was because it would actually create the code box in the forum. This is displayed on the main page and on the thank you page. I really do appreciate your help on this. Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 25, 2009 Author Share Posted March 25, 2009 I guess I should post the new changed files index.php <?php //Load the countries list require('email_countries.php'); function is_email($email) { $formatTest = '/^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } if (isset($_POST['form_id'])) { //Create array variable for errors $errors = array(); $error_msg = ''; //Posted variables $Name = trim($_POST["Name"]); $Address = trim($_POST["Address"]); $Address2 = trim($_POST["Address2"]); $City = trim($_POST["City"]); $State = trim($_POST["State"]); $Country = trim($_POST["Country"]); $Zip = trim($_POST["Zip"]); $Phone = trim($_POST["Phone"]); $Email = trim($_POST["Email"]) ; $Brochure1 = trim($_POST["Brochure1"]) ; $Brochure2 = trim($_POST["Brochure2"]) ; $Brochure3 = trim($_POST["Brochure3"]) ; $Brochure4 = trim($_POST["Brochure4"]) ; //Validate the posted values if (empty($Name)) { $errors[] = "Name is required."; } if (empty($Address)) { $errors[] = "Address is required."; } if (empty($City)) { $errors[] = "City is required."; } if (empty($State)) { $errors[] = "State is required."; } if (!in_array($Country, $countries)) { $errors[] = "Country is required."; } if (empty($Email)) { $errors[] = "Email is required."; } else if (!is_email($Email)) { $errors[] = "Email is invalid."; } //Create error message if needed if (count($errors)>0) { $error_msg = "The following errors occured. Please correct and resubmit the form:<br />\n"; $error_msg .= "<ul><li>" . implode("</li>\n<li>", $errors) . "</li></ul>"; } else { //There were no errors send the data $recipient = "you@you.com"; $subject = "test subject line"; $headers = 'From: <webmaster@example.com>' . "\r\n"; $forminfo = "Name: $Name\n"; $forminfo .= "Address: $Address\n"; $forminfo .= "Address2: $Address2\n"; $forminfo .= "City: $City\n"; $forminfo .= "State: $State\n"; $forminfo .= "Country: $Country\n"; $forminfo .= "Zip: $Zip\n"; $forminfo .= "Phone: $Phone\n"; $forminfo .= "Email: $Email\n\n"; $forminfo .= "this is brochure 1: $Brochure1\n"; $forminfo .= "this is brochure 2: $Brochure2\n"; $forminfo .= "this is brochure 3: $Brochure3\n"; $forminfo .= "this is brochure 4: $Brochure4\n"; $forminfo .= "Form Submitted: " . date('M d, Y') . "\n\n"; $formsend = mail("$recipient", "$subject", "$forminfo", "From: $email\r\nReply-to:$email"); //Create confirmation/error page if ($formsend) { $title = "Thank you"; $refresh = "<meta http-equiv=\"refresh\" content=\"5; URL=index.php?main_page=request_a_brochure\">"; $content = "Thank you. You submission has been complete. Redirecting..."; } else //Problem sending email { $title = "Submission error"; $content = "There was a problem sending your submission."; $refresh = ""; } //Show email confirmation/error page require('email_confirm.php'); exit(); } } //Page was not posted or there was an error require('email_form.php'); ?> email_form.php <?php function createSelectOptions($options, $selectedOption) { foreach ($options as $option) { $selected = ($option == $selectedOption)? ' selected="selected"': ''; echo "<option value=\"$option\"$selected>$option</option>\n"; } } ?> <!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>Request A Brochure</title> <link rel="stylesheet" type="text/css" href="http://www.stxmilling.com/view.css" media="all"> <script type="text/javascript" src="http://www.stxmilling.com/view.js"></script> <script type="text/javascript"> String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); } function validEmail(emailStr) { //Return true/false for valid/invalid email formatTest = /^[\w`\-=~!#$%^&*'+{}|'/?]+(\.[\w`\-=~!#$%^&*'+{}|'/?]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i lengthTest = /^(.{1,64})@(.{4,255})$/ return (formatTest.test(emailStr) && lengthTest.test(emailStr)); } function validateForm(formObj) { var errors = new Array(); if(formObj.Name.value.trim()=='') { errors[errors.length] = 'Name is required'; } if(formObj.Address.value.trim()=='') { errors[errors.length] = 'Address is required'; } if(formObj.Zip.value.trim()=='') { errors[errors.length] = 'Zip Code is required'; } if(formObj.City.value.trim()=='') { errors[errors.length] = 'City is required'; } if(formObj.State.value.trim()=='') { errors[errors.length] = 'State is required'; } if(formObj.Country.value.trim()=='') { errors[errors.length] = 'Country is required'; } if(formObj.Email.value.trim()=='') { errors[errors.length] = 'Email is required'; } else if(!validEmail(formObj.Email.value.trim())) { errors[errors.length] = 'Email is invalid'; } if(errors.length>0) { errorMsg = 'The following errors occured:\n'; errorMsg += '<ul><li>' + errors.join('</li><li>') + '</li></ul>'; document.getElementById('errors').innerHTML = errorMsg; alert('There were errors. Please correct and resubmit.'); return false; } return true; } </script> </head> <body id="main_body" > <br /><br /> <div id="form_container"> <div id="errors"><?php echo $error_msg; ?></div> <form id="form_14512" class="appnitro" method="post" action="" onsubmit="return validateForm(this)"> <div class="form_description"> <h2>Request A Brochure</h2> </div> <ul> <li id="li_1" > <label class="description" for="Name">* Name </label> <div> <input id="Name" name="Name" class="element text medium" type="text" maxlength="255" value="<?php echo $Name; ?>"/> </div> <p class="guidelines" id="guide_1"><small>Enter your name</small></p> </li> <li id="li_2" > <label class="description" for="element_2">* Mailing Address </label> <div> <input id="Address" name="Address" class="element text large" value="<?php echo $Address; ?>" type="text"> <strong><label for="Address">* Street Address</label></strong> </div> <div> <input id="Address2" name="Address2" class="element text large" value="<?php echo $Address2; ?>" type="text"> <label for="Address2">Address Line 2</label> </div> <div class="left"><strong> <input id="City" name="City" class="element text medium" value="<?php echo $City; ?>" type="text"> <strong><label for="City">* City</label></strong> </div> <div class="right"><strong> <input id="State" name="State" class="element text medium" value="<?php echo $State; ?>" type="text"> <label for="State">* State / Province / Region</label></strong> </div> <div class="left"><strong> <input id="Zip" name="Zip" class="element text medium" maxlength="15" value="<?php echo $Zip; ?>" type="text"> <strong><label for="Zip">* Postal / Zip Code</label></strong> </div> <div class="right"><strong> <select class="element select medium" id="Country" name="Country"> <option value="" selected="selected"></option> <?php createSelectOptions($countries, $Country); ?> </select> <strong><label for="Country">* Country</label></strong> </div> <p class="guidelines" id="guide_2"><small>Enter your mailing address</small></p> </li> <li id="li_3" > <label class="description" for="Phone">Phone Number </label> <div> <input id="Phone" name="Phone" class="element text medium" type="text" maxlength="255" value="<?php echo $Phone; ?>"/> </div> <p class="guidelines" id="guide_3"><small>Enter your phone number</small></p> </li> <li id="li_4" > <label class="description" for="Email">* Email </label> <div> <input id="Email" name="Email" class="element text medium" type="text" maxlength="255" value="<?php echo $Email; ?>"/> </div> <p class="guidelines" id="guide_4"><small>Enter your email address</small></p> </li> <input name='Brochure1' type='checkbox' id='Brochure1'value='Yes' <?php if($_POST['on'] == 'Yes') echo "Yes" ?> >Brochure1 <br /> <input name='Brochure2' type='checkbox' id='Brochure2'value='Yes' <?php if($_POST['on'] == 'Yes') echo "Yes" ?>>Brochure2 <br /> <input name='Brochure3' type='checkbox' id='Brochure3'value='Yes' <?php if($_POST['on'] == 'Yes') echo "Yes" ?>>Brochure3 <br /> <input name='Brochure4' type='checkbox' id='Brochure4'value='Yes' <?php if($_POST['on'] == 'Yes') echo "Yes" ?>>Brochure4 <br /> <li class="buttons"> <input type="hidden" name="form_id" value="14512" /> <input value="Submit form" type="submit" /> </li> </ul> </form> <div id="footer"></div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 25, 2009 Share Posted March 25, 2009 For the checkboxes, I would do this. In the HTML form <input type="checkbox" name="selections[]" value="item 1" /> Item 1<br /> <input type="checkbox" name="selections[]" value="item 2" /> Item 2<br /> <input type="checkbox" name="selections[]" value="item 3" /> Item 3<br /> Then within the Javascript tags add this function function optionSelected(checkGroup) { for(var i=0; i<checkGroup.length; i++) { if (checkGroup[i].checked) { return true; } } return false; } and within the form validation add this after the "else if" line if(f!optionSelected(formObj['selections[]'])) { errors[errors.length] = 'Must select at least one option.'; } Now, in the PHP validation code add this in the PHP validation section, also after the "else if" line if(!isset($_POST['selections'])) { $errors[] = "Must select at least one option."; } Finally, within the code the builds the email add this where you want the output to be, if you only need the selected options to be displayed you could use this $forminfo .= "Selected options:\n"; foreach($_POST['selections'] as $option) { $forminfo .= " - " . $selection . "\n"; } Or if you need all the options printed with a yes/no, then you would have to create an array of all the options in PHP and do this: $optionsList = array('Option 1','Option 2','Option 3'); foreach($optionsList as $option) { $forminfo .= "$option: " . ((in_array($option, $_POST['options']))?'Yes':'No') . "\n"; } For the problem with the thank you page, I think you would need to either build a flat HTML page that includes that left bar or you would need to figure out how to include the welcome page into one of the Zen pages. Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 25, 2009 Author Share Posted March 25, 2009 I am having trouble getting it to fully work the way it should. Right now it sends and email but the check boxes all say no and it doesn't matter whether they are checked or not. Also for some reason one of the pieces of code i put in prevents the javascript box from coming on. The way I have it right now is it does require a user to fill in at least one check box but it is not displaying properly in the email. If you could see if I have all the code placed properly. I had a hard time trying to figure out which document and which place they needed to go. I think I have it correct but I must have missed something... index.php <?php //Load the countries list require('email_countries.php'); function is_email($email) { $formatTest = '/^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } if (isset($_POST['form_id'])) { //Create array variable for errors $errors = array(); $error_msg = ''; //Posted variables $Name = trim($_POST["Name"]); $Address = trim($_POST["Address"]); $Address2 = trim($_POST["Address2"]); $City = trim($_POST["City"]); $State = trim($_POST["State"]); $Country = trim($_POST["Country"]); $Zip = trim($_POST["Zip"]); $Phone = trim($_POST["Phone"]); $Email = trim($_POST["Email"]) ; $Brochure1 = trim($_POST["Brochure1"]) ; $Brochure2 = trim($_POST["Brochure2"]) ; $Brochure3 = trim($_POST["Brochure3"]) ; $Brochure4 = trim($_POST["Brochure4"]) ; //Validate the posted values if (empty($Name)) { $errors[] = "Name is required."; } if (empty($Address)) { $errors[] = "Address is required."; } if (empty($City)) { $errors[] = "City is required."; } if (empty($State)) { $errors[] = "State is required."; } if (!in_array($Country, $countries)) { $errors[] = "Country is required."; } if (empty($Email)) { $errors[] = "Email is required."; } else if (!is_email($Email)) { $errors[] = "Email is invalid."; } if(!isset($_POST['selections'])) { $errors[] = "Must select at least one option."; } //Create error message if needed if (count($errors)>0) { $error_msg = "The following errors occured. Please correct and resubmit the form:<br />\n"; $error_msg .= "<ul><li>" . implode("</li>\n<li>", $errors) . "</li></ul>"; } else { //There were no errors send the data $recipient = "test@test.com"; $subject = "Request A Brochure"; $headers = 'From: <webmaster@example.com>' . "\r\n"; $forminfo = "Name: $Name\n"; $forminfo .= "Address: $Address\n"; $forminfo .= "Address2: $Address2\n"; $forminfo .= "City: $City\n"; $forminfo .= "State: $State\n"; $forminfo .= "Country: $Country\n"; $forminfo .= "Zip: $Zip\n"; $forminfo .= "Phone: $Phone\n"; $forminfo .= "Email: $Email\n\n"; $optionsList = array('Option 1','Option 2','Option 3'); foreach($optionsList as $option) { $forminfo .= "$option: " . ((in_array($option, $_POST['options']))?'Yes':'No') . "\n"; } $forminfo .= "Form Submitted: " . date('M d, Y') . "\n\n"; $formsend = mail("$recipient", "$subject", "$forminfo", "From: $email\r\nReply-to:$email"); //Create confirmation/error page if ($formsend) { $title = "Thank you"; $refresh = "<meta http-equiv=\"refresh\" content=\"5; URL=index.php?main_page=request_a_brochure\">"; $content = "Thank you. You submission has been complete. Redirecting..."; } else //Problem sending email { $title = "Submission error"; $content = "There was a problem sending your submission."; $refresh = ""; } //Show email confirmation/error page require('email_confirm.php'); exit(); } } //Page was not posted or there was an error require('email_form.php'); ?> email_form.php <?php function createSelectOptions($options, $selectedOption) { foreach ($options as $option) { $selected = ($option == $selectedOption)? ' selected="selected"': ''; echo "<option value=\"$option\"$selected>$option</option>\n"; } } ?> <!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>Request A Brochure</title> <link rel="stylesheet" type="text/css" href="http://www.stxmilling.com/view.css" media="all"> <script type="text/javascript" src="http://www.stxmilling.com/view.js"></script> <script type="text/javascript"> String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); } function validEmail(emailStr) { //Return true/false for valid/invalid email formatTest = /^[\w`\-=~!#$%^&*'+{}|'/?]+(\.[\w`\-=~!#$%^&*'+{}|'/?]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i lengthTest = /^(.{1,64})@(.{4,255})$/ return (formatTest.test(emailStr) && lengthTest.test(emailStr)); } function validateForm(formObj) { var errors = new Array(); if(formObj.Name.value.trim()=='') { errors[errors.length] = 'Name is required'; } if(formObj.Address.value.trim()=='') { errors[errors.length] = 'Address is required'; } if(formObj.Zip.value.trim()=='') { errors[errors.length] = 'Zip Code is required'; } if(formObj.City.value.trim()=='') { errors[errors.length] = 'City is required'; } if(formObj.State.value.trim()=='') { errors[errors.length] = 'State is required'; } if(formObj.Country.value.trim()=='') { errors[errors.length] = 'Country is required'; } if(formObj.Email.value.trim()=='') { errors[errors.length] = 'Email is required'; } else if(!validEmail(formObj.Email.value.trim())) { errors[errors.length] = 'Email is invalid'; } if(f!optionSelected(formObj['selections[]'])) { errors[errors.length] = 'Must select at least one option.'; } if(errors.length>0) { errorMsg = 'The following errors occured:\n'; errorMsg += '<ul><li>' + errors.join('</li><li>') + '</li></ul>'; document.getElementById('errors').innerHTML = errorMsg; alert('There were errors. Please correct and resubmit.'); return false; } return true; } function optionSelected(checkGroup) { for(var i=0; i<checkGroup.length; i++) { if (checkGroup[i].checked) { return true; } } return false; } </script> </head> <body id="main_body" > <br /><br /> <div id="form_container"> <div id="errors"><?php echo $error_msg; ?></div> <form id="form_14512" class="appnitro" method="post" action="" onsubmit="return validateForm(this)"> <div class="form_description"> <h2>Request A Brochure</h2> </div> <ul> <li id="li_1" > <label class="description" for="Name">* Name </label> <div> <input id="Name" name="Name" class="element text medium" type="text" maxlength="255" value="<?php echo $Name; ?>"/> </div> <p class="guidelines" id="guide_1"><small>Enter your name</small></p> </li> <li id="li_2" > <label class="description" for="element_2">* Mailing Address </label> <div> <input id="Address" name="Address" class="element text large" value="<?php echo $Address; ?>" type="text"> <strong><label for="Address">* Street Address</label></strong> </div> <div> <input id="Address2" name="Address2" class="element text large" value="<?php echo $Address2; ?>" type="text"> <label for="Address2">Address Line 2</label> </div> <div class="left"><strong> <input id="City" name="City" class="element text medium" value="<?php echo $City; ?>" type="text"> <strong><label for="City">* City</label></strong> </div> <div class="right"><strong> <input id="State" name="State" class="element text medium" value="<?php echo $State; ?>" type="text"> <label for="State">* State / Province / Region</label></strong> </div> <div class="left"><strong> <input id="Zip" name="Zip" class="element text medium" maxlength="15" value="<?php echo $Zip; ?>" type="text"> <strong><label for="Zip">* Postal / Zip Code</label></strong> </div> <div class="right"><strong> <select class="element select medium" id="Country" name="Country"> <option value="" selected="selected"></option> <?php createSelectOptions($countries, $Country); ?> </select> <strong><label for="Country">* Country</label></strong> </div> <p class="guidelines" id="guide_2"><small>Enter your mailing address</small></p> </li> <li id="li_3" > <label class="description" for="Phone">Phone Number </label> <div> <input id="Phone" name="Phone" class="element text medium" type="text" maxlength="255" value="<?php echo $Phone; ?>"/> </div> <p class="guidelines" id="guide_3"><small>Enter your phone number</small></p> </li> <li id="li_4" > <label class="description" for="Email">* Email </label> <div> <input id="Email" name="Email" class="element text medium" type="text" maxlength="255" value="<?php echo $Email; ?>"/> </div> <p class="guidelines" id="guide_4"><small>Enter your email address</small></p> </li> <input type="checkbox" name="selections[]" value="item 1" /> Item 1<br /> <input type="checkbox" name="selections[]" value="item 2" /> Item 2<br /> <input type="checkbox" name="selections[]" value="item 3" /> Item 3<br /> <li class="buttons"> <input type="hidden" name="form_id" value="14512" /> <input value="Submit form" type="submit" /> </li> </ul> </form> <div id="footer"></div> </div> </body> </html> If you could help me out to get this a little more perfected on my end then I will be so happy and through bothering you. Thanks again I really really really do appreciate your time and help with this Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 26, 2009 Share Posted March 26, 2009 It was a typo on my part, but which I would expect you to catch. As stated in my signature "I do not always test the code I provide..." There are two problems: one, the form is using the field name "selections" and part of the code in the processing script is using "options". Also, the $optionsList array used inthe processign script is using different values than used on the form. Replace the corresponding sectino in the PHP code with this: //These must match EXACTLY the values of the checkbox values $optionsList = array('item 1','item 2','item 3'); foreach($optionsList as $option) { $forminfo .= "$option: " . ((in_array($option, $_POST['selections']))?'Yes':'No') . "\n"; } Quote Link to comment Share on other sites More sharing options...
welchyboy Posted March 26, 2009 Author Share Posted March 26, 2009 I understand you do not always test the code you provide and I can understand you are very busy. I am not complaining by any means just very new as I stated still learning my way around php and html for that matter. I have got the code working just the way I like and modified the email which is sent out. For some reason I do not get the javascript pop up box but all the php validation is working and that is all I need. I THANK YOU so MUCH for all the help and really walking me through this form. The customers I have provided this form are very pleased and I have learned a great deal as well. I am marking this thread as successful Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.