如何将复选框的值与php中的文本值结合起来? [英] How can I combine values of checkboxes with values of text in php?

查看:146
本文介绍了如何将复选框的值与php中的文本值结合起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,我制作了一个基本上由两个选项组成的html表单:首先,客户端选择一系列复选框(来自设备的部分),然后写入每个选定复选框的数量。

pre code $< td>< input id =11.11.015.0002name =pecas []type =复选框value =11.11.015.0002 - BATERIA CHUMBO ACIDO 6V / 4AH/>< / td>
< td>< input name =qntd []size =7type =text/>< / td>

和php:

  if(isset($ pecas))
{
$ mensagem。=选择选择:< br />< br />;
}
else {
echo< script> alert('Selecione asPeçasDesejadas!'); location.href ='http://www.lyuz.com.br/pecas /埃罗';< /脚本>中;
出口;
}
foreach($ pecas as $ pecas_s){
} $ mensagem。= - 。$ pecas_s。< br />;

这给了我所有选中的复选框(部分),现在我只想获取input_text (金额)与这些选定的复选框相关联。



我卡住了。帮帮我。

解决方案

在每个元素的名称中指定一个键。使用一个数字,只要确保 pecas [1] 对应于 qntd [1] 。然后,当您循环访问其中一个数组时,这些键在另一个数组中是相同的。例如:

 <?php 
$ count = 0;

foreach($ itemList as $ item){
echo< tr> \\\
;
echo'< td>< input type ='checkbox'id ='{$ item ['id']}'name ='pecas [{$ count}]'>< / td> \\ \
;
echo'< td>< input type ='test'id ='{$ item ['id']}'name ='qntd [{$ count}]'>< / td> \\ \
;
echo< / tr> \\\
;
$ count ++;





$如果有3个复选框和数量框,可以说1和3盒被检查,但不是第二个。

 数组(
'pecas'=>数组(
0) =>'some value',//注意,没有1键,因为第二个复选框没有被选中
2 =>'其他值'
),
'qntd'= >数组(
0 =>'某个qntd',
1 =>''// 1没有被选中,所以在第二个文本框中什么都不应该输入
2 =>'其他qntd'

);

0 (第一个复选框)和 2 (第三个)将存在于'pecas'数组中,并且与 0 2 。然后你可以遍历数据:

pre $ //检查至少有一个复选框被选中
if(!空($ _ POST ['pecas'])){
//在获取键($ k)和值($ v)的复选框上循环。
foreach($ _ POST ['pecas'] as $ k => $ v){
//显示一条消息
echoPecas {$ v}({$ k})was was使用{$ _POST ['qntd'] [$ k]}的qntd检查< br>;
}
}


I'm new with PHP, and I'm making a html form which consist basically in two options: First, the client select a series of checkboxes (parts from an equipment) and then write the amounts of each selected checkbox...

<td><input id="11.11.015.0002" name="pecas[]" type="checkbox" value="11.11.015.0002 - BATERIA CHUMBO ACIDO 6V/4AH" /></td>
<td><input name="qntd[]" size="7" type="text" /></td>

and in php:

if(isset($pecas))
    {
    $mensagem .= "Peças Selecionadas:<br /><br />";
    }
else {
    echo "<script>alert('Selecione as Peças Desejadas!'); location.href='http://www.lyuz.com.br/pecas/erro';</script>";
    exit;
}
    foreach ($pecas as $pecas_s) {
    } $mensagem .= " - ".$pecas_s."<br />";

That gave me all the selected checkboxes (parts), now I'm trying to get only the input_text (amounts) associate with these selected checkboxes..

I'm stuck. Help.

解决方案

Specify a key in the name of each element. Use a number and just make sure that pecas[1] corresponds to qntd[1]. Then when you loop over one of the arrays, the keys will be the same in the other array. For example:

<?php
$count = 0;

foreach($itemList as $item){
    echo "<tr>\n";
    echo "  <td><input type='checkbox' id='{$item['id']}' name='pecas[{$count}]'></td>\n";
    echo "  <td><input type='test' id='{$item['id']}' name='qntd[{$count}]'></td>\n";
    echo "</tr>\n";
    $count++;
}

If there are 3 checkboxes and quantity boxes, and lets say the 1st and 3rd boxes are checked, but not the 2nd. Your post array will look like:

array(
    'pecas'=> array(
        0 => 'some value', //notice, no 1 key because the second checkbox was not checked.
        2 => 'some other value'
        ),
    'qntd' => array(
        0 => 'some qntd',
        1 => '' //1 was not checked, so nothing should have been entered in the second textbox.
        2 => 'some other qntd'
        )
    );

The keys 0 (first checkbox) and 2 (third) will exist in the 'pecas' array and will correspond with keys 0 and 2 in the 'qntd' array. You can then loop over the data like:

//check that at least one checkbox was checked
if(!empty($_POST['pecas'])){
    //loop over the checkboxes getting key ($k) and value ($v).
    foreach($_POST['pecas'] as $k=>$v){
        //display a message
        echo "Pecas {$v} ({$k}) was checked with a qntd of {$_POST['qntd'][$k]}<br>";
    }
}

这篇关于如何将复选框的值与php中的文本值结合起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