how to read form label?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mikko

    how to read form label?


    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.





  • Alistair Baillie SS2002

    #2
    Re: how to read form label?

    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" <eipostia@inval id.net> wrote in message
    news:soIfe.135$ At4.90@read3.in et.fi...[color=blue]
    >
    > 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.
    >
    >
    >
    >
    >[/color]


    Comment

    • Ken Robinson

      #3
      Re: how to read form label?

      Mikko wrote:[color=blue]
      > Is it possible to read "label" from form-field?
      >
      > <label for="product1"> bread</label>
      > <input id="product1" name="product1" type="text" size="3">[/color]

      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.[color=blue]
      >
      > 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.[/color]

      You could use hidden fields for this:
      <input name="product_n ame" 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

      Comment

      • Mikko

        #4
        Re: how to read form label?

        Ken Robinson kirjoitti:[color=blue]
        > Mikko wrote:
        >[color=green]
        >>Is it possible to read "label" from form-field?
        >>
        >><label for="product1"> bread</label>
        >><input id="product1" name="product1" type="text" size="3">[/color]
        >
        >
        > 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.
        >[color=green]
        >>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.[/color]
        >
        >
        > You could use hidden fields for this:
        > <input name="product_n ame" 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
        >[/color]

        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".



        Comment

        • J Wynia

          #5
          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">
          <html>
          <head>
          <title></title>

          <script language="JavaS cript">
          <!--
          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.getEle mentById("produ cts");
          $product = document.getEle mentById("produ ct1");
          $label_id = "label_" + $product.id;
          $label = document.getEle mentById($label _id);
          $label_value = $label.innerHTM L;
          //Take our gathered info and append a hidden element to the form for the
          label.
          var $new_element = document.create Element("<input >");
          $new_element.se tAttribute('typ e', 'hidden');
          $new_element.se tAttribute('nam e', $label_id);
          $new_element.se tAttribute('val ue', $label_value);
          $form.appendChi ld($new_element );
          return true;
          }
          //-->
          </script>
          </head>

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

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

          </body>
          </html>


          Comment

          Working...