PHP Form Email Question

Good Morning Everyone!!

I have been working on a little project for the past two months which started off as a Google Sheet and I’m now trying to expand it into a website. I’m learning coding as I go along and have managed to accomplish a decent amount however there are still a few things I’m trying to figure out and can’t for the life of me.

  1. I’m trying to create a feature where during an employee’s shift, they enter multiple reports via a PHP form - these reports do not all consist of the same template/form. The goal is to be able to view the combined submitted reports (as a full report) on the same page and at the end of a shift email them all as one email with HTML. Is there a way to send forms to a dedicated page and keep piling them up until they are submitted somehow as one email at which point they are cleared from that page?

  2. I have created two methods of emailing individual PHP forms. One excludes any blank fields but does not allow me to customize the email body with HTML combining multiple inputs on the same line and eliminating the label for some fields, ie.,
    Instead of:
    Label 1: Input 1
    Label 2: Input 2
    I’m trying to get:
    Label 1: Input 1 - Input 2
    The second method allows me to customize the email with the above and HTML, however it includes empty fields.
    Is there a way to customize the PHP form’s email with the desired results, while excluding the label and input for fields left empty?

I’ve been trying really hard and cannot seem to find a solution. Any help at all is extremely appreciated!!

A database would be my recommendation for several reasons. But any stateful data store will work.

I prefer to use templates for all emails. It makes it simple to change the format and content without making it a tedious task.

Thank you so much for your response!

A database would be my recommendation for several reasons. But any stateful data store will work.
Unfortunately I’m not very experienced with this and still learning. I’ve never heard of “stateful data store”. Is there a link to a page you could recommend to go based off of? And yes, I’m currently using MySQLi as the database.

I prefer to use templates for all emails. It makes it simple to change the format and content without making it a tedious task.
How does one use/create this type of template?

Below is a photo of one of the forms and below that a photo of how I’d like the email to look like (some fields like i.e., site name and address are combined on one line with the same label separated by a dash. The only thing it’s not doing right now is leaving out labels and inputs for data not entered. The third picture is off my HTML/Email script.

Also, when the form is submitted, I would like to have it stacked into a secondary dedicated page where all the reports will add into in chronological order and the option to then email them at the click of a button, all at once using the same email format. Please help as I’ve spent a lot of time looking into options and can’t figure this out.

emailformat

You need to start small, with only the code needed for one form field. Once you can design, write, test, and debug the code for that field, you can deal with the code needed for all the rest of the fields. It is not worth your time to write out all the code for 30+ fields, if you must make repeated changes to it before you arrive at the final usage. The way you would deal with the code for multiple fields, is to use a data-driven design, where you have a data structure (array, database table) that defines the expected form fields, what validation steps each needs, and what processing each field will be used for on the server. Also, with a data-driven design, the data will be operated on as a set, using php array functions. The 30+ lines of code copying variables to other variables would become one single line of code that trims all the data at once and places the trimmed version into an array variable that would get used in the rest of the code.

Web servers are stateless. What this means is any data submitted to a web page is lost when the web server completes the processing of any server-side code for that web page. You must persistently store the data somewhere if you want it to be available outside of the one specific page request that it was submitted to. The easiest and most general-purpose place to store the data would be a database. Once you have the data stored in a database, you can do whatever you want with it, display it any way you want, send summary emails…

Create the file as an html file and add placeholders. You then use str_replace to replace the placeholders with the values you want to change them to, using arrays for each the placeholders you added and their values. You use that replacement as the email body.

Sponsor our Newsletter | Privacy Policy | Terms of Service