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.

4
  • You want to send which variable sourcestationid or destinationstationid ? Commented May 7, 2018 at 10:37
  • I need to send both station id. @BitsPlease Commented May 7, 2018 at 10:41
  • Store your source and destination id in data attribute of option as <option data-sourceid="yoursource id" data-destinationeid="yourdestination id">one</option> tha on change of your select pass both data-sourceid and data-destinationeid to your controller. Commented May 7, 2018 at 10:43
  • stackoverflow.com/questions/3245967/… Commented May 7, 2018 at 10:49

2 Answers 2

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
?>
Sign up to request clarification or add additional context in comments.

2 Comments

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
In my controller, this is giving this error. Undefined variable: select @Bits Please
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.