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



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

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

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

然后在PHP中,我有这样的:

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

但它并没有按预期工作,也就是说,我只看到第一个复选框为"是",而后续选中的复选框则没有。

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

有人能帮忙吗?

谢谢,

Nick

表单:

<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();
                });
            });
$mailing = array();
foreach($_POST as $v){
    $mailing[] = trim(stripslashes($v));
}

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

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

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

然后有一个复选框列表:

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

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

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

这是我的解决方案:

在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[]" />

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

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

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

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

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

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

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

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

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

var n=1;

然后:

  • $(".add").click处理程序中,递增n的值
  • $(".remove").click处理程序中,递减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);
?>

这是我的解决方案:

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

最新更新