phpemailmultidimensional-arrayforeach

How to get a set of dropdown list values and email it using PHP?


I have three dropdown list - a set.

  1. 1st one is for 'destination'
  2. 2nd one is for select guest interest in what 'Attraction or Activity' in the 'destination'
  3. 3rd is for 'available attraction / activity' in the 'destination'

created by following code:

<div class="multi-field">
    <select class="text-one" name="destination[]">
        <option selected value="base">Please Select</option>
        <option value="colombo">Colombo</option>
        <option value="kandy">Kandy</option>
        <option value="anuradhapura">Anuradhapura</option>
    </select>
    <br />

    <select class="text-two" name="attraction_or_activity[]">
        <option value="attraction_or_activity">Select the attractions or activities</option>
        <option value="attraction">Attraction</option>
        <option value="activity">Activity</option>
    </select>

    <select class="populated_attr_or_activity" name="attraction_or_activity_selected[]">
        <option value="available_attr_act">Available attractions / activities</option>
        <!-- Here, available options will be populated -->
    </select>
</div>

After guest selected the option as they want and submit the form I should receive these selections to my email message as follow order.

destination (selected value in 1st drop down list) : Attraction/Activity (selected value in 2nd drop down list) : Selected available attraction/activity (selected value in 3rd drop down list)

This set of dropdown list can be added more dynamically by clicking an Add More (Add Destination) button. So for now I am using following code to get my email.

<?php
if(empty($errors)){ // if the errors array is empty then send the mail 
    $headers  = 'MIME-Version: 1.0' . "\r\n"; // adding support for HTML type email
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // adding support for HTML type email
    //print $tourist_name;
    $msg = '';
    $i = 1;
    foreach ($_POST['destination'] as $key => $val) {
        $msg .= ' <strong>' . $i++ . '. Destination : </strong>' . $val . ', <strong>Attraction/Activty : </strong>' . $_POST['attraction_or_activity'][$key] . '<br />';
    };
    $msg .= 'Name : ' .$tourist_name.'<br/>';
    $msg .= 'E-mail : ' . $tourist_email.'<br/>';
    $msg .= 'Country : ' .$tourist_country.'<br/>';
    mail("myemail@gmail.com","Custom Tour Package",$msg, $headers);
}
?>

As you see above I am using 'destination' and 'attraction_or_activity' array only. now my email body looks like:


1. Destination : colombo, Attraction/Activity : attraction

2. Destination : colombo, Attraction/Activity : activity

3. Destination : kandy, Attraction/Activity : attraction

Name : guest name

E-mail : guestEmail@gmail.com

Country : guest's Country


But how I am expecting to get the email is like below:

1. Destination : colombo, Attraction/Activity : attraction Selected attraction/activity : Ganga Ramaya

2. Destination : colombo, Attraction/Activity : activity Selected attraction/activity : city tour

3. Destination : kandy, Attraction/Activity : attraction Selected attraction/activity : Temple Tooth

Name : guest name

E-mail : guestEmail@gmail.com

Country : guest's Country


how I have to modify my code to get the email as I expect above?

how can I include 3rd dropdown lists value into foreach loop?


Solution

  • You just need to take values of attraction_or_activity_selected like you did for attraction_or_activity. Try changing the code inside foreach to this :

    foreach ($_POST['destination'] as $key => $val) {
            $msg .= ' <strong>' . $i++ . '. Destination : </strong>' . $val
                    . ', <strong>Attraction/Activty : </strong>' . $_POST['attraction_or_activity'][$key]
                    . ', <strong>Selected attraction/activity  : </strong>' . $_POST['attraction_or_activity_selected'][$key]
                    . '<br />';
    
    }