mdez13 Posted June 11, 2012 Share Posted June 11, 2012 hey guys, if you goto getrapidcharge.com/rcsite/contact.php, i've implemented a contact form. i'dd like to add a field to the top where i can choose a department from a dropdown list, which in turn will send the email to the selected departments address. This is currently whats processing the form. any help would be greatly appreciated. i thought about an array but im not sure how i'd implement it. <?php session_start(); $errors = ''; $name = ''; $visitor_email = ''; $visitor_telephone = ''; $visitor_subject = ''; $visitor_hear = ''; $user_message = ''; $your_email ='[email protected]';// <<=== update to your email address if(isset($_POST['submit'])) { $name = $_POST['name']; $visitor_email = $_POST['email']; $visitor_telephone = $_POST['telephone']; $visitor_subject = $_POST['subject']; $visitor_hear = $_POST['hear']; $user_message = $_POST['message']; ///------------Do Validations------------- if(empty($name)||empty($visitor_email)) { $errors .= "\n Please Fill out Required Fields. "; } if(IsInjected($visitor_email)) { $errors .= "\n Bad email entry!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n <div class='captcha-miss'>The captcha code does not match!</div>"; } if(empty($errors)) { //send the email $to = $your_email; $subject="Form Submission From Contact Page"; $from = $your_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "A user $name submitted the contact form:\n". "Name: $name\n". "Email: $visitor_email \n". "Telephone: $visitor_telephone \n". "Subject: $visitor_subject \n". "How did you hear about us? $visitor_hear \n". "Message: \n ". "$user_message\n". "IP: $ip\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; mail($to, $subject, $body,$headers); $success = ''; $success .= 'Your message sent!'; //header('Location: contact.php'); } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?> heres the HTML/JS <?php if(!empty($errors)){ echo "<p class='err'>".nl2br($errors)."</p>"; } ?> <div id='contact_form_errorloc' class='err'></div> <form method="POST" name="contact_form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> <p> <label for='name'><strong>Name</strong></label><br> <input type="text" name="name" value='<?php echo htmlentities($name) ?>' class="contact_input"> </p> <p> <label for='email'><strong>Email</strong></label><br> <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>' class="contact_input"> </p> <p> <label for='telephone'><strong>Telephone</strong></label><br> <input type="text" name="telephone" value='<?php echo htmlentities($visitor_telephone) ?>' class="contact_input"> </p> <p> <label for='subject'><strong>Subject</strong></label><br> <input type="text" name="subject" value='<?php echo htmlentities($visitor_subject) ?>' class="contact_input"> </p> <p> <label for='hear'><strong>How did you hear about us?</strong></label><br> <input type="text" name="hear" value='<?php echo htmlentities($visitor_hear) ?>' class="contact_input"> </p> <p> <label for='message'><strong>Message</strong></label> <br> <textarea name="message" rows=8 cols=30 style="border:none;"><?php echo htmlentities($user_message) ?></textarea> </p> <p> <img src="script/captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br /> <label for='message'>Enter the code above here :</label><br> <input id="6_letters_code" name="6_letters_code" type="text" style="border:none;"><br /> <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p> <input type="submit" value="Submit" name='submit' class="rapidcharge-contact-btn"> </form> <script type="text/javascript"> var frmvalidator = new Validator("contact_form"); //remove the following two lines if you like error message box popups frmvalidator.EnableOnPageErrorDisplaySingleBox(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("name","req","You forgot to enter your name!"); frmvalidator.addValidation("email","req","You forgot to enter your email!"); frmvalidator.addValidation("email","email","Please enter a valid email address"); frmvalidator.addValidation("subject","req","You forgot to enter a subject!"); frmvalidator.addValidation("hear","req","How did you hear about us?"); frmvalidator.addValidation("message","req","No message? hrmph!"); </script> <script type="text/javascript"> function refreshCaptcha() { var img = document.images['captchaimg']; img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/264014-contact-form-adjustment/ Share on other sites More sharing options...
insidus Posted June 11, 2012 Share Posted June 11, 2012 For a drop down form, first create the select drop down list in html <select name="email_list"> <option value="support">Support</option> <option value="pay">Pay</option> </select> Then for the PHP side, after you submit the form and retrieve the POST data, you'd do $department = $_POST['email_list']; echo $department; // to make sure it worked Hope that helped /insidus Quote Link to comment https://forums.phpfreaks.com/topic/264014-contact-form-adjustment/#findComment-1353018 Share on other sites More sharing options...
Psycho Posted June 11, 2012 Share Posted June 11, 2012 To add to insidus's explanation, you do NOT want to use the actual emails as the values in the select list - otherwise they could be target for spam by any spider that crawls your site. So an array is a good solution - assuming you don't have a database for this information. I typically have my form pages submit to themselves to make validation easier and in that case you could define the array in that page. But, if you are going to have separate pages for the form and the processing then you would want to store the array in a separate file and include it in both pages. I am not going to read through your code to make the changes, but I will provide the guidance on how it can be implemented. 1. Create a file with just the array defined, e.g. contacts.php, using the index keys as the description and the value as the email address $contactAry = array( 'department1' => 'emailaddress1.domain.com', 'department2' => 'emailaddress2.domain.com', 'department3' => 'emailaddress3.domain.com', 'department4' => 'emailaddress4.domain.com', ); 2. Include the email address file in the form page and add a process to build the select list using the department name as the value include('contacts.php'); echo "<select name='department'>\n"; foreach($contactAry as $department => $email) { echo "<option value='$department'>$department</option>\n"; } echo "</select>\n"; 3. Lastly, use the same array to validate that the passed value is valid and translate it into the appropriate email address to use include('contacts.php'); if(!array_key_exists($_POST['department'], $contactAry)) { echo "Invalid department"; } else { $email = $contactAry[$_POST['department']]; } Quote Link to comment https://forums.phpfreaks.com/topic/264014-contact-form-adjustment/#findComment-1353032 Share on other sites More sharing options...
mdez13 Posted June 12, 2012 Author Share Posted June 12, 2012 i've implemented this using the forms processing variable of $your_email, but i get this error: Warning: Invalid argument supplied for foreach(). Thoughts? if(isset($_POST['submit'])) { $contactAry = array( 'Sales' => '[email protected]', 'Advertising' => '[email protected]', ); if(!array_key_exists($_POST['department'], $contactAry)) { echo "Invalid department"; } else { $your_email = $contactAry[$_POST['department']]; } .... <select name="destemail"> <?php echo "<select name='department'>\n"; foreach($contactAry as $department => $your_email) { echo "<option value='$department'>$department</option>\n"; } echo "</select>\n"; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/264014-contact-form-adjustment/#findComment-1353266 Share on other sites More sharing options...
mrMarcus Posted June 12, 2012 Share Posted June 12, 2012 Probably because your array is within a condition. Move it outside your if(isset($_POST['submit'])) condition, and the error should go away. Quote Link to comment https://forums.phpfreaks.com/topic/264014-contact-form-adjustment/#findComment-1353294 Share on other sites More sharing options...
mdez13 Posted June 13, 2012 Author Share Posted June 13, 2012 i assumed that could be the problem. now it just pushes down my headers and display text outside of the select. http://getrapidcharge.com/rcsite/contact.php Quote Link to comment https://forums.phpfreaks.com/topic/264014-contact-form-adjustment/#findComment-1353546 Share on other sites More sharing options...
mdez13 Posted June 14, 2012 Author Share Posted June 14, 2012 this is what i have now. it works to an extent. it will send the message but also display 'invalid department' at the same time, thoughts? http://getrapidcharge.com/rcsite/contact.php <?php session_start(); $errors = ''; $name = ''; $visitor_email = ''; $visitor_telephone = ''; $visitor_subject = ''; $visitor_hear = ''; $user_message = ''; $your_email = ''; $department = ''; $contactAry = array( 'Sales' => '[email protected]', 'Advertising' => '[email protected]', ); if(isset($_POST['submit'])) { if(!array_key_exists($_POST['department'], $contactAry)) { $invaliddept = ''; $invaliddept .= "<p class='fail' align='center'>Invalid department</p>"; } else { $your_email = $contactAry[$_POST['department']]; } $name = $_POST['name']; $visitor_email = $_POST['email']; $visitor_telephone = $_POST['telephone']; $visitor_subject = $_POST['subject']; $visitor_hear = $_POST['hear']; $user_message = $_POST['message']; $your_email = $_POST['department']; ///------------Do Validations------------- if(empty($name)||empty($visitor_email)) { $errors .= "\n<div class='captcha-miss'>Please Fill out Required Fields.</div>"; } if(IsInjected($visitor_email)) { $errors .= "\n Bad email entry!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n <div class='captcha-miss'>The captcha code does not match!</div>"; } if(empty($errors)) { //send the email $to = $your_email; $subject="Form Submission From Contact Page"; $from = $your_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "$name submitted the contact form for $department:\n". "Name: $name\n". "Email: $visitor_email \n". "Telephone: $visitor_telephone \n\n". "How did you hear about us? $visitor_hear \n\n". "Subject: $visitor_subject \n\n". "Message: \n ". "$user_message\n". "IP: $ip\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; mail($to, $subject, $body,$headers); $success = ''; $success .= "<p class='success' align='center'>Your message sent!</p>"; } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?> <?php echo $success; ?> <?php echo $invaliddept; ?> <?php if(!empty($errors)){ echo "<p class='err'>".nl2br($errors)."</p>"; } ?> <div id='contact_form_errorloc' class='err'></div> <form method="POST" name="contact_form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> <p> <label for='department'><strong>Department</strong></label><br> <select name="department"> <option>Select Department...</option> <?php foreach($contactAry as $department => $your_email) { echo "<option value='$department'>$department</option>\n"; } ?> </select> </p> <p> <label for='name'><strong>Name</strong></label><br> <input type="text" name="name" value='<?php echo htmlentities($name) ?>' class="contact_input shadow"> </p> <p> <label for='email'><strong>Email</strong></label><br> <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>' class="contact_input shadow"> </p> <p> <label for='telephone'><strong>Telephone</strong></label><br> <input type="text" name="telephone" value='<?php echo htmlentities($visitor_telephone) ?>' class="contact_input shadow"> </p> <p> <label for='hear'><strong>How did you hear about us?</strong></label><br> <input type="text" name="hear" value='<?php echo htmlentities($visitor_hear) ?>' class="contact_input shadow"> </p> <p> <label for='subject'><strong>Subject</strong></label><br> <input type="text" name="subject" value='<?php echo htmlentities($visitor_subject) ?>' class="contact_input shadow"> </p> <p> <label for='message'><strong>Message</strong></label> <br> <textarea name="message" rows=8 cols=35 style="border:none;width:310px;" class="shadow"><?php echo htmlentities($user_message) ?></textarea> </p> <p> <img src="script/captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br /> <label for='message'>Enter the code above here:</label><br> <input id="6_letters_code" name="6_letters_code" type="text" style="border:none;" class="shadow"><br /> <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p> <input type="submit" value="Submit" name='submit' class="rapidcharge-contact-btn"> </form> <script type="text/javascript"> var frmvalidator = new Validator("contact_form"); //remove the following two lines if you like error message box popups frmvalidator.EnableOnPageErrorDisplaySingleBox(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("department","req","You forgot to choose a department!"); frmvalidator.addValidation("name","req","You forgot to enter your name!"); frmvalidator.addValidation("email","req","You forgot to enter your email!"); frmvalidator.addValidation("subject","req","You forgot to enter a subject!"); frmvalidator.addValidation("hear","req","How did you hear about us?"); frmvalidator.addValidation("message","req","No message? hrmph!"); </script> <script type="text/javascript"> function refreshCaptcha() { var img = document.images['captchaimg']; img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/264014-contact-form-adjustment/#findComment-1353871 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.