tags:

views:

31

answers:

3

Let's say I have a HTML form: USA CDN And I select option 1 (USA) Then a php page Ok, duh, works fine and echo's "1" How can I display "USA" as well? So in essence, I want to pass along the option's TEXT also. How could I do this?

A: 

I suggest having a copy of the same data structure you used to print the form.

For example, if you were to do it all in one PHP page...

<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
  <select name='country'><?php
    foreach($countries as $key=>$country){?>
      <option value='<?php echo $key;?>'><?php echo $country;?></option>
    <?php
    } ?>
  </select>

  <input type='submit'>
</form>

<?php
if(isset($_POST['country'])){
  $chosen=(int)$_POST['country'];

  if(!isset($countries[$chosen])){?>Unknown country selection, wtf<?php exit; }?>

  <div style='margin-top:20px;'>You selected <?php echo $countries[$chosen];?></div>

<?php }

Another option is to ditch the numeric value altogether, and just use the country name as the value of the option. However, for verification, you'll still want the list of values. Check the differences in this example...

<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
  <select name='country'><?php
    foreach($countries as $country){?>
      <option value='<?php echo $country;?>'><?php echo $country;?></option>
    <?php
    } ?>
  </select>

<input type='submit'>
</form>

<?php
if(isset($_POST['country'])){
  $chosen=preg_replace('/[^\w]/','',$_POST['country']);//this isn't strictly necessary, since the next line checks if the value is in your original array - but filtering input is a good habit!

  if(!in_array($_POST['country'],$countries)){?>Unknown country selection, wtf<?php exit; }?>

<div style='margin-top:20px;'>You selected <?php echo $chosen;?></div>

<?php }
Alex JL
@Alex - hit that baby with a little more formatting
bemace
@bemace Alas, formatting doesn't appear to be my strong point!
Alex JL
My select dropdowns are dynamically filled from a database that's already pre-populated with thousands of rows of data, all with numeric IDs. The form has 5 select elements that change related to the user input, so select 1 gets a new option and select 2 re-populates with an ajax call. So these 5 selects are all based on the numeric ID in the database. I just need a way to display the option text in a echo statement after the form has been posted so that the user can see what they chose and I can't go reformatting everything at this point.
Rob Bennet
@Rob Bennet The best option is to retrieve the text from the database after the form has been posted. That should be pretty simple, and is the most secure option (setting a hidden form field with JavaScript when they make the selection is another option, but verification would be difficult).
Alex JL
A: 

The client's browser will not post back anything beyond the form element's name and value - you would have to incorporate additional form fields and Javascript (or change the element's value) to post "USA".

danlefree
Can I get some comments to go with that downvote?
danlefree
I've received two downvotes on my post without explanation, too, so we're in this together, I guess.
Alex JL
+2  A: 

You would either need to change the option value on the HTML page to be the text you want displayed (poses a XSS security hazard) or on the page that's displaying the text, you'd need an array where all the values match with the forum input.

Ex: $names[1] = "USA"; $names[2] = "CDN"; Then when you want to display it, you'd call $names[$selection] to output the text.

Another option you can consider is using javascript to update a hidden HTML element on the submitting page when the user makes their selection (using onUpdate). This information would be passed along with the submitted form data. Not the most secure or reliable of options, but an option nonetheless.

GigaWatt