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

如何将选中/未选中的复选框值数组传递给PHP电子邮件生成器?,php,arrays,forms,checkbox,Php,Arrays,Forms,Checkbox,我在表单中添加了一个复选框,用户可以在其中动态添加行。你可以看到表格 我使用一个数组将每行的值传递给PHP电子邮件生成器,其他输入都可以正常工作,但我无法使复选框正常工作。复选框输入当前如下所示: <input type="checkbox" name="mailing[]" value="Yes"> 但它并没有像预期的那样工作,即,我只看到选中的第一个复选框为“是”,而随后选中的复选框为“是” 另一个问题是,我希望为未选中的复选框生成值“No” 有人能帮忙吗 谢谢 尼克 表格:

我在表单中添加了一个复选框,用户可以在其中动态添加行。你可以看到表格

我使用一个数组将每行的值传递给PHP电子邮件生成器,其他输入都可以正常工作,但我无法使复选框正常工作。复选框输入当前如下所示:

<input type="checkbox" name="mailing[]" value="Yes">
但它并没有像预期的那样工作,即,我只看到选中的第一个复选框为“是”,而随后选中的复选框为“是”

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

有人能帮忙吗

谢谢

尼克

表格:

<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>
$(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();
                });

            });
要处理未选中的复选框,最好使用唯一值设置每个复选框:

<input type="checkbox" name="mailing[1]" value="Yes">
<input type="checkbox" name="mailing[2]" value="Yes">
您也可以将其与多个复选框一起使用:

$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);
$box=3;
$mailing=array();
$p=array\u key\u存在('mailing',$\u POST)$_POST['mailing']:数组();
对于($v=0;$v<$box;$v++){
如果(数组键存在($v,$p)){
$mailing[$v]=修剪(条纹斜杠($p[$v]);
}否则{
$mailing[$v]=“否”;
}
}
打印(邮寄);

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

<input type="checkbox" name="mailing[]" value="Yes-1"> 
<input type="checkbox" name="mailing[]" value="Yes-2"> 
您必须在初始页面加载时定义
n
。假设您只从一个“人”开始,只需在您的
$(“.add”)上方添加。单击
处理程序:

var n=1;
然后:

  • 在您的
    $(“.add”)。单击
    处理程序,增加
    n
  • 在您的
    $(“.remove”)。单击
    处理程序,减小
    n

要在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);
?>

以下是我的解决方案:

在html中有一组复选框,如

$_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;
}

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

<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>
$\u POST['something']=$this->fixararyofcheckbox($\u POST['something']);
函数fixaryofcheckbox($checks){
$newChecks=array();
对于($i=0;$i
这将为每个(和每个)复选框提供一个值为“开”或“关”的数组

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

以下是我的解决方案:



@AJ不知道该怎么做,因为表单上的后续行是使用克隆动态添加的。更新了我的答案…你不能这样做吗?(上图)@AJ不,我不清楚如何使用克隆方法生成唯一值。我已经在上面的问题中添加了表单和克隆脚本。复选框不是这样工作的……如果没有选中,它就不会被发送到服务器。如果您需要明确知道哪些复选框未选中,则必须实现另一种机制来完成此操作。有不同的技术,其中之一是为每个复选框使用隐藏字段。查看此链接:我认为您可以为每个复选框指定不同的值。我会用代码更新我的答案。谢谢。这个方法可以和我用来动态添加行的克隆方法一起使用吗?@Nick:只要你保持某种运行索引,并在任何时候动态添加行并在删除行时将其递减,就可以了)。您需要向脚本发送复选框总数,并稍微修改我的代码,以使用多个复选框而不是列表。未选中的复选框不会与表单的其余部分一起提交。如果你想说“否”,你就必须跟踪表单上的哪些复选框,并列出哪些复选框没有被选中。另一种方法是在提交时使用一些javascript循环所有复选框,并建立未选中复选框的列表,然后将该值作为另一个表单字段提交。建议通过添加“checked”属性,默认情况下将每一新行设置为“checked”,以减少用户提交未选中行的可能性<代码>
x.find('input:checkbox').each(function() { this.value='Yes-'+n; }); 
var n=1;
<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);
?>
<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[]" />
$_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;
}
<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>