用于获取组合框选定文本的 PHP 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18268068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:15:21  来源:igfitidea点击:

PHP code to get selected text of a combo box

phphtmlcomboboxselectedtext

提问by Emmanuel Yasith

I have a combo box named "Make". In that combo box I'm loading vehicle manufacturer names. When I click SEARCH button I want to display the selected manufacturer name. Below is part of my HTML code.

我有一个名为“Make”的组合框。在那个组合框中,我正在加载车辆制造商名称。当我单击“搜索”按钮时,我想显示选定的制造商名称。下面是我的 HTML 代码的一部分。

<label for="Manufacturer"> Manufacturer : </label>
<select id="cmbMake" name="Make" >
   <option value="0">Select Manufacturer</option>
   <option value="1">--Any--</option>
   <option value="2">Toyota</option>
   <option value="3">Nissan</option>
</select>

<input type="submit" name="search" value="Search"/>

Below is my PHP code so far I've done.

以下是到目前为止我已经完成的 PHP 代码。

 <?php

if(isset($_POST['search']))
{
    $maker = mysql_real_escape_string($_POST['Make']);
    echo $maker;
    }
 ?>

If I select Toyota from the combo box and press SEARCH button, I'm getting the answer as '2' . It means it gives me the value of the 'Toyota'. But I want to display the name 'Toyota'. How can I do that? Please help me ....

如果我从组合框中选择 Toyota 并按 SEARCH 按钮,我得到的答案为 '2' 。这意味着它给了我“丰田”的价值。但我想显示“丰田”这个名字。我怎样才能做到这一点?请帮我 ....

回答by Ajeesh

Try with this. You will get the select box value in $_POST['Make'] and name will get in $_POST['selected_text']

试试这个。您将在 $_POST['Make'] 中获得选择框值,在 $_POST['selected_text'] 中获得名称

<form method="POST" >
<label for="Manufacturer"> Manufacturer : </label>
  <select id="cmbMake" name="Make"     onchange="document.getElementById('selected_text').value=this.options[this.selectedIndex].text">
     <option value="0">Select Manufacturer</option>
     <option value="1">--Any--</option>
     <option value="2">Toyota</option>
     <option value="3">Nissan</option>
</select>
<input type="hidden" name="selected_text" id="selected_text" value="" />
<input type="submit" name="search" value="Search"/>
</form>


 <?php

if(isset($_POST['search']))
{

    $makerValue = $_POST['Make']; // make value

    $maker = mysql_real_escape_string($_POST['selected_text']); // get the selected text
    echo $maker;
}
 ?>

回答by Barmar

Put whatever you want to send to PHP in the valueattribute.

将您想发送给 PHP 的任何内容放在value属性中。

  <select id="cmbMake" name="Make" >
     <option value="">Select Manufacturer</option>
     <option value="--Any--">--Any--</option>
     <option value="Toyota">Toyota</option>
     <option value="Nissan">Nissan</option>
  </select>

You can also omit the valueattribute. It defaults to using the text.

您也可以省略该value属性。它默认使用文本。

If you don't want to change the HTML, you can put an array in your PHP to translate the values:

如果您不想更改 HTML,可以在 PHP 中放置一个数组来转换值:

$makes = array(2 => 'Toyota',
               3 => 'Nissan');

$maker = $makes[$_POST['Make']];

回答by Bora

You can achive this with creating new array:

您可以通过创建新数组来实现此目的:

<?php
$array = array(1 => "Toyota", 2 => "Nissan", 3 => "BMW");

if (isset ($_POST['search'])) {
  $maker = mysql_real_escape_string($_POST['Make']);
  echo $array[$maker];
}
?>

回答by chirag ode

if you fetching it from database then

如果你从数据库中获取它然后

<select id="cmbMake" name="Make" >
<option value="">Select Manufacturer</option>
<?php $s2="select * from <tablename>"; 
$q2=mysql_query($s2); 
while($rw2=mysql_fetch_array($q2)) { 
?>
<option value="<?php echo $rw2['id']; ?>"><?php echo $rw2['carname']; ?></option><?php } ?>
</select>

回答by Code L?ver

Change your select box options value:

更改您的选择框选项值:

<select id="cmbMake" name="Make" >
     <option value="">Select Manufacturer</option>
     <option value="Any">--Any--</option>
     <option value="Toyota">Toyota</option>
     <option value="Nissan">Nissan</option>
  </select>

