2

is there a way to create custom post variables when a user presses submit, like this:

$_POST['var'] = 'hi';

3 Answers 3

9

In order to set post values on the page with the form you should use hidden input tags.

i.e.

<input type="hidden" name="var" value="hi" />

It will be invisible and your receiving script will see that key/value passed along.

Sign up to request clarification or add additional context in comments.

Comments

1

Variables POSTed by the browser to your PHP script will only correspond to the fields of the form that was used in the browser -- which means you have to put your custom data in that form.


If you don't want them displayed, you can use a hidden input field :

<input type="hidden" name="var" value="hi" />

But note that the data will still be sent by the browser -- which means you have to escape/filter/protect it, like any other value that comes from the user ; and it cannot be trusted : anyone can pretty easily modify the value of that form field, even if it's not visible.

1 Comment

+1 for pointing out that anything from the browser should be considered tampered with.
0

while $_POST variable is an array, you can also define var like this

$_POST['var'] = 'hi';

it is same like hidden field. :)

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.