php – CodeIgniter form_validation-> run()总是返回false?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – CodeIgniter form_validation-> run()总是返回false?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我是CodeignITer的新手,我一直在尝试实现表单提交功能,但每当我按“提交”时,表单页面只会刷新,数据库不会更新!似乎$this-> form_validation-> run()总是返回false,但我不知道为什么.

控制器功能如下:

public function write_PRof_review($prof_id)
    {
        $this->load->;model('Queries');
        // form stuff here
        $this->load->helPEr('form');
        $this->load->library('form_validation');

        $data['prof_id'] = $prof_id;
        if($this->form_validation->run() == FALSE){
            $this->load->view('create_prof_review',$data);
        }
        else {
            $this->Queries->submit_prof_review($prof_id,$this->USERID);
            $this->load->view('form_submitted');
        }       
    }

这是模型中的函数submit_prof_review():

function submit_prof_review($prof_id,$user_id)
    {
        $data = array(
            'course_code' => $this->input->post('course_code'),'easiness' => $this->input->post('easiness'),'helpfulness' => $this->input->post('helpfulness'),'clarity' => $this->input->post('clarity'),'comment' => $this->input->post('comment')
        );

    $average = round((($data['easiness'] + $data['helpfulness'] + $data['clarity'])/3),2);
    date_default_timezone_set('Asia/Hong_Kong');
    $date = date('m/d/Y h:i:s a',time());

    $data['average'] = $average;
    $data['date'] = $date;
    $data['course_id'] = 0; 

    return $this->db->insert('review_prof',$data);
}

最后是表单视图(create_prof_review.PHP):

<h2>Write a review</h2>
<?PHP echo form_open('home/write_prof_review/'.$prof_id); ?>
<h3>Course code
<input type = 'text' name = 'course_code'></h3>
<h3>Easiness
<input type = 'text' name = 'easiness'></h3>
<h3>Helpfulness
<input type = 'text' name = 'helpfulness'></h3>
<h3>Clarity
<input type = 'text' name = 'clarity'></h3>
<h3>Comment</h3>
<textarea name = 'comment' rows = '4' cols = '50'></textarea>
<br>
<input type = 'submit' name = 'submit' value = 'Submit'>
</form>

被困在这几天,但我仍然无法弄清楚出了什么问题.任何帮助将不胜感激!

解决方法

我认为这种情况正在发生,因为您尚未设置任何验证规则.
控制器代码应如下所示:

public function write_prof_review($prof_id)
{
    $this->load->model('Queries');
    // form stuff here
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['prof_id'] = $prof_id;

    // here it is; I am binding rules 

    $this->form_validation->set_rules('course_code','Course Code','required');
    $this->form_validation->set_rules('easiness','easiness','required');
    $this->form_validation->set_rules('helpfulness','helpfulness','required');

    if($this->form_validation->run() == FALSE) {
        $this->load->view('create_prof_review',$data);
    }
    else {
        $this->Queries->submit_prof_review($prof_id,$this->USERID);
        $this->load->view('form_submitted');
    }
}

请参考the CodeIgniter user guide;它将为您提供有关验证规则的更多信息.

脚本宝典总结

以上是脚本宝典为你收集整理的php – CodeIgniter form_validation-> run()总是返回false?全部内容,希望文章能够帮你解决php – CodeIgniter form_validation-> run()总是返回false?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。