1

Form can be successfully submitted via ajax and the notification message can be displayed in jquery window ("Your name has been successfully changed!")but when I submit an empty form it doesn't return the message "fail". Could you please check my code and help me to find my mistake.

Controller:

$this->load->library('form_validation');
$this->load->model('my_model');   

$this->form_validation->set_rules('name','Name','required|trim|alpha|min_length[3]|xss_clean');

if($this->form_validation->run()) {
    $user_id = $this->session->userdata('user_id');
    $name = $this->input->post('name');
    if ($this->model_users->did_change_name($user_id, $name)) {
        $data = array(
            'message_ok' => "Your name has been successfully changed!"
        );
        echo json_encode($data);
    } else { 
        $data = array(
            'message' => 'fail'                   
        );
        echo json_encode($data);         
    }
}
4
  • 1
    Look at the request response in the browser's developer tools (CTRL+I, normally) - the PHP script may be error'ing without you knowing. Commented Aug 8, 2014 at 16:18
  • 1
    @NiceTry Please take some time to format your code correctly, i did it all for this question but remember is more fun to answer questions when the code is well indented! Commented Aug 8, 2014 at 16:23
  • I check in console network, there are not any errors Commented Aug 8, 2014 at 16:25
  • Mathieu, thanks for formatting. Actually, I thought it was well formatted :) Commented Aug 8, 2014 at 16:29

3 Answers 3

1

It's all about parentheses :) Try this way:

if($this->form_validation->run()) {
    $user_id = $this->session->userdata('user_id');
    $name = $this->input->post('name');
    if ($this->model_users->did_change_name($user_id, $name)) {
        $data = array(
            'message_ok' => "Your name has been successfully changed!"
        );
        echo json_encode($data);
    } 
}
else { 
        $data = array(
            'message' => 'fail'                   
        );
        echo json_encode($data);         
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much:) I have just noticed that :)
1

I haven't used CI in a good while, but from my understanding you're checking if the form was valid to run a series of events, such as a name change. But when the form validation returns false that's when one of your validation rules have failed, which it has.

if($this->form_validation->run()) {

    // your code

} else {

    $data = array(
        'message' => 'fail'                   
    );
    echo json_encode($data); 

}

3 Comments

when the validation forms returns false, controller should return the message "false" but it doesn't work
Is nothing being returned, or does it return the successful message? If it's the latter the problem may be in your model.
when I submit a name, it changes the name successfully and returns the successful message. When I submit an empty form, it does not change the name and does not return the "fail" message.
0
if($this->form_validation->run()==TRUE) {

Right now your code is always going to return the first argument because you aren't actually checking the result of the validation. It's currently equivalent to saying if(1)

https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

1 Comment

I just tried in the way you described but nothing has been changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.