PHP code to get selected text of a combo box

I have a "Make" combo box. In this combo box, I upload the names of the car manufacturers. When I press the SEARCH button, I want to display the selected manufacturer name. The following 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"/> 

Below is my PHP code so far I have done.

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

If I select Toyota from the combo box and press the SEARCH button, I get the answer as "2". This means that it gives me the value of Toyota. But I want to show the name Toyota. How can i do this? Please help me....

+4
source share
5 answers

Try it out. You will get the value of the select box in $ _POST ['Make'], and the name will get $ _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; } ?> 
+5
source

Put everything you want to send to PHP in the value attribute.

  <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 value attribute. The default is text.

If you do not want to modify the HTML, you can put an array in your PHP to convert the values:

 $makes = array(2 => 'Toyota', 3 => 'Nissan'); $maker = $makes[$_POST['Make']]; 
+4
source

You can achieve this by creating a 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]; } ?> 
+1
source

if you retrieve it from the 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> 
+1
source

Change the value of the block selection option:

 <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 cannot get the text of the selected option in php. it will only give the value of the selected parameter.

Edition:

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

In php file:

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

Source: https://habr.com/ru/post/1497378/


All Articles