Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hidden field in php

Tags:

html

php

For hidden fields, can i use a field of the type

<input type="hidden" name="field_name" value="<?php print $var; ?>"/>

and retrieve it after the GET / POST method as $_GET['field_name'] / $_POST['field_name'] ?

are there any other ways of using hidden fields in php?

like image 673
user550265 Avatar asked Feb 09 '11 20:02

user550265


People also ask

What are hidden fields in PHP?

A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

What is hidden form field?

Hidden form field is used to store session information of a client. In this method, we create a hidden form which passes the control to the servlet whose path is given in the form action area. Using this, the information of the user is stored and passed to the location where we want to send data.


2 Answers

You absolutely can, I use this approach a lot w/ both JavaScript and PHP.

Field definition:

<input type="hidden" name="foo" value="<?php echo $var;?>" />

Access w/ PHP:

$_GET['foo'] or $_POST['foo']

Also: Don't forget to sanitize your inputs if they are going into a database. Feel free to use my routine: https://github.com/niczak/PHP-Sanitize-Post/blob/master/sanitize.php

Cheers!

like image 163
niczak Avatar answered Oct 06 '22 05:10

niczak


Yes, you can access it through GET and POST (trying this simple task would have made you aware of that).

Yes, there are other ways, one of the other "preferred" ways is using sessions. When you would want to use hidden over session is kind of touchy, but any GET / POST data is easily manipulated by the end user. A session is a bit more secure given it is saved to a file on the server and it is much harder for the end user to manipulate without access through the program.

like image 28
Jim Avatar answered Oct 06 '22 04:10

Jim