通过电子邮件发送 HTML 表单提交内容时包括标签值
我创建了一个动态 HTML 表单,它使用 jQuery 并根据用户的输入自定义表单字段和标签。
除了将表单字段值传递给 $_POST[]
数组之外,是否还有一种简单的方法来传递标签值?我需要将表单提交转换为电子邮件,并且电子邮件应包含表单上出现的相同标签名称。我考虑过使用隐藏的输入字段,但这似乎不是一个理想的选择。
有什么想法吗?
I've created a dynamic HTML form that uses jQuery 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发送这些标签文本是没有用的。这是不必要的流量,也是需要过滤/验证的另一件事。
您在服务器端创建表单,因此您已经可以访问那里的标签文本。我建议您将这些文本存储在常量中,例如:
因此,当您创建表单时,您只需键入:
并在构建电子邮件正文时使用相同的常量(
TEXT_EMAIL
和其他) 。这样,如果您需要添加对其他语言的支持,您也将处于轻松的境地。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:
So when you create the form, you can just type:
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.
如果您不通过 Ajax 请求将数据 POST 到服务器,那么考虑隐藏表单字段是正确的。
假设常规表单提交,只有
input
和textarea
元素的值被发送到服务器。添加适当的隐藏输入元素并从标签中设置这些元素的值是您唯一的选择。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
andtextarea
elements are sent to the server. Adding appropriate hidden input elements and setting the values of these from the labels is your only option.