我有一个下拉框,数据来自我的数据库。我想将选定的下拉项发送到控制器操作方法。然后我将这些值发送给模型以进行进一步的工作。这里的问题是我在下拉框的单个项目中有两个数据库值。而且,我不知道如何发送这两个方法。以下是我的代码,
<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>
从这个下拉列表项中,我想将sourcestationid和destinationstationid分别作为2个参数发送到我的控制器操作方法。这是我的控制器代码,虽然这不是我认为的正确方法,
function getdata(){
$iotdata['test'] = $this->input->post('select2');
//rest of the code according to the source and destination id item
}
提前致谢。
答案 0 :(得分:3)
您还没有将变量保留在选项的值标记内。
<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>
此外,在PHP结束时,您可以将字符串分解为数组并将其进一步存储到db。
<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>
答案 1 :(得分:2)
感谢@BitsPlease提出我们的建议。你的想法正在发挥作用但我需要做一个小改动才能立即得到它。我的select标签需要在form标签下才能获得它们而无需再次重新加载页面。这是我的观点,
<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>
这是php代码,
$select = explode(',', $this->input->post('select'));
print_r($select)