0

Can't get my form to send data from a multiple select option (named 'country' in my coding below). I've checked out a few answers on here already, but I'm really no PHP genius to be able to figure out how and why it's not working for me. What happens when I test it is that the form sends the option which was selected last and that's it.

My HTML code is as follows:

<form class="well" action="contact-send.php" method="post">
            <input type="text" class="span4" name="name" placeholder="Your name" /><br />
            <input type="text" class="span4" name="email" placeholder="Your email address" /><br />
            <select class="span4" multiple="multiple" name="country[]" size="5" style="padding:4px;">
            <option value="Thailand">Thailand</option>
            <option value="Cambodia">Cambodia</option>
            <option value="Laos">Laos</option>
            <option value="Vietnam">Vietnam</option>
            <option value="India">India</option>
            <option value="Japan">Japan</option>
            <option value="Philippines">Philippines</option>
            <option value="Bhutan">Bhutan</option>
            <option value="Singapore">Singapore</option>
            <option value="Borneo">Borneo</option>
            <option value="Myanmar">Myanmar</option>
            <option value="Malaysia">Malaysia</option>
            <option value="China">China</option>
            <option value="Nepal">Nepal</option>
            <option value="Sri Lanka">Sri Lanka</option>
            <option value="North Korea">North Korea</option>
            </select>
            <input type="date" class="span4" name="departure" placeholder="Departure date" /><br />
            <input type="text" class="span4" name="days" placeholder="Number of days" /><br />
            <input type="text" class="span4" name="travelers" placeholder="Number of travelers" /><br />
             <button class="btn btn-primary" type="submit" name="submit" value="Submit">Submit</button>
        </form>    

My PHP code is as follows:

<?php

if (isset($_POST['submit'])) {
$msg = 'Name: ' .$_POST['name'] ."\n"
.'Email: ' .$_POST['email'] ."\n"
.'Countries: ' .$_POST['country'] ."\n"
.'Departure Date: ' .$_POST['departure'] ."\n"
.'Number of Days: ' .$_POST['days'] ."\n"
.'Number of Travelers: ' .$_POST['travelers'] ."\n";



mail ('me@myemailaddress.com', 'Landing Page Enquiry', $msg);

 header ('location: send-success.php');

} else {
header('location: send-failed.php');
exit(0);
}
?>
chrsmrtn81
  • 21
  • 1
  • 7
  • 1
    Why it has to be `name="country[]"`? have you tried to `var_dump($_POST['country']);`? it might be returning an array and in your code, you put the `$_POST['country']` inside a string. Check if it is an array, an if it is an array, try to implode it first before you concatenate it with a string. – Jayson O. Apr 22 '13 at 02:46
  • How come this post is not helping you? http://stackoverflow.com/questions/12064687/sending-email-from-php-on-localhost – Rocks Apr 22 '13 at 03:01
  • I used name="country[]" because that's what every other thread I could find on the problem said to do..? Not right? Just did var_dump($_POST['country']); and it returned this array(3) { [0]=> string(8) "Thailand" [1]=> string(8) "Cambodia" [2]=> string(4) "Laos" } ..so seems it's an array, so I implode it like this? .'Countries: ' .implode($_POST['country']) ."\n" – chrsmrtn81 Apr 22 '13 at 03:09
  • Ok, figured it out now! Thanks for your help Jayson, what you suggested worked a treat!!! :) Awesome! – chrsmrtn81 Apr 22 '13 at 03:12

1 Answers1

0

You named your form element:

name="country[]" 

so $_POST['country'] is an array. If you want all the elements of the array, i.e. all the selections, then do something like:

if (isset($_POST['submit'])) {
    $countries =  implode(' ', $_POST['country']);
}
7stud
  • 46,922
  • 14
  • 101
  • 127