Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST extra values in an HTML <form>

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.

like image 307
brux Avatar asked Jan 04 '11 21:01

brux


People also ask

How do you post data in HTML?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

Is post allowed in HTML forms?

POST: In the post method, after the submission of the form, the form values will not be visible in the address bar of the new browser tab as it was visible in the GET method. It appends form data inside the body of the HTTP request. It has no size limitation. This method does not support bookmark the result.

How do you write a post in HTML?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.


3 Answers

You can include a hidden form element.

<input type="hidden" name="foo" value="bar" />
like image 186
sberry Avatar answered Oct 17 '22 07:10

sberry


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']

like image 28
alexn Avatar answered Oct 17 '22 07:10

alexn


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.

like image 4
jeroen Avatar answered Oct 17 '22 07:10

jeroen