jquery post codeigniter validation

痴心易碎 提交于 2020-01-11 07:06:50

问题


We are using jquery to .load() a form into a div

We then use jquery to .post() that form to a codeigniter controller ie /app/post

We then want Codeigniter to perform validation but were not sure how to return to a page to display the validation errors? If re re .load() the controller wont it re-init the object and we lose the data?

Are we approaching this in the wrong way?


回答1:


Store the validation messages in sessions from your controller and then show them on the corresponding view/page but if all validation is done correctly by the user the you should destroy the session again.




回答2:


I'm going to take some liberties answering this question because I don't think I understand it.

First off, I don't know much about $.post(), so I am going to answer you're question as if you we're you using $.ajax() becuase that's what I know and I am pretty sure they are similar.

We then want Codeigniter to perform validation but were not sure how to return to a page to display the validation errors?

You don't return to a page to display the errors, you echo them out so that jQuery can receive the output (like a CI view file) and you can then handle the result however you want.

Using $.ajax(), here's what I would do..

The CI controller:

if( ! $this->form_validation->run($my_form_rules))
{
    // Set the status header so $.ajax() recognizes it as an error
    $this->output->set_status_header(400);

    // The error string will be available to the $.ajax() error
    // function in the javascript below as data.responseText
    echo validation_errors();

    exit();
}
else
{
    // Do something with the post data
    $result = $this->do_something();

    // Set the status header so $.ajax(0 recognizes a success
    // and set the header to declare a json response
    $this->output->set_status_header(200);
    $this->output->set_header('Content-type: application/json');

    // Send the response data as json which will be availible as
    // var.whatever to the $.ajax() success function
    echo json_encode($result);

    exit();
}

The ajax:

$.ajax({
    data: myPostDataObj,
    dataType: "json",
    type: "POST",
    success: function(data) {
        alert(data.message);
    },
    error: function(data) {
        alert(data.responseText);
    }
});

You can read more about $.ajax() in jQuery here, but basically, you're sending the post data to whatever controller you have setup, it takes that data, runs it through the validation process, and if it fails, it echoes out some standard text that ajax will send to your error function as var.responseText.

If it passes validation, you would do something with the post data, then return whatever results you want as a json object that can easily be used in you're javascript function.

I think that's probably a better solution, and I hope that helps explain a little bit of what's going. I hope.



来源:https://stackoverflow.com/questions/2331389/jquery-post-codeigniter-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!