PHP - 如何加载相同的表单,但在字段类型方面存在一些差异?
我有一个表单,其中包含一些文本类型的字段和 2 个默认具有值的字段(name
和 second name
),这 2 个字段的 type="hidden ”。
现在,我想在“为另一个人填写订单”表单上添加一个链接。如果用户单击此链接,则必须重新加载相同的表单,但有一点不同:名称和第二名字段必须是文本类型,以允许用户输入姓名/第二名。
我可以重新加载表单,但字段 name
和 second name
仍然隐藏,因为我不知道如何定义一个条件,即在用户单击时更改类型在链接上。你能解释一下我该怎么做吗?
默认填写的字段:
if ($field_label=="Name")
{
return sprintf("<div class='ginput_container'>$name<input name='input_%d' id='$field_id' type='hidden' value='$name' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
else if ($field_label=="Second name")
{
return sprintf("<div class='ginput_container'>$secondname<input name='input_%d' id='$field_id' type='hidden' value='$secondname' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
I have a form with some fields of type text and 2 fields (name
and second name
) that have a value by default, and these 2 fields are of type="hidden".
Now, I would like to add a link on the form "Fill an order for another person". If the user click on this link the same form must be reloaded but with a little difference: the fields name and second name must be of type text to allow the user enter a name/second name.
I can reload the form, but the fields name
and second name
remain hidden, because I don't know how define a condition that say to change the types if the user clicks on the link. Could you explain how can I do this?
The fields that are filled by default:
if ($field_label=="Name")
{
return sprintf("<div class='ginput_container'>$name<input name='input_%d' id='$field_id' type='hidden' value='$name' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
else if ($field_label=="Second name")
{
return sprintf("<div class='ginput_container'>$secondname<input name='input_%d' id='$field_id' type='hidden' value='$secondname' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加另一个输入字段并将其命名为“form_subscribed”。将其值设置为 1。
提交表单后,您将能够在 php 脚本中检查这一点。如果设置此值,则使用文本,如果不设置,则使用隐藏
编辑:如果您只想显示一个附加表单,您可以在页面加载时创建它,并使用 javascript 使其在用户单击链接后可见
如果您希望能够添加多个表单,请使用 javascript 构建新表单。每次点击链接都可以添加另一个表单。您唯一需要确保的是更新表单元素的名称。
add another input field and call it something like "form_submitted". set the value of this to 1.
you will be able to check for this in your php script once you've submitted the form. if this value is set use text if not use hidden
EDIT: If you want to only display one additional form you can create it on page load and use javascript to make it visible once a user clicks on the link
if you want to be able to add multiple forms, use javascript to build the new ones. each click on the link can add another form. the only thing you need to make sure is that you update the name of the form elements.
您无需重新加载表格。您可以使用 jQuery 删除现有的隐藏字段,并在其位置创建一个新的文本框,其值与隐藏字段相同。
例如: http://jsfiddle.net/FT2B3/1/
You do not need to reload the form. You can just use jQuery to remove the existing hidden field, and create a new textbox in it's place, with the same value as the hidden one.
For example: http://jsfiddle.net/FT2B3/1/