POST extra values in an HTML <form>

陌路散爱 提交于 2019-11-28 03:32:43

问题


I have a simple form which passes the value of an <input /> element:

<form action="updaterow.php" method="POST"> 
    <input type="text" name="price" />
</form>

How can I post extra values along with the <input /> value? For example, an arbitrary string, or a variable inside the current PHP script.

I know this is possible with GET:

<form action="updaterow.php?foo=bar" method="GET">
    <input type="text" name="price" />
</form>

or:

<form action="updaterow.php?foo=<?=htmlspecialchars($bar)?>" method="GET">
    <input type="text" name="price" />
</form>

but I need POST.


回答1:


You can include a hidden form element.

<input type="hidden" name="foo" value="bar" />



回答2:


You can simply use a hidden field. Like so:

<input type="hidden" name="your-field-name" value="your-field-value" />

This field will then be available in your PHP script as $_POST['your-field-name']




回答3:


As mentioned already, using hidden input fields is an option, but you can also use a session. That way you don´t have to post anything, the variables remain on the server and are not exposed in the html.



来源:https://stackoverflow.com/questions/4598779/post-extra-values-in-an-html-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!