编程语言
首页 > 编程语言> > php – 如何从多个选择选项中获取所有选定的值?

php – 如何从多个选择选项中获取所有选定的值?

作者:互联网

我有一个有多个选择选项的联系表格.但在提交联系表单后,电子邮件中只显示1个值.想找到解决方案来完成这项工作.

/ *我的代码开始* /

$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在这里开始 - >

<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>

/ *代码结束* /

这是处理表单的文件名“process.php”中的php代码. “$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;
}
?>

标签:contact-form,php
来源: https://codeday.me/bug/20191007/1863900.html