PHP / Form get value and selection from Select Box.

I have an HTML form with a select box.  I cant figure out how to pass along the option value (index value) and also the selected name of a <SELECT> box.  I need to be able to access both via the $_POST[].  I can get the option vale but not the text of the selected option.
keith1001Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
That's the way it works.  If you want the text, you have to include it in the value.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
So you gonna to have a hidden field for example, and use javascript to set its value with the option's selected Text value.  By saving value into a hidden field, you should able to retrieve it after your form was posted.
RobOwner (Aidellio)Commented:
This is by design because your PHP should be able to work out the text from the value of the option.  Given how easy it is to spoof data, you should do some server side validation regardless.
Ray PaseurCommented:
PHP is a programming language that prepares HTML documents.  For this reason, PHP is aware of the content of the HTML documents before they are sent to the client.  Usually a SELECT control would be prepared from data that was gotten from a data base.  When the form is submitted your application will compare the inputs to the original data in the data base.  This is by design to be sure that your form has not become an attack vector.

If you're new to PHP and want some good learning resources, this article can lead you in the right direction.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

This code snippet shows the general design we use for something like this.  Note that it takes no action if the $_POST request does not contain a valid color number.  This is a sensible design - we accept only known good values.  You can copy this script and install it on your server to see it in action, or test it on my server here:
http://iconoun.com/demo/temp_keith1001.php

<?php // demo/temp_keith1001.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28681061.html
 */
error_reporting(E_ALL);


// SIMULATE A DATABASE TABLE WITH ID KEYS AND COLOR NAMES
$data = array
( 23 => 'red'
, 47 => 'green'
, 19 => 'blue'
)
;

// HAS THE FORM BEEN POSTED?
if (!empty($_POST))
{
    // WAS A COLOR SELECTION INCLUDED IN THE REQUEST?
    $color = !empty($_POST['color']) ? $_POST['color'] : NULL;

    // WAS A VALID COLOR SELECTED
    if (!empty($data[$color]))
    {
        echo PHP_EOL . "A VALID COLOR WAS SELECTED: " . $data[$color];

        // REST OF POST-REQUEST PROCESSING HERE
        die("<br>Success using color number: $color");
    }

}

// CREATE THE HTML FORM
$form = <<<EOD
<form method="post">
Choose a color:
<select name="color">
<option value="0">Please choose:</option>
<option value="23">Red</option>
<option value="47">Green</option>
<option value="19">Blue</option>
</select>
<br>
<input type="submit" name=""submit" value="Post my request for the chosen color" />
</form>
EOD;

echo $form;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
keith1001Author Commented:
This what I was looking for, excellent.  Thanks for a nice clean example and other useful links!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.