tags:

views:

115

answers:

2

input type="text" value="5". I enter some text like(abc) in textbox . I needed to store text in database with id 5. So i needed value in hidden mode. How to make value in hidden mode.

+4  A: 

Add an additional field to the page. In this example the first is the normal text field, the second will be hidden and will not show up:

<input type="text" value="" name="text" />
<input type="hidden" value="5" name="id" />

Then on the server, access both text and id to save. In PHP it would look like this:

$text = $_POST['text'];
$id   = (int)$_POST['id'];

Be aware that this is easy to tamper with on the client side, so be sure to do whatever tests are necessary on the server to make sure it wasn't hacked.

Doug Neiner
@Doug Neiner: so you know which technologies he is talking about? :)
Sarfraz
@Sarfraz the post is tagged HTML, I can only imagine that is what he means. Since he didn't specify a server technology, I just said "In PHP it would look like..." and gave an example. The answer was the `input type=hidden`.... I think :)
Doug Neiner
@Doug Neiner: great answer, thanks :)
Sarfraz
+1  A: 

Doug Neiner's answer is the preferred method, however as an ALTERNATIVE, if your fields are dynamically generated, you could do what SO does, which is incorporate the ID# into the id attribute and then parse, such as id="myText-1234 where 1234 is the id you want to pass. Then you can split() the id of the text box by "-" and grab the id, so long as your convention remains consistent.

Jason
Great suggestion, but it should "name" not "id".
Doug Neiner
"id" could work because it is guaranteed to be unique (so long as that is your convention). also, "name" is not a valid attribute on all html elements, where "id" is. however, for the question at hand, "name" could work just as well.
Jason
@Jason, `id` is never sent to the server. Perhaps I am reading too much into an already unclear question, but if the OP is using a `form` and a normal `input` then the only way to pass the text content *and* the id in one set as you suggest, would be to put it in the `name` attribute. That was what I meant. If he were using AJAX and pre-processing the data, then of course he could use `id`.
Doug Neiner