0
votes

I am attempting to echo the name of a selected dropdown rather than its value. I understand that echoing a dropdown's value can be achieved by implementing something such as:

$no2 = $_POST['vehicleStyle'];
echo $no2;

where vehicleStyle is the name.

this is my code

<select name="vehicleStyle">
<option value="2">Volvo</option>
<option value="3">Saab</option>
<option value="4">Fiat</option>
<option value="5">Audi</option>
</select>

If I add

    $no2 = $_POST['vehicleStyle'];
    echo $no2;

to my code, I get either 2,3,4 or 5 (whichever is selected). How can I echo the dropdown name, either volvo, saab , fiat or audi?

NOTE: i use this code

    $no2 = $_POST['vehicleStyle'];
    echo $no2;

to show the price of the car which i set in value, but also i need to echo the text to show the name of the car, how i can do that?

4
you have some confusion... what is sent to the server is the VALUE of an input, not "the text inside it". - Alberto Sinigaglia
The option name is only for presentation in the HTML. It isn't sent with the form. If you need the name, then put that as the value instead. If you need both, you need to add both as the value (either separate it somehow: name;id and explode it on the back end, or pass the value as a json object. value='{"name": "Volvo","id":2}'), - M. Eriksson
if you need both, you have to manage it in other ways - Alberto Sinigaglia
Change the value of the option to the text, unless you need the numeric ID - Martin

4 Answers

2
votes

if you don't need the values (2, 3, 4, 5), you can just use:

<select name="vehicleStyle">
<option>Volvo</option>
<option>Saab</option>
<option>Fiat</option>
<option>Audi</option>
</select>

but if you need both, you should do something like this:

$no2 = [
  "2" => "Volvo",
  "3" => "Saab",
  "4" => "Fiat",
  "5" => "Audi"
]$_POST['vehicleStyle'];
echo $no2;

be aware that if you change the dropdown in the future, you have to come back here and change also this array

1
votes

Simple approach is give the value the same as the text in it!

<form method="post">
    <select name="vehicleStyle">
        <option value="Volvo">Volvo</option>
        <option value="Saab">Saab</option>
        <option value="Fiat">Fiat</option>
        <option value="Audi">Audi</option>
    </select>
    <input type="submit" name="vehicleName">
</form>

if(isset($_POST['vehicleName'])){
    echo $_POST['vehicleStyle'];
}
0
votes

If you are also need both text(name) and value(id) as magnus eriksson Suggest you need to separate them like: id;name and explode it on the back end to get id as well as name.

<form method="post">
    <select name="vehicleStyle">
        <option value="2;Volvo">Volvo</option>
        <option value="3;Saab">Saab</option>
        <option value="4;Fiat">Fiat</option>
        <option value="5;Audi">Audi</option>
    </select>
    <input type="submit" name="vehicleName">
</form>
if(isset($_POST['vehicleName'])){
    list($id, $name) = explode(';', $_POST['vehicleStyle']);
    echo $id.' => '.$name;
}
0
votes

Thank you all,

i solved this by editing the code to:

<form method="post">
<select name="vehicleName">
<option value="2|volvo">Volvo</option>
<option value="3|saab">Saab</option>
<option value="4|fiat">Fiat</option>
<option value="5|audi">Audi</option>
</select>
<input type="submit" name="submit">
</form>

and

if(isset($_POST['submit']))
{
$no2=$_POST['vehicleName'];
$no2_explode=explode('|', $no2);

for text

<?php echo $no2_explode[1]; ?>

and for the price number

<?php echo $no2_explode[0]; ?>