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?
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 }
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".
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.