【问题标题】:CodeIgniter 3: form is not submittingCodeIgniter 3:表单未提交
【发布时间】:2018-03-19 03:25:01
【问题描述】:

我是 codeigniter 的新手,我已经尝试了所有方法,但我的表单仍然没有提交到数据库。

这是我的代码。我真的找不到我哪里出错了。

查看

<?= form_open('forms/submit_shifter_form');?>              
<div id="form-interview-fill-mainform" class="form-group">
    <div class="container">
        <div class="col-sm-12">
            <div class="input-group">
                <label>Reason/s for shifting:</label>
                <input type="text" class="form-control form-input" name="shifter_reason">
            </div>

            <div class="col-sm-6">
                <div class="input-group">
                    <label>Strengths:</label>
                    <input type="text" class="form-control form-input" name="shifter_strength">
                </div>
            </div>
            <div class="col-sm-6">
                <div class="input-group">
                    <label>Weaknesses:</label>
                    <input type="text" class="form-control form-input" name="shifter_weakness">
                </div>
            </div>
        </div>          
    </div> 
<?php echo form_submit('submit','Confirm', 'class="btn btn-success btn-lg" id="submit"');?>   
<?php echo form_close();?>

控制器

<?php
defined('BASEPATH') or exit('No direct script access allowed ');

class Forms extends CI_Controller {
    public function session(){
     if($this->session->userdata('logged_in')){
        $session_data = $this->session->userdata('logged_in');
        $data['id'] = $session_data['id'];
        $data['username'] = $session_data['username'];
        $data['password'] = $session_data['password'];
        return $data;
    }
}
    public function submit_shifter_form(){
         $shifter = array(
            'course' => $this->input->post('shifter_course'),
            'reason' => $this->input->post('shifter_reason'),
            'strength' => $this->input->post('shifter_strength'),
            'weakness' => $this->input->post('shifter_weakness'),
            'futureContribution' => $this->input->post('shifter_contribution')
        );
       $session_data = $this->session->userdata('logged_in');
    $id = $session_data['id'];
        $this->load->model('forms_model');
        $this->forms_model->shifter_form_submit($id,$shifter);
        redirect(site_url('profile'), 'refresh');
    }

}

型号

<?php
defined('BASEPATH') or exit('No direct script access allowed ');

class Forms_Model extends CI_Model{
    function shifter_form_submit($id,$shifter)
    {
         $this->db->insert('tbl_prototype_shifter',$shifter);
            $insert_id = $this->db->insert_id();
            $shift = array(
            'studentId' => $id
            );
            $this->db->where('id', $insert_id);
            $this->db->update('tbl_prototype_shifter', $shift);
    }

}

我的其他表单在我的数据库中正确提交数据,在我添加这个新表单后,我似乎不再正确提交。拜托,谁能帮忙,我不知道我哪里出错了

【问题讨论】:

  • 你的提交按钮在哪里?
  • 我刚刚编辑过,抱歉
  • 您确定&lt;?= 打印表单标签吗?
  • 我改成
  • 表单未提交,能否请您也分享一下您在表单提交后看到的错误?

标签: php forms codeigniter submit


【解决方案1】:

你有

$shifter = array(
            'course' => $this->input->post('shifter_course'),
            'reason' => $this->input->post('shifter_reason'),
            'strength' => $this->input->post('shifter_strength'),
            'weakness' => $this->input->post('shifter_weakness'),
            'futureContribution' => $this->input->post('shifter_contribution')
        );

但是你的表单有,

shifter_reason
shifter_strength
shifter_weakness

所以剩下的所有字段都将为空,您的表格是否接受剩余字段的空值?

如果没有,则设置一些默认值,

在控制器中

确保您已加载数据库:

function __construct() {
    parent::__construct();
    $this->load->database();
    $this->load->model('forms_model');
    $this->load->helper('form');
}

在你的其他方法中,

$session_data = $this->session->userdata('logged_in');
$this->forms_model->shifter_form_submit($session_data['id'],$shifter);

在模型中,

function shifter_form_submit($id,$shifter)
{
        // try to insert
        $this->db->insert('tbl_prototype_shifter',$shifter);

        // check if insert was ok
        if($this->db->affected_rows() > 0)
        {
              // if ok get last insert id
              $insert_id = $this->db->insert_id();

              // data to be updated
              $shift = array(
                       'studentId' => $id
              );

              // update 
              $this->db->where('id', $insert_id);
              $this->db->update('tbl_prototype_shifter', $shift);
        }else{
                // failed to insert, so cannot update other table too
               echo $this->db->_error_message();
               throw new Exception("Can't insert the resource");
       }

}

【讨论】:

  • 我在 else 中添加了一个警报,仍然没有。我已经检查了文件名以防错字,但仍然没有提交
  • 我没有收到任何错误消息,每次我点击提交按钮时,页面都会自行重新加载
  • @domin0101 评论这一行,看看有没有错误redirect(site_url('profile'), 'refresh');
  • 我评论了这行,还是一样。我知道当我单击提交按钮时,控制器中的功能应该显示在 url 中,但是在我的中,该功能没有显示,页面只是简单地重新加载
  • 请在模型函数print_r(func_get_args()); die();中这样调试,一旦你提交,调用模型方法,程序应该打印参数并退出;
【解决方案2】:

试试这个你的提交按钮。

<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多