I’m trying to use form data posted from another page as $_POST. The data contains an email and file paths which are held as values in check boxes.
What I’m trying to achieve is to send an email for each of the selected files as an attachment. I already have the working as an array but this just sends all the files.
I was hoping to improve the function of this form by allowing the user to select which files to attach.
Can anyone offer a solution that will extract the file path data (from checkbox value) for use in a foreach loop (which will send the emails)
I don’t need a solution for the emails, I just need to get the loop working so an example that can echo the following would suffice.
Email: $email File: $filename*
/Loop second time round /
Email: $email File: $filename* Etc…
- Loop for $filename should only show for selected check boxes on previous page.
Input page looks like this..
<!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>
The output page looks like this
<!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>
Advertisement
Answer
First, some code indentation would probably help read your code easier.
Next, change your form checkboxes to submit as an array instead of individual variables.
<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>
Now you can easily loop through all of the values selected by the user.
<?php foreach($_POST['containers'] as $container) { //... } foreach($_POST['scripts'] as $script) { //... } foreach($_POST['macros'] as $macro) { //... } ?>
HTML will only pass checked values to the server by default.