How to retrieve the text of a drop down list after a POST?

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

    How to retrieve the text of a drop down list after a POST?


    Hello guys,

    <SELECT name = "f_hello">
    <OPTION value = 'H'>Hello</OPTION>"
    <OPTION value = 'G'>Good-Bye</OPTION>"
    </SELECT>

    In the case above, I can easily retrieve the f_hello variable which will
    containts H or G after a POST. But how could I retrieve the "Hello" or
    "Good-Bye" string?

    Thank you,
    Denis


  • rlee0001

    #2
    Re: How to retrieve the text of a drop down list after a POST?


    Zorro wrote:[color=blue]
    > Hello guys,
    >
    > <SELECT name = "f_hello">
    > <OPTION value = 'H'>Hello</OPTION>"
    > <OPTION value = 'G'>Good-Bye</OPTION>"
    > </SELECT>
    >
    > In the case above, I can easily retrieve the f_hello variable which will
    > containts H or G after a POST. But how could I retrieve the "Hello" or
    > "Good-Bye" string?
    >
    > Thank you,
    > Denis[/color]

    You can't automatically. You have to either pass the string itself as
    the value or use a lookup on the php processing page. For example:

    <SELECT name = "f_hello">
    <OPTION value = 'Hello'>Hello</OPTION>"
    <OPTION value = 'Good-Bye'>Good-Bye</OPTION>"
    </SELECT>

    Or alternatively you can leave the HTML the way you had it and do
    something like this:

    $myvar = ($_POST['f_hello']=='G')?'Good-Bye':'Hello';

    The above code simply looks to see if the returned value was a "G", and
    if so returns "Good-Bye". Otherwise it returns "Hello". If you have
    many values (more than two) you can use a switch case structure like
    so:

    switch ($_POST['f_hello']) {
    case 'G': $myvar='Good-Bye';break;
    case 'H': $myvar='Hello'; break;
    case 'P': $myvar='Please' ;break;
    case 'T': $myvar='Thank You';break;
    }

    The best option however is to put the correct value in the value clause
    in the HTML to begin with as per example 1 above. HTML does not limit
    the string supplied in the value clause to only one character. Almost
    any text can be present here with a few exceptions regarding special
    symbols.

    -Robert

    Comment

    • Geoff Berrow

      #3
      Re: How to retrieve the text of a drop down list after a POST?

      Message-ID: <1143282598.245 858.14670@i40g2 000cwc.googlegr oups.com> from
      rlee0001 contained the following:
      [color=blue]
      >The best option however is to put the correct value in the value clause
      >in the HTML to begin with as per example 1 above. HTML does not limit
      >the string supplied in the value clause to only one character. Almost
      >any text can be present here with a few exceptions regarding special
      >symbols.[/color]

      I'd just leave it out... :-)

      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Zorro

        #4
        Re: How to retrieve the text of a drop down list after a POST?

        Thanks Robert,

        My problem is the drop down list is dynamically filled with a sql query so
        I'd have to look up the string from the DB.
        Yes, I could pass the string in the value attribute like "H:Hello" also.

        I think I'll keep up with the query anyway.

        Thanks for helping.
        Cheers,
        Denis


        "rlee0001" <robeddielee@ho tmail.com> a écrit dans le message de news:
        1143282598.2458 58.14670@i40g20 00...legro ups.com...[color=blue]
        >
        > Zorro wrote:[color=green]
        >> Hello guys,
        >>
        >> <SELECT name = "f_hello">
        >> <OPTION value = 'H'>Hello</OPTION>"
        >> <OPTION value = 'G'>Good-Bye</OPTION>"
        >> </SELECT>
        >>
        >> In the case above, I can easily retrieve the f_hello variable which will
        >> containts H or G after a POST. But how could I retrieve the "Hello" or
        >> "Good-Bye" string?
        >>
        >> Thank you,
        >> Denis[/color]
        >
        > You can't automatically. You have to either pass the string itself as
        > the value or use a lookup on the php processing page. For example:
        >
        > <SELECT name = "f_hello">
        > <OPTION value = 'Hello'>Hello</OPTION>"
        > <OPTION value = 'Good-Bye'>Good-Bye</OPTION>"
        > </SELECT>
        >
        > Or alternatively you can leave the HTML the way you had it and do
        > something like this:
        >
        > $myvar = ($_POST['f_hello']=='G')?'Good-Bye':'Hello';
        >
        > The above code simply looks to see if the returned value was a "G", and
        > if so returns "Good-Bye". Otherwise it returns "Hello". If you have
        > many values (more than two) you can use a switch case structure like
        > so:
        >
        > switch ($_POST['f_hello']) {
        > case 'G': $myvar='Good-Bye';break;
        > case 'H': $myvar='Hello'; break;
        > case 'P': $myvar='Please' ;break;
        > case 'T': $myvar='Thank You';break;
        > }
        >
        > The best option however is to put the correct value in the value clause
        > in the HTML to begin with as per example 1 above. HTML does not limit
        > the string supplied in the value clause to only one character. Almost
        > any text can be present here with a few exceptions regarding special
        > symbols.
        >
        > -Robert
        >[/color]


        Comment

        • rlee0001

          #5
          Re: How to retrieve the text of a drop down list after a POST?

          Let me make sure I understand the problem correctly.

          Your table in your database looks like this?:

          COL1 COL2
          H Hello
          G Good-Bye

          And you are using Col1 (H or G) as a key into the table and you want to
          know how to read the other columns from the table when the form is
          submitted? Is that correct?

          -Robert

          Comment

          • rlee0001

            #6
            Re: How to retrieve the text of a drop down list after a POST?

            True. That would have been most elegant. :o)

            -Robert

            Comment

            • Zorro

              #7
              Re: How to retrieve the text of a drop down list after a POST?

              Yes, you are correct. After the post, I know the value (ex : H ) and I need
              the text also. Then I do a SELECT col2 FROM myTable WHERE COL1 = 'H'

              I wanted to avoid doing this query by passing both the value and the text
              through the post.

              Denis

              "rlee0001" <robeddielee@ho tmail.com> a écrit dans le message de news:
              1143288409.2206 75.136750@g10g2 00...legr oups.com...[color=blue]
              > Let me make sure I understand the problem correctly.
              >
              > Your table in your database looks like this?:
              >
              > COL1 COL2
              > H Hello
              > G Good-Bye
              >
              > And you are using Col1 (H or G) as a key into the table and you want to
              > know how to read the other columns from the table when the form is
              > submitted? Is that correct?
              >
              > -Robert
              >[/color]


              Comment

              • David Haynes

                #8
                Re: How to retrieve the text of a drop down list after a POST?

                Zorro wrote:[color=blue]
                > Yes, you are correct. After the post, I know the value (ex : H ) and I need
                > the text also. Then I do a SELECT col2 FROM myTable WHERE COL1 = 'H'
                >
                > I wanted to avoid doing this query by passing both the value and the text
                > through the post.
                >
                > Denis
                >
                > "rlee0001" <robeddielee@ho tmail.com> a écrit dans le message de news:
                > 1143288409.2206 75.136750@g10g2 00...legr oups.com...[color=green]
                >> Let me make sure I understand the problem correctly.
                >>
                >> Your table in your database looks like this?:
                >>
                >> COL1 COL2
                >> H Hello
                >> G Good-Bye
                >>
                >> And you are using Col1 (H or G) as a key into the table and you want to
                >> know how to read the other columns from the table when the form is
                >> submitted? Is that correct?
                >>
                >> -Robert
                >>[/color]
                >
                >[/color]

                <form action="foo.php " method="POST">
                <select name="foo">
                <option value="G:Good-Bye">G</option>
                <option value="H:Hello" >H</option>
                </select>
                </form>

                [foo.php]
                <?php
                if( isset($_POST['foo'] ) {
                $values = explode(':', $_POST['foo']);
                $col1 = $values[0];
                $col2 = $values[1];
                }
                ?>

                Is this what you wanted?

                -david-


                Comment

                • rlee0001

                  #9
                  Re: How to retrieve the text of a drop down list after a POST?

                  Denis,

                  David's code with work for the H:Hello syntax you posted before.

                  But be aware that the user can manipulate both strings so you shouldn't
                  trust anything you pass via POST. The best way is to do a query on the
                  destination page. It may seem redundant but it is the least error prone
                  and compared to parsing apart strings it should be easier to manage as
                  well if you change things later.

                  Is client-side JS acceptable? If so put the full string in the
                  'longvalue' attribute of the option tags. Then on form_onsubmit copy
                  that attribute's value to a seperate hidden form input element. Use
                  hiddenfield.val ue =
                  getElementById( 'myselect').get Attribute('long value') or something like
                  that (untested).

                  -Robert

                  Comment

                  • Zorro

                    #10
                    Re: How to retrieve the text of a drop down list after a POST?

                    Yes, this is a very good solution.

                    thank you guys for your help;
                    Denis

                    "rlee0001" <robeddielee@ho tmail.com> a écrit dans le message de news:
                    1143349385.5869 81.110540@i40g2 00...legr oups.com...[color=blue]
                    > Denis,
                    >
                    > David's code with work for the H:Hello syntax you posted before.
                    >
                    > But be aware that the user can manipulate both strings so you shouldn't
                    > trust anything you pass via POST. The best way is to do a query on the
                    > destination page. It may seem redundant but it is the least error prone
                    > and compared to parsing apart strings it should be easier to manage as
                    > well if you change things later.
                    >
                    > Is client-side JS acceptable? If so put the full string in the
                    > 'longvalue' attribute of the option tags. Then on form_onsubmit copy
                    > that attribute's value to a seperate hidden form input element. Use
                    > hiddenfield.val ue =
                    > getElementById( 'myselect').get Attribute('long value') or something like
                    > that (untested).
                    >
                    > -Robert
                    >[/color]


                    Comment

                    • Duderino82

                      #11
                      Re: How to retrieve the text of a drop down list after a POST?

                      what are the true risk of putting an option like this?

                      <option value="Good-Bye">Good-Bye</option>

                      what can the user do to mess up this? I don't see the problem...I did a
                      few <select> where option values are elements of a ENUM so I can't use
                      the 2 columns coding...is it dangerous? in which way?

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: How to retrieve the text of a drop down list after a POST?

                        Duderino82 wrote:[color=blue]
                        > what are the true risk of putting an option like this?
                        >
                        > <option value="Good-Bye">Good-Bye</option>
                        >
                        > what can the user do to mess up this? I don't see the problem...I did a
                        > few <select> where option values are elements of a ENUM so I can't use
                        > the 2 columns coding...is it dangerous? in which way?
                        >[/color]

                        It's quite easy for me to save the page to my disk, edit it to change the data,
                        and resubmit it to your site.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        Working...