简体   繁体   English

PHP表单清单显示“ For:”标签,而不是“ Value:”

[英]PHP Form Checklist show “For:” label instead of “Value:”

I finally got a checklist working in PHP, but it's listing the "value:" of my input rather than the "for:" the reason I can't use the 'value:' is because the value is a number that gets totalled up and I want to know what option has been ticked instead of just seeing a number, 我终于有了一个在PHP中工作的清单,但是它列出了输入的“值:”而不是“ for:”,之所以不能使用“值:”,是因为该值是一个总计的数字我想知道已经勾选了哪个选项,而不仅仅是看到一个数字,

<input class="basic-config" type="checkbox" name="ch[]" id="basic-option1" value="100"><label for="basic-option1">option 1<span class="right-align">$100</span></label>

<input class="basic-config" type="checkbox" name="ch[]" id="basic-option2" value="200"><label for="basic-option2">option 2<span class="right-align">$200</span></label>

The PHP is below: PHP如下:

if (isset($_POST['submit'])) {

$name = $_POST ['name'];
$mailFrom = $_POST ['mail'];
$website = $_POST ['website'];
$comments = $_POST ['comments'];
$basicAmount = $_POST ['basic-amount'];
$extras = "None";
if(isset($_POST["ch"]))
$extras = implode(", ", $_POST["ch"]);

$mailTo = "josh@myemail.com";
$headers = "From: ".$mailFrom; 
$txt = "\n
Basic Order Request. \n\n
From: $name \n\n
Email: $mailFrom \n\n
Website: $website \n\n
Comments: $comments \n\n
Extras: $extras \n\n
Total: $basicAmount";
}

How can I get it to list the "for" or even "id" name of the checkbox ticked instead of listing the "value"? 如何获取列出复选框的“ for”或“ id”名称而不是列出“ value”?

For example, this is what I want: 例如,这就是我想要的:

Extras: Option 1, Option 2 Total: $300 附加项:选项1,选项2总计:$ 300

This is what I have at the moment and not what I want: 这是我目前所拥有的,而不是我想要的:

Extras: 100, 200 Total: $300 额外:100,200合计:$ 300

Would appreciate any help! 将不胜感激!

Only successful controls (inputs) are submitted and only the value. 仅提交成功的控件(输入),仅提交值。 You can use the option as the array key: 您可以使用该选项作为数组键:

<input type="checkbox" name="ch[Option 1]" value="100">
<input type="checkbox" name="ch[Option 2]" value="200">

Then get those keys: 然后获取这些密钥:

$extras = implode(", ", array_keys($_POST['ch']));

Or create a hidden input array and pair them up (use the same key): 或创建一个隐藏的输入数组并将它们配对(使用相同的键):

<input type="hidden" name="ex[0]" value="Option 1">
<input type="checkbox" name="ch[0]" value="100">
<input type="hidden" name="ex[1]" value="Option 2">
<input type="checkbox" name="ch[1]" value="200">

Then access the options that have a corresponding check: 然后访问具有相应检查的选项:

$extras = implode(", ", array_intersect_key($_POST['ex'], $_POST['ch']));

just make your value identical with your id , such as: 只要使您的id相同,例如:

<input class="basic-config" type="checkbox" name="ch[]" id="basic-option1" value="Option 1"><label for="basic-option1">option 1<span class="right-align">$100</span></label>
<input class="basic-config" type="checkbox" name="ch[]" id="basic-option2" value="Option 2"><label for="basic-option1">option 2<span class="right-align">$200</span></label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM