[英]php skip empty array
I have a form(page2) with 2 checkboxes when a user selects checkbox 1 the script on the 3rd page will send instructions 1 to the emailaddress. 当用户选择复选框1时,我有一个带有2个复选框的form(page2),第3页上的脚本会将指令1发送到电子邮件地址。 When chkbox 2 is selected it sends instructions2. 选择chkbox 2时,它将发送指令2。
This is working but i also get errors like filename cannot be empty when only 1 chkbox is checked. 这是可行的,但是当只选中1个chkbox时,我也收到类似文件名不能为空的错误。
this is the code from page 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>
this is the code from page 3 这是第3页的代码
// array with filenames to be sent as attachment
$i_allinonewebsolution=$_POST['i_allinonewebsolution'];
$i_custommade=$_POST['i_custommade'];
$files = array("$i_allinonewebsolution","$i_custommade");
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
I think the problem is here: 我认为问题出在这里:
// array with filenames to be sent as attachment
$i_allinonewebsolution=$_POST['i_allinonewebsolution'];
$i_custommade=$_POST['i_custommade'];
$files = array("$i_allinonewebsolution","$i_custommade");
I have tried to use implode and array_filter but i am doing that wrong i guess. 我尝试使用爆破和array_filter,但我猜我做错了。 Thanks for any help. 谢谢你的帮助。
I have added the form pages to jsfiddle. 我已经将表单页面添加到jsfiddle中。 there are 3 pages links are in the first jsfiddle 第一个jsfiddle中有3个页面链接
Try something like this 试试这个
$files = array();
if(isset($_POST['i_allinonewebsolution'])) {
array_push($files, $_POST['i_allinonewebsolution']);
}
if(isset($_POST['i_custommade'])) {
array_push($files, $_POST['i_custommade']);
}
If you are wanting to scale this and have more check boxes you could put them in an array like 如果您想缩放比例并具有更多复选框,则可以将它们放在数组中,例如
$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]);
}
}
When I do similar things with DB queries (where results can commonly be NULL in Select queries) I do a test to see if the variable has been set. 当我对数据库查询执行类似的操作(选择查询中的结果通常为NULL)时,我将进行测试以查看变量是否已设置。
Use isset to test if data was actually posted. 使用isset来测试是否实际发布了数据。
eg 例如
if(isset($var))
//Do Action
http://php.net/manual/en/function.isset.php http://php.net/manual/zh/function.isset.php
make sure POST values are not empty, try a simple if condition like if(empty($_POST['i_allinone']) && empty($_POST['custommade'])) before you proceed into actual logic on page 2 确保POST值不为空,尝试简单的if条件,如if(empty($ _ POST ['i_allinone'])&& empty($ _ POST ['custommade']))),然后继续进行第2页的实际逻辑研究
You can also use null , is_set etc to check if the POST or REQUEST values are empty 您还可以使用null , is_set等检查POST或REQUEST值是否为空
$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");
}
Just give a try ! 试试看! Sorry for extended code. 对不起,扩展代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.