Jump to content

Recommended Posts

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.com

below 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.

Link to comment
https://forums.phpfreaks.com/topic/275994-contact-form-array-error/
Share on other sites

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
    }

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 by DaveyK

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?

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.