如何将一组选中/未选中的复选框值传递给 PHP 电子邮件生成器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6376112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:10:54  来源:igfitidea点击:

How to pass an array of checked/unchecked checkbox values to PHP email generator?

phparraysformscheckbox

提问by Nick

I have added a checkbox to a form that the user can dynamically add rows to. You can see the form here.

我在用户可以动态添加行的表单中添加了一个复选框。您可以在此处查看表格。

I use an array to pass the values for each row to a PHP email generator, and all works fine for other inputs, but I can't get the checkbox to work. The checkbox input currently looks like this:

我使用一个数组将每一行的值传递给 PHP 电子邮件生成器,并且对于其他输入都可以正常工作,但是我无法让复选框工作。复选框输入目前看起来像这样:

<input type="checkbox" name="mailing[]" value="Yes">

Then in the PHP I have this:

然后在PHP中我有这个:

$mailing = trim(stripslashes($_POST['mailing'][$i]));

But it is not working as expected, i.e. I am only seeing 'Yes' for the first checkbox checked, and nothing for subsequent checkboxes that are checked.

但它没有按预期工作,即我只看到第一个选中的复选框为“是”,而随后选中的复选框没有任何内容。

One further issue is that I would like the value 'No' to be generated for unchecked checkboxes.

另一个问题是,我希望为未选中的复选框生成值“否”。

Could someone help with this?

有人可以帮忙吗?

Thanks,

谢谢,

Nick

缺口

Form:

形式:

<form method="post" action="bookingenginetest.php">
            <p>
                <input type="checkbox" name="mailing[]" value="Yes">
                <label>Full Name:</label> <input type="text" name="name[]">
                <label>Email:</label> <input type="text" name="email[]">
                <label>Telephone:</label> <input type="text" name="telephone[]">
                <span class="remove">Remove</span>
            </p>
            <p>
                <span class="add">Add person</span><br /><br /><input type="submit" name="submit" id="submit" value="Submit" class="submit-button" />
            </p>

        </form>

Cloning script:

克隆脚本:

$(document).ready(function() {

                $(".add").click(function() {
                    var x = $("form > p:first-child").clone(true).insertBefore("form > p:last-child");
                    x.find('input').each(function() { this.value = ''; });
                    return false;
                });

                $(".remove").click(function() {
                    $(this).parent().remove();
                });

            });

回答by GWW

$mailing = array();
foreach($_POST as $v){
    $mailing[] = trim(stripslashes($v));
}

To handle unchecked boxes it would be better to set each checkbox with a unique value:

要处理未选中的框,最好为每个复选框设置一个唯一值:

<input type="checkbox" name="mailing[1]" value="Yes">
<input type="checkbox" name="mailing[2]" value="Yes">

or

或者

<input type="checkbox" name="mailing[a]" value="Yes">
<input type="checkbox" name="mailing[b]" value="Yes">

Then have a list of the checkboxes:

然后有一个复选框列表:

$boxes = array(1,2,3);
$mailing = array();
$p = array_key_exists('mailing',$_POST) ? $_POST['mailing'] : array();
foreach($boxes as $v){
    if(array_key_exists($v,$p)){
        $mailing[$v] = trim(stripslashes($p[$v]));
    }else{
        $mailing[$v] = 'No';
    }
}

print_r($mailing);

You could also use this with a number of checkboxes instead:

您也可以将其与多个复选框一起使用:

$boxes = 3;
$mailing = array();
$p = array_key_exists('mailing',$_POST) ? $_POST['mailing'] : array();
for($v = 0; $v < $boxes; $v++){
    if(array_key_exists($v,$p)){
        $mailing[$v] = trim(stripslashes($p[$v]));
    }else{
        $mailing[$v] = 'No';
    }
}

print_r($mailing);

回答by Rooster242

Here's my solution:

这是我的解决方案:

With an array of checkboxes in the html like so...

在 html 中使用一系列复选框,就像这样......

<input type="hidden" name="something[]" value="off" />
<input type="checkbox" name="something[]" />
<input type="hidden" name="something[]" value="off" />
<input type="checkbox" name="something[]" />
<input type="hidden" name="something[]" value="off" />
<input type="checkbox" name="something[]" />

