Validating jquery zip code

We use jquery for .load () form in div

Then we use jquery for .post (), which are formed in the codeigniter controller, i.e. / app / post

Then we want Codeigniter to do the validation, but not sure how to return to the page to display validation errors? If re re.load () the controller does not reload the object, and we lose data?

Are we getting it wrong?

+2
source share
2 answers

Save the verification messages in sessions from your controller, and then show them on the corresponding view / page, but if all the checks are correct, the user must destroy the session again.

+1

, , , .

-, $.post(), , , $.ajax(), , , .

, Codeigniter , , , ?

, , , jQuery (, CI), , .

$.ajax(), .

CI:

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

ajax:

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

$.ajax() jQuery , , , , , , , ajax var.responseText.

, - post, , , json-, javascript.

, , , , , , . .

+6

Source: https://habr.com/ru/post/1765547/


All Articles