<option>选择内部文本而不是值

时间:2017-11-10 23:11:38

标签: php

我有一个<select>表格,我想选择所选的文字。

 <select multiple name="detaliicomanda[]"  class='main'  >
 <option value=' '> -- </option>
 <option value='canapea'> Canapea  </option>
 <option value='fotoliu'> Fotoliu </option>
 <option value='seturi'> Seturi </option>
 <option value='coltar'> Coltar  </option>
 <option value='mobila'> Mobila  </option>
 <option value='corpurimobila'> Corpuri Mobila  </option>
</select>

我知道如果我使用inside选项文本更改值,php将选择文本。

我需要选项中的值为“X”并从所选选项中选择数据

希望你们明白我的意思:-S

2 个答案:

答案 0 :(得分:1)

示例1:要从select标记中获取所选选项的值:

<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];  // Storing Selected Value In Variable
echo "You have selected :" .$selected_val;  // Displaying Selected Value
}
?>

示例2:要从select标记获取多个select选项的值,HTML标记中的name属性应使用数组[]进行初始化:

<form action="#" method="post">
<select name="Color[]" multiple> // Initializing Name With An Array
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
// As output of $_POST['Color'] is an array we have to use foreach Loop to display individual value
foreach ($_POST['Color'] as $select)
{
echo "You have selected :" .$select; // Displaying Selected Value
}
?>

在您的代码中,您可以尝试:

    <?php 
         foreach($_POST['detaliicomanda'] as $select){
         echo "You have selected: ".$select;//Displaying selected value
         }
     ?>

答案 1 :(得分:1)

仅将值发送到服务器。您可以使用服务器上的关联数组将值从值转换为文本。

$detaliicomanda = array(
    'canapea' => 'Canapea',
    'fotoliu' => 'Fotoliu',
    ...
);

然后你可以这样做:

$comanda = array_map(function($x) use($detaliicomanda) {
    return $detaliicomanda[$x];
}, $_POST['detaliicomanda']);