如何通过选中/取消复选框值到PHP电子邮件生成一个数组? [英] How to pass an array of checked/unchecked checkbox values to PHP email generator?

查看:140
本文介绍了如何通过选中/取消复选框值到PHP电子邮件生成一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个复选框的形式,用户可以动态地添加行。你可以在这里看到形式

我使用一个数组来传递值每行一个PHP电子邮件生成,和所有其他投入工作正常,但我不能让该复选框工作。该复选框,输入目前看起来是这样的:

 <输入类型=复选框NAME =邮寄[]值=是>

然后在PHP我有这样的:

  $邮件= TRIM(的stripslashes($ _ POST ['邮件'] [$ i]));

但它不按预期工作,即我只看到'是'第一个复选框选中,并没有什么针对检查后续的复选框。

一个进一步的问题是,我想价值否的复选框选中要生成。

有人能帮助这一点?

谢谢,

尼克

形式:

 <形式方法=邮报行动=bookingenginetest.php>
            &所述p为H.;
                <输入类型=复选框NAME =邮寄[]值=是>
                <标签>全名:< /标签> <输入类型=文本名称=名称[]>
                <标签>电子邮件:< /标签> <输入类型=文本名称=电子邮件[]>
                <标签>电话:< /标签> <输入类型=文本名称=电话[]>
                <跨度类=删除>删除< / SPAN>
            &所述; / P>
            &所述p为H.;
                <跨度类=添加>添加的人< / SPAN>< BR />< BR /><输入类型=提交名称=提交ID =提交值=提交阶级=提交键/>
            &所述; / P>        < /表及GT;

克隆脚本:

  $(文件)。就绪(函数(){                $(加)。点击(函数(){
                    变种X = $(形式指p:第一胎)。克隆(真).insertBefore(形式指p:最后的孩子);
                    x.find('输入')每个(函数(){THIS.VALUE ='';});
                    返回false;
                });                $(删除)。点击(函数(){
                    $(本).parent()remove()方法。
                });            });


  $邮件=阵列();
的foreach($ _ POST作为V $){
    $邮寄[] = TRIM(的stripslashes($ V));
}

要处理选中框倒不如设置每个复选框具有独特的价值:

 <输入类型=复选框NAME =邮寄[1]VALUE =是>
<输入类型=复选框NAME =邮寄[2]VALUE =是>

 <输入类型=复选框NAME =邮寄[A]值=是>
<输入类型=复选框NAME =邮寄[B]VALUE =是>

然后有一个复选框列表:

  $箱=阵列(1,2,3);
$邮件=阵列();
$ P = array_key_exists('邮件',$ _ POST)? $ _ POST ['邮件']:阵列();
的foreach($箱为$ V){
    如果(array_key_exists($ V,$ P)){
        $邮寄[$ V] = TRIM(的stripslashes($ P [$ V]));
    }其他{
        $邮寄[$ V] ='否';
    }
}的print_r($邮寄);

您也可以与一些复选框,而不是使用

  $箱= 3;
$邮件=阵列();
$ P = array_key_exists('邮件',$ _ POST)? $ _ POST ['邮件']:阵列();
为($ V = 0; $ V族$箱; $ V ++){
    如果(array_key_exists($ V,$ P)){
        $邮寄[$ V] = TRIM(的stripslashes($ P [$ V]));
    }其他{
        $邮寄[$ V] ='否';
    }
}的print_r($邮寄);

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:

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

Then in the PHP I have this:

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

            });

解决方案

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

这篇关于如何通过选中/取消复选框值到PHP电子邮件生成一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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