php跳过空数组

php跳过空数组,php,arrays,checkbox,email-attachments,Php,Arrays,Checkbox,Email Attachments,我有一个表单(第2页),其中有两个复选框,当用户选中复选框1时,第3页上的脚本将向电子邮件地址发送说明1。选择chkbox 2时,它发送指令2 这是工作,但我也得到了错误,如文件名不能为空时,只有1 chkbox被选中 这是第2页的代码 <form name="form" method="POST" action="instructies_verzenden2.php"> <p></p> <input type="checkbo

我有一个表单(第2页),其中有两个复选框,当用户选中复选框1时,第3页上的脚本将向电子邮件地址发送说明1。选择chkbox 2时,它发送指令2

这是工作,但我也得到了错误,如文件名不能为空时,只有1 chkbox被选中

这是第2页的代码

    <form name="form" method="POST" action="instructies_verzenden2.php"> 
    <p></p>
    <input type="checkbox" name="i_allinonewebsolution" value="file_1.txt"><span       class="info-image information">All-in-One Websolution</span>
    <input type="checkbox" name="i_custommade" value="file_2.txt"><span class="info-image information">Custom Made</span>
    <input type="hidden" name="keyfield" value="<?php echo $domeinnaam?>"><input type="hidden" name="emailadres" value="<?php echo $data2[emailadres]?>"><input type="submit"  class="sub-small sub-opslaan green" value="Update gegevens">
    <p></p>
    </form>
我曾尝试使用内爆和阵列过滤器,但我想我做得不对。 谢谢你的帮助

更新 我已经将表单页面添加到JSFIDLE中。第一个JSFIDLE中有3个页面链接


试试这样的方法

$files = array();
if(isset($_POST['i_allinonewebsolution'])) {
   array_push($files, $_POST['i_allinonewebsolution']);
}
if(isset($_POST['i_custommade'])) {
   array_push($files, $_POST['i_custommade']);
}
如果您想扩展此功能并有更多复选框,可以将它们放入一个数组中,如

$checkboxes = array('i_allinonewebsolution',
                    'i_custommade',
                    'i_checkbox3',
                    'i_checkbox4');

$files = array();

foreach($checkboxes as $checkbox){
    if(isset($_POST[$checkbox])) {
       array_push($files, $_POST[$checkbox]);
    }
}

当我对DB查询执行类似的操作时(在Select查询中,结果通常为NULL),我会进行一次测试,以查看是否设置了变量

使用isset测试数据是否已实际发布

e、 g


确保POST值不为空,在进入第2页的实际逻辑之前,尝试一个简单的if条件,如if(empty($\u POST['i\u allinone'])和empty($\u POST['custommake'])

您还可以使用null设置为等来检查POST请求值是否为空

 $i_allinonewebsolution=$_POST['i_allinonewebsolution'];
$i_custommade=$_POST['i_custommade'];
if(!empty($i_allinonewebsolution))
{
$files = array("$i_allinonewebsolution");
}
if(!empty($i_custommade))
{
$files = array("$i_custommade");
}
if(!empty($i_allinonewebsolution) && !empty($i_custommade))
{
$files = array("$i_allinonewebsolution","$i_custommade");
}

试试看!很抱歉扩展了代码。

请确保POST值不为空,在进入第2if页的实际逻辑之前,尝试一个简单的if条件,如if(empty($\u POST['i\u allinonewebsolution'])&&empty($\u POST['custommade']){$i\u allinonewebsolution=$\u POST['i\u allinonewebsolution]}if(isset($\u POST['i\u custommade'])){$i_custommade=$_POST[“i_custommade”];}$files=array($i_allinonewebsolution“,$i_custommade”);这给了我两个错误您在localhost中测试了吗?在life网站上没有,但我无法共享登录详细信息我可以将页面发送到Email嘿,我不是说登录凭据,在尝试xampp localhost时,可以消除警告或错误,如未分配任何POST值。i_AllinoWebSolution']}它在}上给出了一个错误我不知道为什么?错过了分号Sorry PHP开发的第一条规则,如果你遇到错误,你可能错过了分号。就是这样,我将用你的解决方案更新上面的描述非常感谢。我假设当我需要再添加30个复选框时,我只是不断为每个复选框添加IF}。是的,你可以这样做,但我会添加更多的可伸缩性代码。我不理解代码。当它为空时,只需要忽略它。我不知道如何在我发布的代码中实现if-else。如果添加更多的复选框,这将不能很好地扩展。是的,我同意你的看法。我只是想解释一下如何克服目前的情况……我已经做到了。但它仍然会出错$files=array($i_allinonewebsolution“,$i_custommade”);如果(isset($files))
$checkboxes = array('i_allinonewebsolution',
                    'i_custommade',
                    'i_checkbox3',
                    'i_checkbox4');

$files = array();

foreach($checkboxes as $checkbox){
    if(isset($_POST[$checkbox])) {
       array_push($files, $_POST[$checkbox]);
    }
}
if(isset($var))
//Do Action
 $i_allinonewebsolution=$_POST['i_allinonewebsolution'];
$i_custommade=$_POST['i_custommade'];
if(!empty($i_allinonewebsolution))
{
$files = array("$i_allinonewebsolution");
}
if(!empty($i_custommade))
{
$files = array("$i_custommade");
}
if(!empty($i_allinonewebsolution) && !empty($i_custommade))
{
$files = array("$i_allinonewebsolution","$i_custommade");
}