如何解决通过PHP&_POST创建动态数组,以使复选框可在foreach循环中使用
我正在尝试使用从另一页发布的表单数据作为$ _POST。数据包含电子邮件和文件路径,这些值作为复选框中的值保存。
我要实现的目标是为每个选定文件发送电子邮件作为附件。我已经可以使用数组了,但这只是发送所有文件。
我希望通过允许用户选择要附加的文件来改进此表单的功能。
任何人都可以提供一种解决方案,该解决方案将从复选框值中提取文件路径数据以供foreach循环使用(该循环将发送电子邮件)
我不需要电子邮件的解决方案,我只需要使循环正常工作,这样一个可以回显以下内容的示例就足够了。
电子邮件:$ email 文件:$ filename *
/第二轮循环/
电子邮件:$ email 档案:$ filename * 等等...
- $ filename的循环应仅在上一页的选定复选框中显示。
输入页面看起来像这样。
<!DOCTYPE html>
<html>
<body>
<center>
<h5>Payload Tests</h5>
<form action="output.php" method="post">
<fieldset>
<legend>Enter Email Address</legend>
<input type="text" name="email" size="30" value="Enter Email Address">
<br>
</fieldset>
<fieldset>
<legend>Select Container Type</legend>
<input type="checkbox" name="container1" value="7z" checked> 7zip
<input type="checkbox" name="container2" value="dmg"> dmg
<input type="checkbox" name="container3" value="gz"> gz
<input type="checkbox" name="container4" value="rar" checked> rar
<input type="checkbox" name="container5" value="tar"> tar
<input type="checkbox" name="container6" value="zip" checked> zip<br>
</fieldset>
<fieldset>
<legend>Select Script Type</legend>
<input type="checkbox" name="script1" value="bat" checked>bat
<input type="checkbox" name="script2" value="ps1" checked>ps1
<input type="checkbox" name="script3" value="py">py
<input type="checkbox" name="script4" value="sh">sh<br>
</fieldset>
<fieldset>
<legend>Select Macro Type</legend>
<input type="checkbox" name="macro1" value="docm" checked>docm
<input type="checkbox" name="macro2" value="xlsm" checked>xlsm<br>
</fieldset>
<br>
<input type="submit" value="Send" />
</form>
<br>
<hr>
</center>
</body>
</html>
输出页面如下所示
<!DOCTYPE html>
<html>
<body>
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
//Convert posted form data into single variable
$mail1 = ($_POST["email"]);?>
<br>
<p>Email Attachments Sent to....
<br>
Email address: <span style="color:blue"><?php echo $mail1;?></span>
<br>
</p>
<p>
<!-- Now the loop to send each selected file in a separate email -->
<?php
$file = array(extract($_POST)); //Only want the checkbox bits used here not the email??
foreach ($file as $file1){
echo $file1<br>;
//Sendemail code goes here
}
// END loop
// For Testing ouput
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
</p>
</body>
</html>
解决方法
首先,一些代码缩进可能会帮助您更轻松地阅读代码。
接下来,更改您的表单复选框,以数组形式而不是单个变量的形式提交。
<form action="output.php" method="post">
<fieldset>
<legend>Enter Email Address</legend>
<input type="text" name="email" size="30" value="Enter Email Address">
<br>
</fieldset>
<fieldset>
<legend>Select Container Type</legend>
<input type="checkbox" name="containers[]" value="7z" checked> 7zip
<input type="checkbox" name="containers[]" value="dmg"> dmg
<input type="checkbox" name="containers[]" value="gz"> gz
<input type="checkbox" name="containers[]" value="rar" checked> rar
<input type="checkbox" name="containers[]" value="tar"> tar
<input type="checkbox" name="containers[]" value="zip" checked> zip<br>
</fieldset>
<fieldset>
<legend>Select Script Type</legend>
<input type="checkbox" name="scripts[]" value="bat" checked>bat
<input type="checkbox" name="scripts[]" value="ps1" checked>ps1
<input type="checkbox" name="scripts[]" value="py">py
<input type="checkbox" name="scripts[]" value="sh">sh<br>
</fieldset>
<fieldset>
<legend>Select Macro Type</legend>
<input type="checkbox" name="macros[]" value="docm" checked>docm
<input type="checkbox" name="macros[]" value="xlsm" checked>xlsm<br>
</fieldset>
<br>
<input type="submit" value="Send" />
</form>
现在,您可以轻松遍历用户选择的所有值。
<?php
foreach($_POST['containers'] as $container) {
//...
}
foreach($_POST['scripts'] as $script) {
//...
}
foreach($_POST['macros'] as $macro) {
//...
}
?>
HTML默认情况下仅将检查值传递给服务器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。