Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a contact from that suppose to send emails to different people as the dropdown option.

The option values are defined in arrays with an array and I am battling to call the array value so the email can go to designated person.

Arrays are defined here:

if((isset($_GET['enquiry']) && $_GET['enquiry']=='Locate a dealer/distributor')||(isset($_POST['enquiry']) && $_POST['enquiry']=='Locate a dealer/distributor')){
    $chk_Locate=TRUE;
}

if(isset($_GET['enquiry']) && $_GET['enquiry'] == 'CC'){
    $query_recipients = array(array('Complaint', '[email protected]'),
                            array('Suggestion', '[email protected]'),
                            array('Compliment', '[email protected]'));
    $enquiry = '<input type="hidden" name="enquiry" id="enquiry" value="CC">';
}

else{
    $query_recipients = array(array('Locate a dealer/distributor', '[email protected]'),
                            array('Technical support', '[email protected]'),
                            array('Back orders', '[email protected]'),
                            array('Product enquiry', '[email protected]'),
                            array('Catalog request', '[email protected]'),
                            array('New customer enquiry', '[email protected]'),
                            array('Existing customers - logon', '[email protected]'),
                            array('Existing customers - orders', '[email protected]'),
                            array('Report web problems', '[email protected]'));
}

Loop

foreach($query_recipients as $key => $val){ 
    if((isset($_POST['enquiry']) && $_POST['enquiry'] == $val[0])||(isset($_GET['enquiry']) && $_GET['enquiry'] == $val[0])){
        $selected = $val[0].' : '.$item;
        if($val[0]=='Existing customers - logon'||$val[0]=='Technical support'||$val[0]=='Catalog request'||$val[0]=='Product enquiry'||$val[0]=='New customer enquiry'||$val[0]=='Existing customers - orders'||$val[0]=='Catalog request')

The code that calls them is:

$_POST['recipient_email'] = $contact_email.','.$recpt_email;
    if(isset($ref_number)){
        $_POST['subject'] = 'Examplecompany Group: '.$ref_number.': '.$_POST['enquiry'];
    }else{
        $_POST['subject'] = 'Examplecompany Group: '.$_POST['enquiry'];
    }
    $_POST['sender_email'] = $_POST['email'];
    $_POST['cc'] = $_POST['email'].', Examplecompany Group <[email protected]>';
    $_POST['body'] = '<p>Name: ' . $_POST['name'] . '</p>'
                        . '<p>Surname: ' . $_POST['surname'] . '</p>'
                        . '<p>Company: ' . $_POST['company'] . '</p>'
                        . '<p>Examplecompany Account No.: ' . $_POST['accnr'] . '</p>'
                        . '<p>Account No. verification: ' . $cust_verify . '</p>'
                        . '<p>Email: ' . $_POST['email'] . '</p>'
                        . '<p>Tel/cell: ' . $_POST['tel'] . '</p>'
                        . $_POST['enquiry'] . ': ' . $_POST['message'];

    if(Email::Send($_POST, NULL)){
        $message = 'Your feedback has been sent'.$cont_ref;
    } else {
        $message = 'There was an error sending your feedback';
    }
}

This is the line I suppose to use to call them $_POST['recipient_email'] = $contact_email.','.$recpt_email; for now it works but it calls the last array array('Report web problems', '[email protected]') irrespective of chosen option.

Please help, if i can find a way to call the array values I believe my form with work according to plan.

There is a line that i dont understand

$enquiry_options .= '<option>'.$val[0].'</option>';
    $recpt_email = $val[1];

If the val is set to $Val[1] the form sends email to the last email address on arrays but if set to $Val[0] it doesnt.

share|improve this question
1  
Have you considered using key-value pairs instead of nested arrays? For example, array('Complaint' => '[email protected]', 'Suggestion' => '[email protected]'). See php.net/manual/en/language.types.array.php –  user113215 Aug 27 '12 at 15:03
    
Thanks for input, i'll check it out and feedaback. –  Mlungisi Aug 27 '12 at 17:32
    
Thanks for input, yes I've tried that and the page does load. I am not the original coder and cant seem to know what the original coder assign 1 here: $recpt_email = $val[1]; –  Mlungisi Aug 27 '12 at 17:40
    
Thanks @user113215 i've been cracking my heard with this code but no luck. Enquiry field is: <div><label>Enquiry</label><div><select name="enquiry" id="enquiry" class="select"><option selected="selected"><?php echo $selected ?></option><?php echo $enquiry_options ?></select></div></div> –  Mlungisi Aug 27 '12 at 20:06
    
I moved my comments to an answer and provided some sample code. See below. –  user113215 Aug 27 '12 at 20:39

1 Answer 1

up vote 0 down vote accepted

Consider switching your array to use key-value pairs instead of nested arrays. (Refer to the array documentation.)

$enquiry_choices = array(
    'Complaint' => '[email protected]',
    'Suggestion' => '[email protected]',
    'Compliment' => '[email protected]',
    'Locate a dealer/distributor' => '[email protected]',
    'Technical support' => '[email protected]',
    'Back orders' => '[email protected]',
    'Product enquiry' => '[email protected]',
    'Catalog request' => '[email protected]',
    'New customer enquiry' => '[email protected]',
    'Existing customers - logon' => '[email protected]',
    'Existing customers - orders' => '[email protected]',
    'Web site problems' => '[email protected]',
);

// Optional: Sort the choices by name
ksort($enquiry_choices);

This makes it easy to get the e-mail address associated with the user's choice.

$enquiry = $_GET['enquiry'];
$enquiry_email = (array_key_exists($enquiry, $enquiry_choices) ? $enquiry_choices[$enquiry] : false);

It is also simple to generate the HTML for the dropdown list. (Refer to the foreach documentation.) Note that the code above that defines $enquiry must preceed the code below.

echo <<<HTML
<div>
    <label for="enquiry">Enquiry:</label>
</div>
<select name="enquiry" id="enquiry" class="select">
HTML;

foreach($enquiry_choices as $key => $value) {
    $selected = ($enquiry == $key ? ' selected="selected"' : '');
    echo "<option$selected>$key</option>";
}

echo '</select>';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.