how to read form label?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Is it possible to read "label" from form-field?

<label for="product1">bread</label>
<input id="product1" name="product1" type="text" size="3">

number comes with $_GET[product1];

but how do I read the "bread"?

Or is there a way to print the "name" to form?
<input name="bread" type="text" size="3">

The point would be to have same single text visible on the page, and
returned by form.

Re: how to read form label?

Mikko wrote:
Quoted text here. Click to load it

Not from PHP, you probably can do it from Javascript. Remember, PHP
runs on the server and has no knowledge of what anything looks like on
the screen or the code that displayed it. It just knows of information
that the web servers obtain from then browsers.
Quoted text here. Click to load it

You could use hidden fields for this:
<input name="product_name" type=hidden value="bread">

Or you could use an array for the name:
<input id="product1" name="product1[bread]" type="text" size="3">

And reference it by $_GET['product1']['bread']

It depends on what you are trying to do.


Re: how to read form label?

Ken Robinson kirjoitti:
Quoted text here. Click to load it

The meaning would be to have only one field, that can be modified easily
with Contribute.

So if updater wants to change "bread" in form to "puppet" the text in
page would change from bread to puppet. And the text returned by PHP
would change from
bread: 3
puppet: 3
(3 is from textfield in the form)

I would make that 1 entry as editable area by dreamweaver. So user could
update hes "store" himself, without needing to know about PHP or

If he just changes the text, the php will still return something like
field1: 3, unless I can read the new text "puppet".

Re: how to read form label?

Below is a complete example document. It's strung together and not
organized like a real implementation should be, but shows you a way to
grab an associated label for an element and tack on a new hidden form
element to pass it along as though it was there all along.

Watch out for line wrapping.

J Wynia
Myriad Intellect, Inc.
"Web technology that earns its keep."

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<script language="JavaScript">
function submit_labels(){
//Gather information from the form and the element in question
//A more abstract version of this function should really be used where the
//form is submitted and all labels synced up with matching hidden values.
$form = document.getElementById("products");
$product = document.getElementById("product1");
$label_id = "label_" + $;
$label = document.getElementById($label_id);
$label_value = $label.innerHTML;
//Take our gathered info and append a hidden element to the form for the
var $new_element = document.createElement("<input>");
$new_element.setAttribute('type', 'hidden');
$new_element.setAttribute('name', $label_id);
$new_element.setAttribute('value', $label_value);
return true;

<form id ="products" name="products"
action=" " method="get"

<label for="product1" id="label_product1">bread</label>
<input id="product1" name="product1" type="text" size="3">
<input type="submit">


Re: how to read form label?

You dont.

have an array or other way of matching the product numbers to a type;


$_MY_PRODUCTS[1] = "Bread";
$_MY_PRODUCTS[2] = "Soup";

echo $_MY_PRODUCTS[ $_GET['product1'] ];

- Ali

Quoted text here. Click to load it

Site Timeline