forms with dynamic inputs

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

    forms with dynamic inputs

    Hi Gurus

    I have a php page with a form in it, that is along the lines of:

    <form...>
    item with id 234
    <input type=submit ... NAME=add234 ID=add234>
    <input type=hidden NAME=add234 ID=234>
    item with id 235
    <input type=submit ... NAME=add235 ID=add235>
    <input type=hidden NAME=add235 ID=235>
    [....etc....]
    </form>

    In the PHP code at the top of the document, I want to:

    a. intercept any submit from the form
    b. get the related ID number

    At the moment, I have only one add button, so I use code like this:

    if($HTTP_POST_V ARS[add]){
    ...
    .....
    }

    So my questions are:

    A. how can I change

    if($HTTP_POST_V ARS[add]){

    to make it intercept any submit button that starts with add

    B. how can I get the corresponding ID number


    TIA

    - Nicolaas


  • News Me

    #2
    Re: forms with dynamic inputs

    WindAndWaves wrote:[color=blue]
    > Hi Gurus
    >
    > I have a php page with a form in it, that is along the lines of:
    >
    > <form...>
    > item with id 234
    > <input type=submit ... NAME=add234 ID=add234>
    > <input type=hidden NAME=add234 ID=234>
    > item with id 235
    > <input type=submit ... NAME=add235 ID=add235>
    > <input type=hidden NAME=add235 ID=235>
    > [....etc....]
    > </form>
    >
    > In the PHP code at the top of the document, I want to:
    >
    > a. intercept any submit from the form
    > b. get the related ID number
    >
    > At the moment, I have only one add button, so I use code like this:
    >
    > if($HTTP_POST_V ARS[add]){
    > ...
    > ....
    > }
    >
    > So my questions are:
    >
    > A. how can I change
    >
    > if($HTTP_POST_V ARS[add]){
    >
    > to make it intercept any submit button that starts with add
    >
    > B. how can I get the corresponding ID number
    >
    >
    > TIA
    >
    > - Nicolaas
    >
    >[/color]

    You can't get at the element id from within PHP, as that lives in the
    client. Instead, give the submit buttons the same name (e.g.,
    "submitted_ id") but a unique value. Then when any submit button is
    clicked, the common name will have the unique value:

    item with id 234
    <input type='submit' ... name='submitted _id' value='add234'>
    item with id 235
    <input type='submit' ... name='submitted _id' value='add235'>
    item with id 236
    <input type='submit' ... name='submitted _id' value='add236'>


    Then in your code:

    $item_id = $HTTP_POST_VARS['submitted_id'];

    $item_id will be one of 'add234', 'add235', or 'add236'.

    NM

    --
    convert uppercase WORDS to single keystrokes to reply

    Comment

    • WindAndWaves

      #3
      Re: forms with dynamic inputs


      "News Me" <newsTWOme@paci fierDOTcom> wrote in message
      news:10uhke2a79 me54b@corp.supe rnews.com...[color=blue]
      > WindAndWaves wrote:[color=green]
      > > Hi Gurus
      > >
      > > I have a php page with a form in it, that is along the lines of:
      > >
      > > <form...>
      > > item with id 234
      > > <input type=submit ... NAME=add234 ID=add234>
      > > <input type=hidden NAME=add234 ID=234>
      > > item with id 235
      > > <input type=submit ... NAME=add235 ID=add235>
      > > <input type=hidden NAME=add235 ID=235>
      > > [....etc....]
      > > </form>
      > >
      > > In the PHP code at the top of the document, I want to:
      > >
      > > a. intercept any submit from the form
      > > b. get the related ID number
      > >
      > > At the moment, I have only one add button, so I use code like this:
      > >
      > > if($HTTP_POST_V ARS[add]){
      > > ...
      > > ....
      > > }
      > >
      > > So my questions are:
      > >
      > > A. how can I change
      > >
      > > if($HTTP_POST_V ARS[add]){
      > >
      > > to make it intercept any submit button that starts with add
      > >
      > > B. how can I get the corresponding ID number
      > >
      > >
      > > TIA
      > >
      > > - Nicolaas
      > >
      > >[/color]
      >
      > You can't get at the element id from within PHP, as that lives in the
      > client. Instead, give the submit buttons the same name (e.g.,
      > "submitted_ id") but a unique value. Then when any submit button is
      > clicked, the common name will have the unique value:
      >
      > item with id 234
      > <input type='submit' ... name='submitted _id' value='add234'>
      > item with id 235
      > <input type='submit' ... name='submitted _id' value='add235'>
      > item with id 236
      > <input type='submit' ... name='submitted _id' value='add236'>
      >
      >
      > Then in your code:
      >
      > $item_id = $HTTP_POST_VARS['submitted_id'];
      >
      > $item_id will be one of 'add234', 'add235', or 'add236'.
      >[/color]


      Thank you for your reply. That is simpler than I thought. The only problem
      I have now is that I don't want the text "add..." to appear on page. All I
      want the user to see is "add to basket"....

      Do you have any hints on this?


      Comment

      • News Me

        #4
        Re: forms with dynamic inputs

        WindAndWaves wrote:[color=blue]
        > "News Me" <newsTWOme@paci fierDOTcom> wrote in message
        > news:10uhke2a79 me54b@corp.supe rnews.com...
        >[color=green]
        >>WindAndWave s wrote:
        >>[color=darkred]
        >>>Hi Gurus
        >>>
        >>>I have a php page with a form in it, that is along the lines of:
        >>>
        >>><form...>
        >>> item with id 234
        >>> <input type=submit ... NAME=add234 ID=add234>
        >>> <input type=hidden NAME=add234 ID=234>
        >>> item with id 235
        >>> <input type=submit ... NAME=add235 ID=add235>
        >>> <input type=hidden NAME=add235 ID=235>
        >>>[....etc....]
        >>></form>
        >>>
        >>>In the PHP code at the top of the document, I want to:
        >>>
        >>>a. intercept any submit from the form
        >>>b. get the related ID number
        >>>
        >>>At the moment, I have only one add button, so I use code like this:
        >>>
        >>>if($HTTP_POS T_VARS[add]){
        >>> ...
        >>>....
        >>>}
        >>>
        >>>So my questions are:
        >>>
        >>>A. how can I change
        >>>
        >>>if($HTTP_POS T_VARS[add]){
        >>>
        >>>to make it intercept any submit button that starts with add
        >>>
        >>>B. how can I get the corresponding ID number
        >>>
        >>>
        >>>TIA
        >>>
        >>>- Nicolaas
        >>>
        >>>[/color]
        >>
        >>You can't get at the element id from within PHP, as that lives in the
        >>client. Instead, give the submit buttons the same name (e.g.,
        >>"submitted_id ") but a unique value. Then when any submit button is
        >>clicked, the common name will have the unique value:
        >>
        >>item with id 234
        >><input type='submit' ... name='submitted _id' value='add234'>
        >>item with id 235
        >><input type='submit' ... name='submitted _id' value='add235'>
        >>item with id 236
        >><input type='submit' ... name='submitted _id' value='add236'>
        >>
        >>
        >>Then in your code:
        >>
        >>$item_id = $HTTP_POST_VARS['submitted_id'];
        >>
        >>$item_id will be one of 'add234', 'add235', or 'add236'.
        >>[/color]
        >
        >
        >
        > Thank you for your reply. That is simpler than I thought. The only problem
        > I have now is that I don't want the text "add..." to appear on page. All I
        > want the user to see is "add to basket"....
        >
        > Do you have any hints on this?
        >
        >[/color]

        Whoops! I forgot that the value appears on the button. Instead of
        using <input type="submit" ...>, use a <button> like this:

        <button type="submit" name="submitted _id" value="add234"> BUTTON
        TEXT</button>

        You could even drop the "add" from the value so that you just get the
        number back.

        NM


        --
        convert uppercase WORDS to single keystrokes to reply

        Comment

        • WindAndWaves

          #5
          Re: forms with dynamic inputs


          "News Me" <newsTWOme@paci fierDOTcom> wrote in message
          news:10uhlkf3d3 1no84@corp.supe rnews.com...[color=blue]
          > WindAndWaves wrote:[color=green]
          > > "News Me" <newsTWOme@paci fierDOTcom> wrote in message
          > > news:10uhke2a79 me54b@corp.supe rnews.com...
          > >[color=darkred]
          > >>WindAndWave s wrote:
          > >>
          > >>>Hi Gurus
          > >>>
          > >>>I have a php page with a form in it, that is along the lines of:
          > >>>
          > >>><form...>
          > >>> item with id 234
          > >>> <input type=submit ... NAME=add234 ID=add234>
          > >>> <input type=hidden NAME=add234 ID=234>
          > >>> item with id 235
          > >>> <input type=submit ... NAME=add235 ID=add235>
          > >>> <input type=hidden NAME=add235 ID=235>
          > >>>[....etc....]
          > >>></form>
          > >>>
          > >>>In the PHP code at the top of the document, I want to:
          > >>>
          > >>>a. intercept any submit from the form
          > >>>b. get the related ID number
          > >>>
          > >>>At the moment, I have only one add button, so I use code like this:
          > >>>
          > >>>if($HTTP_POS T_VARS[add]){
          > >>> ...
          > >>>....
          > >>>}
          > >>>
          > >>>So my questions are:
          > >>>
          > >>>A. how can I change
          > >>>
          > >>>if($HTTP_POS T_VARS[add]){
          > >>>
          > >>>to make it intercept any submit button that starts with add
          > >>>
          > >>>B. how can I get the corresponding ID number
          > >>>
          > >>>
          > >>>TIA
          > >>>
          > >>>- Nicolaas
          > >>>
          > >>>
          > >>
          > >>You can't get at the element id from within PHP, as that lives in the
          > >>client. Instead, give the submit buttons the same name (e.g.,
          > >>"submitted_id ") but a unique value. Then when any submit button is
          > >>clicked, the common name will have the unique value:
          > >>
          > >>item with id 234
          > >><input type='submit' ... name='submitted _id' value='add234'>
          > >>item with id 235
          > >><input type='submit' ... name='submitted _id' value='add235'>
          > >>item with id 236
          > >><input type='submit' ... name='submitted _id' value='add236'>
          > >>
          > >>
          > >>Then in your code:
          > >>
          > >>$item_id = $HTTP_POST_VARS['submitted_id'];
          > >>
          > >>$item_id will be one of 'add234', 'add235', or 'add236'.
          > >>[/color]
          > >
          > >
          > >
          > > Thank you for your reply. That is simpler than I thought. The only[/color][/color]
          problem[color=blue][color=green]
          > > I have now is that I don't want the text "add..." to appear on page.[/color][/color]
          All I[color=blue][color=green]
          > > want the user to see is "add to basket"....
          > >
          > > Do you have any hints on this?
          > >
          > >[/color]
          >
          > Whoops! I forgot that the value appears on the button. Instead of
          > using <input type="submit" ...>, use a <button> like this:
          >
          > <button type="submit" name="submitted _id" value="add234"> BUTTON
          > TEXT</button>
          >
          > You could even drop the "add" from the value so that you just get the
          > number back.[/color]

          you are a genius. I should have thought about it myself!

          Thank you


          Comment

          • News Me

            #6
            Re: forms with dynamic inputs

            News Me wrote:
            [color=blue]
            >
            > Whoops! I forgot that the value appears on the button. Instead of
            > using <input type="submit" ...>, use a <button> like this:
            >
            > <button type="submit" name="submitted _id" value="add234"> BUTTON
            > TEXT</button>
            >[/color]

            (I'm sleepy...) I guess that should have been:

            <button type="submit" name="submitted _id" value="234">Add To Basket
            </button>

            NM

            --
            convert uppercase WORDS to single keystrokes to reply

            Comment

            • WindAndWaves

              #7
              Re: forms with dynamic inputs


              "News Me" <newsTWOme@paci fierDOTcom> wrote in message
              news:10uhm826q6 mt74a@corp.supe rnews.com...[color=blue]
              > News Me wrote:
              >[color=green]
              > >
              > > Whoops! I forgot that the value appears on the button. Instead of
              > > using <input type="submit" ...>, use a <button> like this:
              > >
              > > <button type="submit" name="submitted _id" value="add234"> BUTTON
              > > TEXT</button>
              > >[/color]
              >
              > (I'm sleepy...) I guess that should have been:
              >
              > <button type="submit" name="submitted _id" value="234">Add To Basket
              > </button>
              >
              > NM
              >[/color]


              Hi NM

              I have the following:

              <BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14" CLASS="addtotri p">add
              to trip</BUTTON>

              but it tells me that the value of the add button is "add to trip" and not
              14, as I would have liked ....

              what am I doing wrong?

              - Nicolaas


              Comment

              • News Me

                #8
                Re: forms with dynamic inputs

                WindAndWaves wrote:[color=blue]
                > "News Me" <newsTWOme@paci fierDOTcom> wrote in message
                > news:10uhm826q6 mt74a@corp.supe rnews.com...
                >[color=green]
                >>News Me wrote:
                >>
                >>[color=darkred]
                >>>Whoops! I forgot that the value appears on the button. Instead of
                >>>using <input type="submit" ...>, use a <button> like this:
                >>>
                >>><button type="submit" name="submitted _id" value="add234"> BUTTON
                >>>TEXT</button>
                >>>[/color]
                >>(I'm sleepy...) I guess that should have been:
                >>
                >><button type="submit" name="submitted _id" value="234">Add To Basket
                >></button>
                >>
                >>NM
                >>[/color]
                >
                >
                >
                > Hi NM
                >
                > I have the following:
                >
                > <BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14" CLASS="addtotri p">add
                > to trip</BUTTON>
                >
                > but it tells me that the value of the add button is "add to trip" and not
                > 14, as I would have liked ....
                >
                > what am I doing wrong?
                >
                > - Nicolaas
                >
                >[/color]

                Replace this:

                ID="add14"

                with this:

                VALUE="add14"

                NM

                --
                convert uppercase WORDS to single keystrokes to reply

                Comment

                • News Me

                  #9
                  Re: forms with dynamic inputs

                  WindAndWaves wrote:

                  Ignore that last message, my eyes are tired...

                  Hmmm... I'll have to try this myself.

                  NM

                  --
                  convert uppercase WORDS to single keystrokes to reply

                  Comment

                  • Lāʻie Techie

                    #10
                    Re: forms with dynamic inputs

                    On Sat, 15 Jan 2005 00:38:49 -0800, News Me wrote:
                    [color=blue]
                    > Whoops! I forgot that the value appears on the button. Instead of using
                    > <input type="submit" ...>, use a <button> like this:
                    >
                    > <button type="submit" name="submitted _id" value="add234"> BUTTON
                    > TEXT</button>
                    >
                    > You could even drop the "add" from the value so that you just get the
                    > number back.
                    >
                    > NM[/color]

                    Stay clear of the <button> element!

                    IE has a bad implementation in which the name / value pairs of ALL
                    <button> elements within the form are submitted - not just the one that
                    was clicked.

                    Use submit buttons with name of add_{$id}. Simply grep (ie run a regex
                    on) all the form values submitted for "add_\d+" (assuming that all IDs are
                    numeric).

                    HTH,
                    La'ie Techie

                    Comment

                    • WindAndWaves

                      #11
                      Re: forms with dynamic inputs


                      "News Me" <newsTWOme@paci fierDOTcom> wrote in message
                      news:10uhofeg1s s1m4c@corp.supe rnews.com...[color=blue]
                      > WindAndWaves wrote:
                      >
                      > Ignore that last message, my eyes are tired...
                      >
                      > Hmmm... I'll have to try this myself.
                      >
                      > NM[/color]


                      I actually discovered that if I do not make it input type="submit" that I
                      run into other difficulties: the data is not really reposted and the old
                      data is kept instead. IN that way the user can only add the first product.

                      Well, anyway, let me knows

                      THANKS a lot for your help.


                      Comment

                      • WindAndWaves

                        #12
                        Re: forms with dynamic inputs


                        "La?ie Techie" <laie@win_remov e_get_nospam_so lutions.com> wrote in message
                        news:1105781619 .e9541b67e05391 c93e4ff68fcf4dc 4de@teranews...[color=blue]
                        > On Sat, 15 Jan 2005 00:38:49 -0800, News Me wrote:[/color]

                        [....]
                        [color=blue]
                        > Use submit buttons with name of add_{$id}. Simply grep (ie run a regex
                        > on) all the form values submitted for "add_\d+" (assuming that all IDs are
                        > numeric).
                        >
                        > HTH,
                        > La'ie Techie
                        >[/color]

                        Could you translate that to novice PHP, sorry, but this is my very first
                        site that I have taken over from someone, and it is all a bit new to me.

                        I have changed it back to a submit button, I can add the
                        name="'.$rowIdn umber.'", but the rest I don't understand at all - lol

                        - Nicolaas


                        Comment

                        • News Me

                          #13
                          Re: forms with dynamic inputs

                          WindAndWaves wrote:[color=blue]
                          > Hi NM
                          >
                          > I have the following:
                          >
                          > <BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14" CLASS="addtotri p">add
                          > to trip</BUTTON>
                          >
                          > but it tells me that the value of the add button is "add to trip" and not
                          > 14, as I would have liked ....
                          >
                          > what am I doing wrong?[/color]

                          I just put the following onto a test page:

                          <form action="" method="get">
                          <BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14"
                          CLASS="addtotri p">add to trip</BUTTON>
                          <BUTTON TYPE="submit" NAME="add" VALUE="15" ID="add15"
                          CLASS="addtotri p">add to trip</BUTTON>
                          </form>

                          And when I press the first button the URL changes to this:



                          and when I press the second button I get this:



                          So the correct value is being returned. How are you extracting the
                          value in your PHP code?

                          NM

                          --
                          convert uppercase WORDS to single keystrokes to reply

                          Comment

                          • WindAndWaves

                            #14
                            Re: forms with dynamic inputs


                            "News Me" <newsTWOme@paci fierDOTcom> wrote in message
                            news:10uhp0relh nap9e@corp.supe rnews.com...[color=blue]
                            > WindAndWaves wrote:[color=green]
                            > > Hi NM
                            > >
                            > > I have the following:
                            > >
                            > > <BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14"[/color][/color]
                            CLASS="addtotri p">add[color=blue][color=green]
                            > > to trip</BUTTON>
                            > >
                            > > but it tells me that the value of the add button is "add to trip" and[/color][/color]
                            not[color=blue][color=green]
                            > > 14, as I would have liked ....
                            > >
                            > > what am I doing wrong?[/color]
                            >
                            > I just put the following onto a test page:
                            >
                            > <form action="" method="get">
                            > <BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14"
                            > CLASS="addtotri p">add to trip</BUTTON>
                            > <BUTTON TYPE="submit" NAME="add" VALUE="15" ID="add15"
                            > CLASS="addtotri p">add to trip</BUTTON>
                            > </form>
                            >
                            > And when I press the first button the URL changes to this:
                            >
                            > http://localhost/test/?add=14
                            >
                            > and when I press the second button I get this:
                            >
                            > http://localhost/test/?add=15
                            >
                            > So the correct value is being returned. How are you extracting the
                            > value in your PHP code?[/color]


                            This is all really:

                            <?php
                            session_start() ;

                            // Initialize variables
                            $errormessage = "";
                            $sqlStart = "SELECT a.id, a.FN, a.name, a.description, a.photolink,
                            a.regionId FROM friars a, regions b";
                            $sqlWhere = " WHERE (a.regionId = b.id) AND ";
                            $sqlEnd = " GROUP BY a.regionId, a.name ";
                            $humanSQL = "<UL>";
                            $maxrows = 50;

                            // If no sortorder is set, initialize it to 1
                            if(!$sortorder) {
                            $_SESSION["sortorder"] = 1;
                            }


                            //If an Accommodation is to be added
                            if($HTTP_POST_V ARS[add]){

                            // Add accomodation to the session with an incremented sortorder
                            $_SESSION["sortorder"] = $sortorder + 1;

                            $id = 0;
                            $id = $HTTP_POST_VARS['add'];
                            echo '************** *** ID = '.$id;

                            // Select DB data for accommodation to ADD and Append to Session
                            include_once("c onnectDB.php");
                            $sqladd = "SELECT * FROM friars WHERE id = $id";
                            $resultAdd = mysql_query($sq ladd);
                            $myrowAdd = mysql_fetch_arr ay($resultAdd);

                            .....lots of other code

                            ?>
                            <html .....

                            <button ..[as discussed]..>

                            ..... /html>


                            Comment

                            • News Me

                              #15
                              Re: forms with dynamic inputs

                              WindAndWaves wrote:

                              [snip]

                              <?php[color=blue]
                              > session_start() ;
                              >
                              > // Initialize variables
                              > $errormessage = "";
                              > $sqlStart = "SELECT a.id, a.FN, a.name, a.description, a.photolink,
                              > a.regionId FROM friars a, regions b";
                              > $sqlWhere = " WHERE (a.regionId = b.id) AND ";
                              > $sqlEnd = " GROUP BY a.regionId, a.name ";
                              > $humanSQL = "<UL>";
                              > $maxrows = 50;
                              >
                              > // If no sortorder is set, initialize it to 1
                              > if(!$sortorder) {
                              > $_SESSION["sortorder"] = 1;
                              > }
                              >
                              >
                              > //If an Accommodation is to be added
                              > if($HTTP_POST_V ARS[add]){
                              >
                              > // Add accomodation to the session with an incremented sortorder
                              > $_SESSION["sortorder"] = $sortorder + 1;
                              >
                              > $id = 0;
                              > $id = $HTTP_POST_VARS['add'];
                              > echo '************** *** ID = '.$id;
                              >[/color]

                              [snip]

                              Looks like that should work to me. I just read La'ie Techie's post.
                              Are you using IE? I'm using FireFox.

                              NM

                              --
                              convert uppercase WORDS to single keystrokes to reply

                              Comment

                              Working...