How to get all selected values from multiple select option?

坚强是说给别人听的谎言 提交于 2019-12-19 04:14:19

问题


I have a contact form with multiple select option. But there is only 1 value appear in the email after the contact form submitted. Would like to look for solution to get this work.

/* My code begins */

$title = $_POST['title'];
$contactperson = $_POST['contactperson'];
$phonenumber = $_POST['phonenumber'];
$emailaddress = $_POST['emailaddress'];
$companyname = $_POST['companyname'];
$brand = $_POST['brand'];
$machinemodel = $_POST['machinemodel'];
$serialnumber = $_POST['serialnumber'];
$problems = $_POST['problems']; <--- this is multiple selection values
$inquiry = $_POST['inquiry'];
$security = $_POST['security'];
$from = "YT Copier";
$to = "mail@jornes.com";
$subject = "Admin, someone submitted service request form.";
$message = "Admin, someone has submitted service request form. Details are as follow: \n\nName: $title $contactperson\n\nPhone Number: $phonenumber\n\nEmail Address: $emailaddress\n\nCompany Name: $companyname\n\nMachine Brand:\n$brand\n\nMachine Model: $machinemodel\n\nSerial Number: $serialnumber\n\nProblem Facing: $problems\n\nMessage: $inquiry";

if ($security=="13") {
   mail($to, $subject, $message, $from);
   header("Location:/supports/submit-service-request/?s=s");
}

else {
   header("Location:/supports/submit-service-request/?s=e");
}

<-- HTML Begins here -->

<select multiple="multiple" name='problems' id='problems' class="inpBox multiple" size='8' >
        <option value="Cannot Copy">Cannot Copy</option>
        <option value="Cannot Print">Cannot Print</option>
        <option value="Cannot Print">Cannot Scan</option>
        <option value="Cannot Fax">Cannot Fax</option>
        <option value="Lines Appear When Printing/Copying">Lines Appear When Printing/Copying</option>
        <option value="Scan to Email Failed">Scan to Email Failed</option>
        <option value="Toner Low/Empty">Toner Low/Empty</option>
        <option value="Others">Others</option>
</select>

/* Code Ends */

This is the php code in the file name "process.php" to process the form. The value of "$problems" is a multiple select option. I tried to select a few choices when filling out the form, but at the end, there is only 1 single value showing in the email. Can someone help? What code should i add to make it work? Truly appreciate if someone could help. Thanks!


回答1:


Use name as like problems[] instead of problems

//Simple Form and getting the values

<form name="s" method="post" action="" >
 <select multiple="multiple" name='problems[]' id='problems' class="inpBox multiple" size='50' style="height:150px;" >
        <option value="Cannot Copy">Cannot Copy</option>
        <option value="Cannot Print">Cannot Print</option>
        <option value="Cannot Print">Cannot Scan</option>
        <option value="Cannot Fax">Cannot Fax</option>
        <option value="Lines Appear When Printing/Copying">Lines Appear When Printing/Copying</option>
        <option value="Scan to Email Failed">Scan to Email Failed</option>
        <option value="Toner Low/Empty">Toner Low/Empty</option>
        <option value="Others">Others</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>


<?php 
if (isset($_POST['submit'])) {
    $problems =  implode(',', $_POST['problems']);
    echo $problems;
}
?>


来源:https://stackoverflow.com/questions/27579831/how-to-get-all-selected-values-from-multiple-select-option

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!