使用代码点火器将下拉值提交到数据库

时间:2016-06-28 16:19:21

标签: codeigniter drop-down-menu model insert

我目前有一个表单,其中包含一个下拉菜单,其中包含从数据库中获取的菜单项。 我想要实现的是允许用户选择其中一个值并将其添加到数据库。表单当前正在提交和插入,除了数据库。当我尝试将下拉值添加到数据库时,所有输入都返回null。如果我把它拿出来,它可以正常工作。

install.packages()

控制器:

 <form name = "form1" id = "form1" method ="post"> <!--action="<?php echo base_url()."index.php/Admin/create_user"; ?>"-->
    <?php echo validation_errors(); ?>
    <label for="first_name" class = "labelForm">First Name:</label>
    <input type="text" id="first_name" name="first_name" class = "input2">
    <label for="last_name" class = "labelForm">Last Name:</label>
    <input type="text" id="last_name" name="last_name" class = "input2">
    <label for="username" class = "labelForm">Username:</label>
    <input type="text" id="username" name="username" class = "input2" onblur="check_if_exists();">
    <label for="password" class = "labelForm">Password:</label>
    <input type="password" id="password" name="password" class = "input2" onblur="validatePassword();">
    <label for="passconf" class = "labelForm">Password:</label>
    <input type="password" id="passconf" name="passconf" class = "input2" onblur="checkPasswords();">
    <label for="email" class = "labelForm">Email:</label>
    <input type="text" id="email" name="email" class = "input2">


 <!--   <label for="hospital" class = "labelForm">Hospital:</label>
    <select name="product" class = "input2" id = "hospitals">
        <option selected disabled hidden style='display: none' value=''></option>
        <?php foreach($hospital_dropdown as $option){?>
            <option id = "hospitals" name="hospitals" value="<?php $option->hospitalName;?>">  <?php print_r($option->hospitalName); ?> </option>
        <?php }?>

    </select>-->


    <label for="hospitals" class = "labelForm">Hospital:</label>
    <select name="product" class = "input2" id = "hospitals">
        <?php foreach($hospital_dropdown as $index => $option):?>
            <option id = "hospitals_<?=$index?>"
                    name="hospitals"
                    value="<?=$option->hospitalName;?>"
            ><?=$option->hospitalName;?></option>
        <?php endforeach;?>
    </select>

    <button type="button"  id = "new_user_submit">Add New User</button>

</form>

型号:

 function create_user(){

    $this->load->model('User_model');
    $password = $this->input->post('password');

    $hash = $this->bcrypt->hash_password($password);
    $data = array(
        'first_name' => $this->input->post('first_name'),
        'last_name' => $this->input->post('last_name'),
        'username' => $this->input->post('username'),
        'password' => $hash,
        'class' => $this->input->post('userclass'),
        'hospital' => $this->input->post('hospitals'),
        'email' => $this->input->post('email'),

    );
    $this->User_model->create_user($data);

    $username = $this->session->userdata('username');

    $data['hospital_dropdown'] =  $this->User_model->hospital_dropdown();


    $data['main_content'] = 'admin';
    $this->load->view('includes/admin/template', $data);
}

如果我取出控制器中的医院部分它会提交罚款,但是当我将其留下时,所有字段都会返回为空并且不会提交。 有任何想法吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

你在标记的php部分有几个错误:

  1. 创建唯一ID:<option id = "hospitals" >不是唯一的

  2. 你需要回显选项值,现在它是空的

  3. 您使用print_r()来回显您的hospitalName。通常,在您的方案中,您使用echo()执行此操作。 print_r对于测试变量内容很有用,请参阅更多here

  4. 还有其他语法可以将php与你的标记混合起来,CodeIgniter建议these

  5. <select></select>标记缺少/错误的名称属性,请参阅here。您正在使用名称属性“product”,但在您的控制器中,您可以编写'hospital' => $this->input->post('hospitals')

  6. 恢复:下面应该修复你的问题

    <select name="hospitals" id="hospitals">
      <?php foreach($hospital_dropdown as $index => $option):?>
          <option value="<?=$option->hospitalName;?>"> 
              <?=$option->hospitalName;?>
          </option>
      <?php endforeach;?>
    </select>