Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";
?>
like image 350
Crash893 Avatar asked May 23 '26 13:05

Crash893


2 Answers

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 )
like image 85
aziz punjani Avatar answered May 25 '26 03:05

aziz punjani


foreach ($_POST['Names'] as $selectedOption)
    echo $selectedOption."\n";
like image 43
case1352 Avatar answered May 25 '26 02:05

case1352



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!