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

2022-01-02 00:00:00 php html combobox selectedtext

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

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.

<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"/>

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

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

 <?php

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

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

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 ....

推荐答案

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

Try with this. You will get the select box value in $_POST['Make'] and name will get in $_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;
}
 ?>

相关文章