简体   繁体   中英

how to return string with select box with select option selected as per a given variable

I using Datatable library to show a table in wordpress custom plugin where I need to return selectbox input. The select box option should display the option as it is saved in database but it is showing the first option always.

How to code the same thing in js when the select input is returned for a column in datatable. I have tried the following way:

columns: [{
       
{
            'data': null,
            'render': function (data, type, row, meta) {
                 
                
            return '<select class="selectpicker" name="pm" id="pm-' + row.mls + '"><option value="Tafolla">Tafolla</option><option value="Lucy">Lucy</option></select>';

            }
         },
}]

Well first get the JS value that is selected then based off return call the php value in a separate call not inlineHTML . Try not overcomplicate cross language calls ...

 document.getElementById('my-select').addEventListener('change', function() { console.log('You selected: ', this.value); //Call PHP values based on selection eg if(this.value == 2) { var text = " <?php echo ($row['status'] == 'AR')?'selected='selected':''; ?>"; } });
 <select id="my-select"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select>

The other way of doing it is in plain php with HTML select

<select name="formGender">
   <option value="">Select...</option>
   <option value="M">Male</option>
   <option value="F">Female</option>
 </select>

//PHP
<?php

 if(isset($_POST['formSubmit']) )
  {
   $varGender = $_POST['formGender'];         
  }

?>

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