17

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.

3 Answers 3

42

You can include a hidden form element.

<input type="hidden" name="foo" value="bar" />
Sign up to request clarification or add additional context in comments.

2 Comments

is having hidden elements a bad practice at all?
@Abdul No. In fact, it's the only way to solve this particular problem!
10

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

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.