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

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

P: n/a

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
Mar 25 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a

Zorro wrote:
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


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

Mar 25 '06 #2

P: n/a
Message-ID: <11*********************@i40g2000cwc.googlegroups. com> from
rlee0001 contained the following:
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.


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/
Mar 25 '06 #3

P: n/a
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" <ro*********@hotmail.com> a écrit dans le message de news:
11*********************@i40g2000cwc.googlegroups.c om...

Zorro wrote:
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


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

Mar 25 '06 #4

P: n/a
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

Mar 25 '06 #5

P: n/a
True. That would have been most elegant. :o)

-Robert

Mar 25 '06 #6

P: n/a
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" <ro*********@hotmail.com> a écrit dans le message de news:
11**********************@g10g2000cwb.googlegroups. com...
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

Mar 26 '06 #7

P: n/a
Zorro wrote:
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" <ro*********@hotmail.com> a écrit dans le message de news:
11**********************@g10g2000cwb.googlegroups. com...
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



<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-
Mar 26 '06 #8

P: n/a
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.value =
getElementById('myselect').getAttribute('longvalue ') or something like
that (untested).

-Robert

Mar 26 '06 #9

P: n/a
Yes, this is a very good solution.

thank you guys for your help;
Denis

"rlee0001" <ro*********@hotmail.com> a écrit dans le message de news:
11**********************@i40g2000cwc.googlegroups. com...
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.value =
getElementById('myselect').getAttribute('longvalue ') or something like
that (untested).

-Robert

Mar 27 '06 #10

P: n/a
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?

Mar 28 '06 #11

P: n/a
Duderino82 wrote:
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?


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.
js*******@attglobal.net
==================
Mar 28 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.