简体   繁体   中英

Passing validation from controller back to ajax function

I am creating a login form using codeigniter and AJAX using jQuery. My question is around how to display the validation errors that are returned from codeigniter. Everything works if he user logs in properly but I use the validation tool in codeigniter and I would like to display these errors back on the page if the login fails. How can I pass back the validation errors from the controller into the .success of the ajax function calll? My code is below.. Thanks!

Login View

<script type="text/javascript">

    $('#login_form').submit(function(e) {

        var username = $('#username').val();
        var password = $('#password').val();

        var loginData = "&username=" + username + "&password=" + password;

        $.ajax({
             type: "POST",
             url: "<?php echo site_url('login/validate') ?>",
             data: loginData,
             success: function(data) {

                   //My question is how to dislpay data if validation_errors() has something and do nothing if login properly worked.
             }
        });
    });

</scirpt>

<form id="login_form">
  Username: <input type="text" name="username" id="username" size="15" /><br />
  Password: <input type="password" name="passwort" id="password" size="15" /><br />
  <div align="center">
      <p><input type="submit" value="Login" /></p>
  </div>
 </form>

Login Controller

function validate() {

    foreach($_POST as $key => $value ) {
         $$key = trime(value);
    }

    $query = ..... call the model to validate in DB this works and will return true or false if it authenticates...

    if( $query ) {
         //redirect to the loggedin view..
         rediect('site/loggedin');
    }
    else {

         //here I want to pass back the validation errors to be shown on the current view. 
    }

}

jondavidjohn is right on the dot. However, to get the validation, you need to return the validation_errors() via ajax:

You will need to have some place where you put in the validation errors:

HTML

<div class="errors"></div>

JQUERY

$.ajax({
    type: "POST",
    url: "<?php echo site_url('login/validate') ?>",
    data: loginData,
    success: function(data) {
        if (data == undefined) {
            window.location = "<?php echo site_url('controller/loggedin') ?>"; //javascript redirect by jondavidjohn
        }
        else {
            $('.errors').html(data); //how you get and set the errors to the html file
        }            
    }
});

PHP

function login() {
    if ($this->form_validation->run() != TRUE) {
      echo validation_errors(); // this returns the errors via ajax
    }
}

You will have to handle the redirect via javascript, so in the ajax success handler

$.ajax({
    type: "POST",
    url: "<?php echo site_url('login/validate') ?>",
    data: loginData,
    success: function(data) {
        if (data) {    //if the data returned is true (or 1)
            window.location = "<?php echo site_url('controller/loggedin') ?>"; //javascript redirect
        }
        else {         //if the data returned is not true (or 0)
            //show your error here
        }            
    }
});

and in your controller you'll just echo 1 or 0 depending on the success of the login. and if $query is a boolean true/false, just echo it.

function validate() {

    foreach($_POST as $key => $value ) {
         $$key = trime(value);
    }

    $query = ..... call the model to validate in DB this works and will return true or false if it authenticates...

    echo $query;  //should be true/false which will be interpreted by the ajax success callback.

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM