By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

how to read form label?

P: n/a

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?
like:
<input name="bread" type="text" size="3">
The point would be to have same single text visible on the page, and
returned by form.

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You dont.

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

eg:

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

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

- Ali

"Mikko" <ei******@invalid.net> wrote in message
news:so**************@read3.inet.fi...

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?
like:
<input name="bread" type="text" size="3">
The point would be to have same single text visible on the page, and
returned by form.


Jul 17 '05 #2

P: n/a
Mikko wrote:
Is it possible to read "label" from form-field?

<label for="product1">bread</label>
<input id="product1" name="product1" type="text" size="3">
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.
number comes with $_GET[product1];

but how do I read the "bread"?
Or is there a way to print the "name" to form?
like:
<input name="bread" type="text" size="3">
The point would be to have same single text visible on the page, and
returned by form.


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.

Ken

Jul 17 '05 #3

P: n/a
Ken Robinson kirjoitti:
Mikko wrote:
Is it possible to read "label" from form-field?

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

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.
number comes with $_GET[product1];

but how do I read the "bread"?
Or is there a way to print the "name" to form?
like:
<input name="bread" type="text" size="3">
The point would be to have same single text visible on the page, and
returned by form.

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.

Ken


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
to
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
field-names.

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

Jul 17 '05 #4

P: n/a
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."
www.myriadintellect.com

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

<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_" + $product.id;
$label = document.getElementById($label_id);
$label_value = $label.innerHTML;
//Take our gathered info and append a hidden element to the form for the
label.
var $new_element = document.createElement("<input>");
$new_element.setAttribute('type', 'hidden');
$new_element.setAttribute('name', $label_id);
$new_element.setAttribute('value', $label_value);
$form.appendChild($new_element);
return true;
}
//-->
</script>
</head>

<body>
<form id ="products" name="products"
action="http://www.phpgeek.com/util/formdump.php" method="get"
onsubmit="submit_labels();">

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

</body>
</html>
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.