Jump to content

Getting value & text from a dropdown


Ravendark

Recommended Posts

Hello,

 

I am searching to find an answer to my question but without success.

I have a HTML dropdown list like this:

 

<select name="ports">
<option value="10">1/1/1/0/shdsl</option>
<option value="12">1/1/2/0/shdsl</option>
<option value="14">1/1/3/0/shdsl</option>
<option value="16">1/1/4/0/shdsl</option>
<option value="18">1/1/5/0/shdsl</option>
<option value="20">1/1/6/0/shdsl</option>
<option value="22">1/1/7/0/shdsl</option>
<option value="24">1/1/8/0/shdsl</option>
<option value="26">1/1/9/0/shdsl</option>
<option value="28">1/1/10/0/shdsl</option>
<option value="30">1/1/11/0/shdsl</option>
<option value="32">1/1/12/0/shdsl</option>
<option value="34">1/1/13/0/shdsl</option>
<option value="36">1/1/14/0/shdsl</option>
<option value="38">1/1/15/0/shdsl</option>
<option value="40">1/1/16/0/shdsl</option>
<option value="42">1/1/17/0/shdsl</option>
<option value="44">1/1/18/0/shdsl</option>
<option value="46">1/1/19/0/shdsl</option>
<option value="48">1/1/20/0/shdsl</option>
<option value="50">1/1/21/0/shdsl</option>
<option value="52">1/1/22/0/shdsl</option>
<option value="54">1/1/23/0/shdsl</option>
<option value="56">1/1/24/0/shdsl</option>
</select>

 

All I want to do is to get the value & the text (e.g 56 & "1/1/24/0/shdsl").

I get the value (say 56) with:

$_POST['ports']

 

Could you please help me get the text?

Link to comment
Share on other sites

You need to put the text in the value too:

 

<option value="10 1/1/1/0/shdsl">1/1/1/0/shdsl</option>

<option value="12 1/1/2/0/shdsl">1/1/2/0/shdsl</option>

etc

etc

 

Or you could create a lookup table that references the value. That way you can produce two variables, one with the value eg. 10 and one with the text item eg: 1/1/1/0/shdsl

Link to comment
Share on other sites

Just to expand on what New Coder said....

 

The value attribute is what your after and what you use in the scripts... the display text is solely for the user to read. You may be able to use Javascript to get that value (not sure on this), but the php $_POST / $_GET array is going to contain the value attribute of the item selected. This is just the way PHP handles HTML form submissions.

 

If the numbers in the attribute field correspond with an array or a database ID, then you can take that value and grab the data that goes with it. But if your list is hard coded then you need to change the code to reflect the actual value in the value attribute field.

 

Hope this makes sense and does not confuse.

 

Nate

 

 

Link to comment
Share on other sites

Nah, won't help, cause I need the 2digit value as it is.

I thought of a different way, I use javascript to place the text to a hidden text field, then pass it to php...

 

Thanks though

 

It may work, but seems to be a longer way around.. and if a user has Javascript turned off, then your form does not work right.

 

It is best to work within the scope of the languages your using. The value attribute is there as the value, the label is the label. Your trying to make the label be the value. If the 2 digit value HAS to remain, then what is that being used for that it cannot retrieve the label that it represents.

 

Just my 2 cents.

 

Nate

Link to comment
Share on other sites

Thanks for replying.

The 2digit value is a port ifindex which is used to for shut-no shut.

The rest is just the interface/port name which is also necessary for informing the user of which port is manipulating.

I will just have to add a javacript test (if disabled alert the user) and I guess it will be fine.

 

Thanks for your answers!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.