2

I have a dropdown box where the data is coming from my database. I want to send the selected drop-down item to the controller action method. And then I will send these values to the model to do the further works. Here, the problem is I have two database values in a single item of drop-down box. And, I am not figuring out how to send those two the method. Here is my code given below,

<select name='select'>
        <option selected disabled>Choose Stations</option>
        <?php foreach ($get_stations as $get_stations_item): ?>
            <option>Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option> 
        <?php endforeach; ?>
    </select>

From this dropdown items, I want to send the sourcestationid and destinationstationid separately as 2 parameters to my controller action method. Here is my controller code though this is not correct way I think,

function getdata(){
    $iotdata['test'] = $this->input->post('select2');
    //rest of the code according to the source and destination id item
}

Thanks in advance.

Mahbub
  • 317
  • 2
  • 9
  • 27
  • You want to send which variable sourcestationid or destinationstationid ? – Bits Please May 07 '18 at 10:37
  • I need to send both station id. @BitsPlease – Mahbub May 07 '18 at 10:41
  • Store your source and destination id in data attribute of option as tha on change of your select pass both data-sourceid and data-destinationeid to your controller. – Devsi Odedra May 07 '18 at 10:43
  • https://stackoverflow.com/questions/3245967/can-an-option-in-a-select-tag-carry-multiple-values?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Devsi Odedra May 07 '18 at 10:49

2 Answers2

4

You haven't kept the variable inside option's value tag.

     <select name='select'>
    <option selected disabled>Choose Stations</option>
    <?php foreach ($get_stations as $get_stations_item): ?>
        <option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationid']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option> 
    <?php endforeach; ?>
</select>

Also here at PHP end, you can explode the string into an array and store it further into db.

<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>
Bits Please
  • 877
  • 6
  • 23
  • I need to send both station id to the controller action. Because for the same source station, I have different destination stations and vice-versa. @Bits Please – Mahbub May 07 '18 at 10:44
  • In my controller, this is giving this error. Undefined variable: select @Bits Please – Mahbub May 07 '18 at 12:25
2

Thanks @BitsPlease for our suggestion. Your idea is working. But I need to do a small change to get it instantly. My select tag needs to be under form tag to get them without reloading the page again. Here is my view,

<form method="post" accept-charset="utf-8" action="<?php echo site_url("controller/action"); ?>">
        <select name='select' onchange="this.form.submit()">
            <option selected disabled>Choose Stations</option>
            <?php foreach ($get_stations as $get_stations_item): ?>
                <option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationidr']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationidr']; ?></option> 
            <?php endforeach; ?>
        </select>
    </form>

Here is the php code,

$select = explode(',', $this->input->post('select'));
print_r($select)
Mahbub
  • 317
  • 2
  • 9
  • 27