Skip to content
Advertisement

Create a dynamic array from PHP &_POST for checked boxes to use in a foreach loop

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..

JavaScript

The output page looks like this

JavaScript

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.

JavaScript

Now you can easily loop through all of the values selected by the user.

JavaScript

HTML will only pass checked values to the server by default.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement