4

I'm new to CodeIgniter and I've been trying to implement a form submitting function, however whenever I press "submit" the form page simply refreshes and the database is not updated! It seems that the $this->form_validation->run() is always returning false, but I have no idea why.

The controller function is as follows:

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');
        }       
    }

And here is the function submit_prof_review() in the model:

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);
}

And finally the view for the form (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>

Been stuck on this for a couple of days, but I still can't figure out what's wrong. Any help would be greatly appreciated!

Kyslik
  • 8,217
  • 5
  • 54
  • 87
Cloud Strife
  • 99
  • 2
  • 2
  • 7

3 Answers3

14

I think this is happening because you have not set any validation rules. Controller code should look like this:

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');
    }
}

Please refer to the CodeIgniter user guide; it will give you more information about validation rules.

Ry-
  • 218,210
  • 55
  • 464
  • 476
nana.chorage
  • 496
  • 4
  • 15
  • thanks, this worked! However after successfully submitting the form I discovered a new problem with the php date() function, which turned up in the mysql database as 0000-00-00 00:00:00. Anyway, thanks again for the help with making the form post! – Cloud Strife Jun 08 '13 at 15:55
  • turns out I was using the wrong date format for the date() function, fixed it by changing it to date('Y-m-d H:i:s', time()) – Cloud Strife Jun 08 '13 at 16:04
  • thank you . it work after so many hours of debugging. i have a switch case form validation rules.. thanks – winnie damayo Feb 22 '18 at 07:43
3

I had the same problem though the cause was different. I was missing one of the input fields that I was validating from the form i.e

private function validate_data(){
        $validate_data = array(

            array(

                'field' => 'steps',
                'label' => 'Steps',
                'rules' => 'trim|required|integer|xss_clean'

            ),

            array(

                'field' => 'pace',
                'label' => 'Pace',
                'rules' => 'trim|required|integer|xss_clean'

            ),

            array(

                'field' => 'speed',
                'label' => 'Speed',
                'rules' => 'trim|required|numeric|xss_clean'

            ),

            array(

                'field' => 'distance',
                'label' => 'Distance',
                'rules' => 'trim|required|numeric|xss_clean'

            )

        );//end array validate_data

        return $validate_data;
  }

I was missing the speed input field in the form. I added it and the problem was solved. It really gave me a headache cause I was just reusing code that I have used so many times, so I could not understand why ($this->form_validation->run() was returning false, something that I had never experienced before.

theTypan
  • 5,471
  • 6
  • 24
  • 29
  • I just had something similar - a condition that turned a field on or off was set incorrectly, so the field was trying to validate despite not existing on the view that submitted it. You answer helped me to go back and think about what was going on, so thanks! – Rillus May 18 '18 at 13:05
0

You can go to system/library/Form_validation.php

and in

if (count($this->_config_rules) == 0)
{
return FALSE;
}

change false to true

ggdx
  • 3,024
  • 4
  • 32
  • 48
  • 9
    That's the worst answer I have ever read! Why edit a system file and introduce a bug?? – Christian Giupponi Aug 25 '13 at 13:54
  • 3
    Upvoted. This is actually a very good solution. By default, CodeIgniter returns `FALSE` if no rules are set and the form is validated anyway. However, in most (all?) cases, this is undesirable behavior. This comment's answer fixes that problem. – Nathanael Aug 12 '14 at 20:27
  • 4
    Two years on - @ChristianGiupponi is right, this is the worst answer ever. NEVER EVER, NO MATTER THE FRAMEWORK/CMS/WHATEVER, EDIT CORE FILES. Extend with your own files. – ggdx Apr 14 '15 at 09:46