You cann't get the text of selected option in php. it will give only the value of selected option.

您无法在 php 中获取所选选项的文本。它只会给出所选选项的值。

EDITED:

编辑:

<select id="cmbMake" name="Make" >
     <option value="0">Select Manufacturer</option>
     <option value="1_Any">--Any--</option>
     <option value="2_Toyota">Toyota</option>
     <option value="3_Nissan">Nissan</option>
  </select>

ON php file:

在 php 文件中:

$maker = mysql_real_escape_string($_POST['Make']);
$maker = explode("_",$maker);
echo $maker[1]; //give the Toyota
echo $maker[0]; //give the key 2

回答by David Ortega

you can make a jQuery onChange event to get the text from the combobox when the user select one of them:

当用户选择其中一个时,您可以创建一个 jQuery onChange 事件以从组合框中获取文本:

<script>    
     $( "select" )
     .change(function () {
     var str = "";
     $( "select option:selected" ).each(function() {
     str += $( this ).text() + " ";
     });
     $('#EvaluationName').val(str);
     })
     .change();
 </script>

When you select an option, it will save the text in an Input hidde

当您选择一个选项时,它会将文本保存在输入隐藏中

  <input type="hidden" id="EvaluationName" name="EvaluationName" value="<?= $Evaluation ?>" />

After that, when you submit the form, just catch up the value of the input

之后,当你提交表单时,只需赶上输入的值

$Evaluation = $_REQUEST['EvaluationName'];

Then you can do wathever you want with the text, for instance save it in a session variable and send it to other page. etc.

然后您可以对文本进行任何您想要的操作,例如将其保存在会话变量中并将其发送到其他页面。等等。

回答by Momoro

I agree with Ajeesh, but there are simpler ways to do this...

我同意 Ajeesh,但有更简单的方法可以做到这一点......

if ($maker == "2") { }

if ($maker == "2") { }

or

或者

if ($maker == 2) { }

if ($maker == 2) { }

  • Why am I not returning a "Toyota" value? Because the "Toyota" choice in the Selection Box would have already returned "2", which, would indicate that the selected Manufacturer in the Selection Box would be Toyota.

  • How would the user know if the value is equal to the Toyota selection in the Selection Box? In between my example code's brackets, you would put $maker = "Toyota"then echo $maker, or create a new string, like so: $maketwo = "Toyota"then you can echo $makertwo(I much prefer creating a new string, rather than overwriting $maker's original value.)

  • If the user selects "Nissan", will the example code take care of that as well..? Yes, and no. While "Toyota" would return value "2", "Nissan" would instead return value "3". The current set value that the example code is looking for is "2", which means that if the user selects "Nissan", which represents value "3", then presses "Search", the example code would not be executed. You can easily change the code to check for value "3", or value "1", which represents "--Any--".

  • What if the user clicks "Search" while the Selection Box is set to "Select Manufacturer"? How can I prevent them from doing so? To prevent them from proceeding any further, change the set value of the example code to "0", and in between the brackets, you may place your code, then after that, add return;, which terminates all execution of any further code within the function / statement.

  • 为什么我不返回“Toyota”值?因为选择框中的“Toyota”选项已经返回“2”,这表示选择框中所选的制造商将是丰田。

  • 用户如何知道该值是否等于选择框中的 Toyota 选择?在我的示例代码的括号之间,您可以放置$maker = "Toyota"thenecho $maker或创建一个新字符串,如下所示:$maketwo = "Toyota"then 您可以echo $makertwo(我更喜欢创建一个新字符串,而不是覆盖$maker的原始值。)

  • 如果用户选择“Nissan”,示例代码是否也会处理这个问题......?是的,也不是。“Toyota”将返回值“2”,而“Nissan”将返回值“3”。示例代码当前查找的设定值为“2”,表示如果用户选择“Nissan”,表示值为“3”,然后按“搜索”,示例代码将不会被执行。您可以轻松更改代码以检查值“3”或值“1”,代表“--Any--”。

  • 如果用户在选择框设置为“选择制造商”时单击“搜索”会怎样?我怎样才能阻止他们这样做?为了防止它们继续进行,将示例代码的设置值更改为“0”,在括号之间,您可以放置​​您的代码,然后添加return;,这将终止函数中任何进一步代码的所有执行/ 陈述。