Jump to content

get 'Text' Value from drop down list!!!


juggysingh

Recommended Posts

Hi,  :(

 

Is there anyway to retrieve the selected text value aswell as the value from a drop down list?

 

<select name="dropdwn1" id="dropdwn1" size="1">
  <option value="0" selected="selected">Choose</option>
  <option value="value1">text1</option>
  <option value="value2">text2</option>
</select>

Link to comment
Share on other sites

I believe that the only way that can be done is if your drop down was populated by a database or an array, or if your value is the same as your text. If you just typed the values and options in there yourself, then it will only read the value portion of the drop down box and you lose the other information.

 

<select id="dropdwn1" name="dropdwn1" size ="1">
  <option value="0" selected="selected">Choose</option>
  <option value="value1">value1</option>
  <option value="value2">value2</option>
</select>

or

 

$states = array(
  "AL"=>"Alabama",
  "AK"=>"Alaska",
  "AZ"=>"Arizona"
)

<select name="state" id="state">
  <option value="" selected="selected">--- Select State ---</option>
  <?
    foreach($states as $key => $value){
       echo '<option value="'.$key.'">'.$value.'</option>';
   }
  ?>
</select>

 

then you can get the information you wanted.

Link to comment
Share on other sites

hi hcdarkmage,

 

Thanks for your code, i have tested it using a form as an example, but the value i get once posted is still not the name but the value?? Please see my code below!

 

Thanks

 

<?php 
if (!isset($_POST['submit'])) {
?>

<?php
$states = array(
  "AL"=>"Alabama",
  "AK"=>"Alaska",
  "AZ"=>"Arizona"
)

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="state" id="state">
  <option value="" selected="selected">--- Select State ---</option>
  <?php
    foreach($states as $key => $value){
       echo '<option value="'.$key.'">'.$value.'</option>';
   }
  ?>
</select>
<input type="submit" name="submit">
</form>


<?php
} else {

$state = $_POST["state"];
echo $_POST['state']; 
}
?> 

Link to comment
Share on other sites

<?php 
if (!isset($_POST['submit'])) {
?>

<?php
$states = array(
  "AL"=>"Alabama",
  "AK"=>"Alaska",
  "AZ"=>"Arizona"
)

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="state" id="state">
  <option value="" selected="selected">--- Select State ---</option>
  <?php
    foreach($states as $key => $value){
       echo '<option value="'.$value.'">'.$value.'</option>';
   }
  ?>
</select>
<input type="submit" name="submit">
</form>


<?php
} else {

$state = $_POST["state"];
echo $_POST['state']; 
}
?> 

 

Since you just want the value and the text to be the same, just echo the value ($value) in both places.

Link to comment
Share on other sites

Hi MatthewJ,

 

I cant use the option of having the text and value the same as each other as this form is being developed as an order form.

 

Thus the value will need to be a number and the text the name associated with that value.

 

Example;

 

<select name="shirt_price" size="3">
<option value="15">long_sleeve</option>
<option value="20">short_sleeve</option>
</select>

Link to comment
Share on other sites

Select menus work by passing the value... aside from having them match, I think you're going to be stuck with one or the other.

 

You could have the values match with the shirts too though.. then just have some code on the handling page that converts the selected item to its value like

<select name="shirt_price" size="3">
<option value="long_sleeve">long_sleeve</option>
<option value="short_sleeve">short_sleeve</option>
</select>

 

Then in the handler

 

if($_POST['shirt_price'] == "long_sleeve") {
$_POST['shirt_price'] == 15;
} else {
$_POST['shirt_price'] == 20;
}

 

etc.

Link to comment
Share on other sites

Or you can do it this way:

 

<?php

$sleeves = array(
"15"=>"Long Sleeve",
"20"=>"Short Sleeve",
);

if (!isset($_POST['submit'])) {

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="shirt" id="shirt">
<option value="" selected="selected">--- Select Sleeve Type ---</option>
<?php
   foreach($sleeves as $key => $value){
      echo '<option value="'.$key.'">'.$value.'</option>';
  }
?>
</select>
<input type="submit" name="submit">
</form>


<?php
} else {

$shType = $_POST["shirt"];
echo "This should work ".$sleeves[$shType];

}
?>

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.