PHP code to get selected text of a combo box

前端 未结 7 936
死守一世寂寞
死守一世寂寞 2020-12-10 08:42

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

相关标签:
7条回答
  • 2020-12-10 09:31

    Put whatever 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. It defaults to using the text.

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

    $makes = array(2 => 'Toyota',
                   3 => 'Nissan');
    
    $maker = $makes[$_POST['Make']];
    
    0 讨论(0)
  • 2020-12-10 09:32

    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];
    }
    ?>
    
    0 讨论(0)
  • 2020-12-10 09:32

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

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

    or

    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.

    0 讨论(0)
  • 2020-12-10 09:34

    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;
    }
     ?>
    
    0 讨论(0)
  • 2020-12-10 09:35

    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.

    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:

    $maker = mysql_real_escape_string($_POST['Make']);
    $maker = explode("_",$maker);
    echo $maker[1]; //give the Toyota
    echo $maker[0]; //give the key 2
    
    0 讨论(0)
  • 2020-12-10 09:36

    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>
    
    0 讨论(0)
提交回复
热议问题