I then fix the posted array with this function...

然后我用这个函数修复了发布的数组......

$_POST[ 'something' ] = $this->fixArrayOfCheckboxes( $_POST[ 'something' ] );
function fixArrayOfCheckboxes( $checks ) {
    $newChecks = array();
    for( $i = 0; $i < count( $checks ); $i++ ) {
        if( $checks[ $i ] == 'off' && $checks[ $i + 1 ] == 'on' ) {
            $newChecks[] = 'on';
            $i++;
        }
        else {
            $newChecks[] = 'off';
        }
    }
    return $newChecks;
}

This will give me an array with values of either 'on' or 'off' for each (and every) checkbox.

这将为我提供一个数组,其中每个(和每个)复选框的值为“on”或“off”。

Note that the hidden input MUST be BEFORE the checkbox input in order for the function to work right.

请注意,隐藏输入必须在复选框输入之前,以便函数正常工作。

回答by AJ.

Change the value for each checkbox to something unique:

将每个复选框的值更改为唯一的值:

<input type="checkbox" name="mailing[]" value="Yes-1"> 
<input type="checkbox" name="mailing[]" value="Yes-2"> 

etc. In order to do this from your jQuery code, add another linethat assigns the value to the new checkbox:

等。为了从您的 jQuery 代码中执行此操作,添加另一行将值分配给新复选框:

x.find('input:checkbox').each(function() { this.value='Yes-'+n; }); 

You'll have to define non the initial page load. Assuming you start with only one "person", just add right above your $(".add").clickhandler:

您必须n在初始页面加载时进行定义。假设您从一个“人”开始,只需在$(".add").click处理程序的正上方添加:

var n=1;

And then:

进而:

  • in your $(".add").clickhandler, increment the value of n
  • in your $(".remove").clickhandler, decrement the value of n
  • 在您的$(".add").click处理程序中,增加的值n
  • 在您的$(".remove").click处理程序中,减少的值n

回答by Zeeshan

To get checked and unchecked both values in POST place a hidden field with exactly the same name but with opposite value as compared to the original chkbox value such that in this case the value will be '0'

要在 POST 中选中和取消选中两个值,请放置一个与原始 chkbox 值完全相同但具有相反值的隐藏字段,这样在这种情况下,该值将为“0”

<input type="hidden" name="chk_name[]" value="0" />
<input type="checkbox"  name="chk_name[]"  value="1"/>
 <input type="hidden" name="chk_name[]" value="0" />
<input type="checkbox"  name="chk_name[]"  value="1"/>

<?php 
function getAllChkboxValues($chk_name) {
    $found = array(); //create a new array 
    foreach($chk_name as $key => $val) {
        //echo "KEY::".$key."VALue::".$val."<br>";
        if($val == '1') { //replace '1' with the value you want to search
            $found[] = $key;
        }
    }
    foreach($found as $kev_f => $val_f) {
        unset($chk_name[$val_f-1]); //unset the index of un-necessary values in array
    }   
    final_arr = array(); //create the final array
    return $final_arr = array_values($chk_name); //sort the resulting array again
}
$chkox_arr = getAllChkboxValues($_POST['chk_name']); //Chkbox Values
echo"<pre>";
print_r($chkox_arr);
?>

回答by YasirPoongadan

Here's my solution:

这是我的解决方案:

<span>
<input class="chkBox" onchange="if($(this).is(':checked')){$(this).parent().find('.hidVal').prop('disabled',true);}else{$(this).parent().find('.hidVal').prop('disabled', false);}" type="checkbox" checked name="session[]" value="checked_value_here" />
<input type="hidden" class="hidVal" name="session[]" value="un_checked_value_here" />
</span>
<span>
<input class="chkBox" onchange="if($(this).is(':checked')){$(this).parent().find('.hidVal').prop('disabled',true);}else{$(this).parent().find('.hidVal').prop('disabled', false);}" type="checkbox" checked name="session[]" value="checked_value_here" />
<input type="hidden" class="hidVal" name="session[]" value="un_checked_value_here" />
</span>