Crash893
Crash893

Reputation: 11702

Post array from html to php

Im pretty new to php programing but im trying to pass the values from a multislect object im html to some code in PHP that will then email the listed people

However the vairable passed via post appears to only be the last item selected

How do you pass something as an array?

html code:

<li id="li_6" >
            <label class="description" for="Names[]">Employees who should be added to this charge code: (Hold CTRL to Multi Select)</label>
            Malav, Nancy, Filiz, CJ are added by default and not on the list
            <div>
                <select name="Names" multiple>  
                    <option value="[email protected]">Bi,Gilbert</option>
                    <option value="[email protected]">Bor,Jeff</option>
                    <option value="[email protected]">Bt,Ivan</option>
                    <option value="[email protected]">Caos,Sheryl</option>
                    <option value="[email protected]">Car,Raymond</option>
                    <option value="[email protected]">Cs,Dale</option>
                    <option value="[email protected]">Cles,Cathy</option>
                    <option value="[email protected]">Cl,Lori</option>
                    <option value="[email protected]">Cons,Renee</option>
                    <option value="[email protected]">ine,Cassandra</option>
                    <option value="[email protected]">Ctt,Pamela</option>
</select>
            </div> 
        </li>

Php Code:

<?php
// Read POST request params into global vars
    $from = "[email protected]";
    $title = $_POST[Title] ;
    $mname = $_POST[ManagerName];
    $memail = $_POST[ManagerEmail];
    **$names= $_POST['Names']; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<**
    $other = $_POST[element_4];

    print $email;
    $to = "[email protected]" + $memail;
    $message ="Title: $title \r\nManager: $mname\r\nManager email: $memail\r\n" ;
    $headers = "From: $from";
    $today = date("m/d/Y");
    $ok = @mail($to, $subject, $message, $headers);

    echo "Your email has been sent! Please confirm with Malav if you do not receive a confrimation email in 2 hours";
?>

Upvotes: 2

Views: 6100

Answers (3)

abelvf
abelvf

Reputation: 51

when I use this:

foreach ( $_POST['names'] as $selectedOption )

and I insert in the database "$selectedOption" appear in the database this:

on

Upvotes: 0

aziz punjani
aziz punjani

Reputation: 25786

To pass it in as an array of values, add square brackets to the name attribute of your select element..

<select name="names[]" multiple="multiple"> 

Then you can iterate over the values in php.

foreach ( $_POST['names'] as $selectedOption )

Upvotes: 10

case1352
case1352

Reputation: 1136

foreach ($_POST['Names'] as $selectedOption)
    echo $selectedOption."\n";

Upvotes: 1

Related Questions