lazyserv Posted March 22, 2013 Share Posted March 22, 2013 Dear all,I'm having problem in my send contact php file. I wanted the email to send based on the subject the visitor choose, for example: Padicure Booking subject the email will send to booking@example.com while General Enquiry subject will send to info@example.combelow is my php code. <?php function emailswitch() { $to = array( 'Pedicure Price Inquiry' => 'info@example.com', 'Manicure Price Inquiry' => 'info@example.com', 'Booking Time' => 'booking@example.com', ); $default = 'booking@example.com'; return isset( $_POST['field_?'] ) && !empty($to[ $_POST['field_?'] ]) ? $to[ $_POST['field_?'] ]: $default; $from = $_POST['email']; } $from_name = $_POST['name']; $subject = $_POST['category']; // collect data $body = ""; foreach($_POST as $key => $val) { if($key != 'captcha') $body .= ucfirst($key).": ".$val."\r\n"; } // construct MIME PLAIN Email headers $header = "MIME-Version: 1.0\n"; $header .= "Content-type: text/plain; charset=utf-8\n"; $header .= "From: $from_name <$from>\r\nReply-To: $from_name <$from>\r\nReturn-Path: <$from>\r\n"; // send email $mail_sent = mail($to, $subject, $body, $header); ?> and below is my html code: <div id="contact_form"> <form action="sendContact.php" method="post" onsubmit="return sendContact();"> <p><label id="lname" for="name">Full name:</label> <input id="name" class="text" name="name" onblur="input_blur('name');" onfocus="input_focus('name');" type="text" /></p> <p><label id="lemail" for="email">Email address:</label> <input id="email" class="text" name="email" onblur="input_blur('email');" onfocus="input_focus('email');" type="text" /></p> <div class="x"></div> <p id="email-error" class="error">You must enter your email address!</p> <p><label id="lcategory" for="category">Category:</label> <select id="category" name="category" onblur="input_blur('category');" onfocus="input_focus('category');"> <option value="Pedicure Price Inquiry">Pedicure Price Inquiry</option> <option value="Manicure Price Inquiry">Manicure Price Inquiry</option> <option value="Booking Time">Booking Time</option></select></p> <p><label id="lmessage" for="message">Message:</label> <textarea id="message" name="message" onblur="input_blur('message');" onfocus="input_focus('message');" rows="" cols=""></textarea></p> <div class="x"></div> <p id="message-error" class="error">You must enter your message!</p> <p><label id="lcaptcha" for="captcha"></label> <input id="captcha" class="text" name="captcha" onblur="input_blur('captcha');" onfocus="input_focus('captcha');" type="text" /></p> <div class="x"></div> <p id="captcha-error" class="error">Are you sure about your calculations?</p> <script type="text/javascript"> generate_captcha('lcaptcha'); </script> <div class="x"></div> <input class="submit" name="send_contact" type="submit" value="Send" /><input class="submit" type="reset" value="Reset" /> </form> </div> <div id="message_sent" style="display: none;"> <h1>Your message has been sent</h1> <p>We'll contact you as soon as possible.</p> <p>You can now <a href="./">go back</a> to home page.</p> </div> <!-- end of contact form --></div> the problem is this code always trigger to default email and not the specific email based on the option the user chose. please advise where the code when wrong... by the way i'm still in learning stage any feedback and comment are much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/ Share on other sites More sharing options...
BagoZonde Posted March 22, 2013 Share Posted March 22, 2013 I don't get it a part with emailswitch(), what's that "field_?"???! Here's some piece solution: $to = array( 'Pedicure Price Inquiry' => 'info@example.com', 'Manicure Price Inquiry' => 'info@example.com', 'Booking Time' => 'booking@example.com', ); $send_to_email=$to[$_POST['category']]; if (!$send_to_email){ $send_to_email=$to['Booking Time']; //default e-mail } Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420286 Share on other sites More sharing options...
DaveyK Posted March 22, 2013 Share Posted March 22, 2013 (edited) I think that that code will return in undefined index error if the mail is not in the array. I would suggest this: $to = array( 'Pedicure Price Inquiry' => 'info@example.com', 'Manicure Price Inquiry' => 'info@example.com', 'Booking Time' => 'booking@example.com', ); $send_to_email= (isset($to[$_POST['category']])) ? $to[$_POST['category']]: $to['Booking Time']; EDIT: Markup Edited March 22, 2013 by DaveyK Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420300 Share on other sites More sharing options...
BagoZonde Posted March 22, 2013 Share Posted March 22, 2013 (edited) Yeah, it will return only Notice in that particular situation but I'm not bother with that, in production state it's safe to hide error_reporting at all. For sure my example works properly, and your is more elegant. Edited March 22, 2013 by BagoZonde Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420311 Share on other sites More sharing options...
lazyserv Posted March 24, 2013 Author Share Posted March 24, 2013 I think that that code will return in undefined index error if the mail is not in the array. I would suggest this: $to = array( 'Pedicure Price Inquiry' => 'info@example.com', 'Manicure Price Inquiry' => 'info@example.com', 'Booking Time' => 'booking@example.com', ); $send_to_email= (isset($to[$_POST['category']])) ? $to[$_POST['category']]: $to['Booking Time']; EDIT: Markup <?php function emailswitch( $key ) { $to = array( 'Pedicure Price Inquiry' => 'info@gmail.com', 'Manicure Price Inquiry' => 'info@gmail.com', 'Booking Time' => 'booking@gmail.com' ); $send_to_email= (isset($to[$_POST['category']])) ? $to[$_POST['category']]: $to['Booking Time']; } $from_name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; // collect data $body = ""; foreach($_POST as $key => $val) { if($key != 'captcha') $body .= ucfirst($key).": ".$val."\r\n"; } // construct MIME PLAIN Email headers $header = "MIME-Version: 1.0\n"; $header .= "Content-type: text/plain; charset=utf-8\n"; $header .= "From: $from_name <$email> \r\nReply-To: $from_name <$email> \r\nReturn-Path: <$email> \r\n"; // send email $to = emailswitch( $subject ); $mail_sent = mail($to, $subject, $body, $header); ?> change to above code but unfortunately didnt works... there was no email sent and no error as well... did i did wrong somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420685 Share on other sites More sharing options...
BagoZonde Posted March 24, 2013 Share Posted March 24, 2013 Just use your imagination: have you returned anything from emailswitch() function? Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420688 Share on other sites More sharing options...
lazyserv Posted March 24, 2013 Author Share Posted March 24, 2013 sorry i'm still quite new with php.. meaning to say the code i use i still miss out a return key? Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420706 Share on other sites More sharing options...
DaveyK Posted March 24, 2013 Share Posted March 24, 2013 well, your name says lazy so we shouldnt be surprised. Why are you asking if you miss when you can also add it in and test for yourself. We are not here to give you all the right answers. We are here to guide you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420723 Share on other sites More sharing options...
lazyserv Posted March 25, 2013 Author Share Posted March 25, 2013 well, your name says lazy so we shouldnt be surprised. Why are you asking if you miss when you can also add it in and test for yourself. We are not here to give you all the right answers. We are here to guide you in the right direction. well if my name represent my character then shall i name myself phpfreak? so im a godlike in php? no right? stop being so naive. im here to look for guidance and not looking for a sarcastic help. even you're in kindy the teacher teach you an A using draw out to show the A or only say A and you know how to write out? please make some commonsense if you're not willing to guide then just get lost... Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420818 Share on other sites More sharing options...
DaveyK Posted March 25, 2013 Share Posted March 25, 2013 As you mentioned, you are looking for guidance. BogaZonde guided you in the right direction, the function does the logic but doesnt return anything. Do you know how to proceed from here? Quote Link to comment https://forums.phpfreaks.com/topic/275994-contact-form-array-error/#findComment-1420876 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.