简体   繁体   中英

How can I pass an HTML value to a PHP variable

I have a simple web app made with CodeIgniter 3 and I have created a simple dropdown menu where the user can select from a variety of options from the database. Here is the code:

<?php   
        $sql4 = "SELECT DISTINCT (color), id FROM porta_color ORDER BY color ASC "; 
                 $result4 = $conn->query($sql4);
?>

<select id="color" class="form-control selectpicker" data-size="10" data-live-search="true" data-style="btn-white" name="k_color" required  >

<?php 
     if ($result4->num_rows > 0) 
     {
       while($row4 = $result4->fetch_assoc()) 
       {
         if($row4['id']>0)
         {?>
         <option value="<?=$row4['id'];?>"><?=$row4['color'];?></option>
         <?php }}}?>
 </select> 

Now what I want to do is based on the selection from the user to pass the value to a PHP variable.

Any idea how I might do that?

Wrap your select into form tags and add submit or button element

<form>
  <select id="color" class="form-control selectpicker" data-size="10" data-live-search="true" data-style="btn-white" name="k_color" required  >
    <option value="color1">Color 1</option>
    <option value="color2">Color 2</option>
    <option value="color3">Color 3</option>
  </select>
  <input type="submit" values="Choose color">
  <button>Another way for submit form</button>
</form>

But true way is using framework abilities

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM