Including Label Values When Emailing HTML Form Submissions
I've created a dynamic HTML form that uses jQuer开发者_运维问答y and customizes the form fields and labels based on the users' input.
In addition to passing the form field values to the $_POST[]
array, is there also a simple way to pass the label values? I need to turn the form submission into an email, and the email should include the same label names that appeared on the form. I've contemplated using hidden input fields, but that doesn't seem like an ideal option.
Any ideas?
There is no use in sending those label texts around. It is unnecessary traffic, and one more thing that needs to be filtered/validated.
You create the form on the server side, so you already have access to the texts of the labels there. I'd advise you to store these texts in constants, like:
define('TEXT_EMAIL', 'Email Address');
So when you create the form, you can just type:
<label for="email"><?=TEXT_EMAIL?></label>
and use the same constant (TEXT_EMAIL
and the others) when you build the email body. This way, you will also be in an easy situation if you need to add support for other languages.
... maybe add the label values to dynamically created hidden fields in the form? Just name the fields (prefix them?) in such a way that you can identify them easily on the server side.
You are right to consider hidden form fields if you're not POSTing the data to the server through an Ajax request.
Assuming a regular form submission, only values of input
and textarea
elements are sent to the server. Adding appropriate hidden input elements and setting the values of these from the labels is your only option.
精彩评论