brux
brux

Reputation: 3230

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.

Upvotes: 17

Views: 21832

Answers (3)

jeroen
jeroen

Reputation: 91734

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.

Upvotes: 4

alexn
alexn

Reputation: 58962

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

Upvotes: 10

sberry
sberry

Reputation: 131978

You can include a hidden form element.

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

Upvotes: 42

Related Questions