CI:将隐藏字段中的值保存到另一个表中

时间:2015-07-08 10:06:10

标签: php mysql codeigniter

我在CI框架中创建了一个包含60个字段的表单,数据库正常工作。现在我想分开一些字段并将其发送到另一个表,该表链接到主表(当然还有关系)。 所以我隐藏了一些字段,并在单选按钮被选中时显示为"是" :

<!-- other fields -->
<div><label class="control-label" for="imp_status">3rd Party</label>
 <div class="controls">
  <table>
   <tr>
    <td><input type="radio" name="imp_status" id="imp_status_yes" value="1" onclick="javascript:importirCheck()"> Yes</td>
    <td><input type="radio" name="imp_status" id="imp_status_no" value="0" onclick="javascript:importirCheck()"> No</td>
   </tr>
  </table>
 </div>
</div>
<div style="display:none" id="info_imp">
 <fieldset> somefields </fieldset>
</div>
<!-- other fields -->

脚本:

function importirCheck() {
    if (document.getElementById('imp_status_no').checked) {
        document.getElementById('info_imp').style.display = 'none';
        $("#imp_status_no").val("");
    }
    else {
        document.getElementById('info_imp').style.display = 'block';
        $("#imp_status_yes").val("");
        $("#imp_comp_name").val("");
        ...
    }
}

视图正在运行,但现在我很困惑我应该如何为我的数据库获取值(特别是来自imp_status因为有2个id)。表单末尾有保存按钮,并链接到另一个脚本。我从该脚本发送了其他值。 现在我有table_primary(..,imp_status,id_imp,..)和table_imp(id_imp,...)。如果没有第三方,我希望imp_status列保存&#34; 0&#34; &安培;将id_imp设置为NULL / 0(在table_primary中)。但如果有第三方,imp_status列应保存&#34; 1&#34; &安培;保存有关此第三方的所有信息(使用主键id_imp到table_imp)。 这是正确的方法(对于脚本)?我该如何修改模型和控制器? 这是我当前的控制器(还没有imp_status和id_imp):

...
$imp_comp_name=$this->input->post("imp_comp_name");
$this->form_m->set_imp_comp_name($imp_comp_name);
$imp_comp_dir=$this->input->post("imp_comp_dir");
$this->form_m->set_imp_comp_dir($imp_comp_dir);
...
$result = $this->form_m->insert();

这是我现在的模特:

// there are properties & setters
public function insert(){
   $sql = "INSERT INTO table_primary
          (...,imp_comp_name,...) VALUES (...,'".$this->imp_comp_name."',...)";
   return $this->db->query($sql);
}

我希望我的问题不会令人困惑。非常欢迎阅读和学习的链接。谢谢。

1 个答案:

答案 0 :(得分:0)

虽然你的问题有点令人困惑,但我想回答它,你需要进一步的帮助,你可以用完整的源代码回复我。

首先在服务器端,单选按钮值将被获取                $ imp_status = $这 - &GT;输入 - &GT;柱(&#39; imp_status&#39);               和值将是所选单选按钮的值,并根据此单选按钮的值,您可以更新您的数据库并创建两个表之间的关系。

但是在您的查询中,您如何确定该表单中有第三方,请详细说明如何获得更多信息。