Welcome Guest, Not a member yet? Register   Sign In
form_validation with jquery ajax/json
#1

[eluser]jblack199[/eluser]
Okay so I've been looking into the form validation stuff but from all the info I am not seeing how to do one thing...

Currently, my form validation is similar to:

Code:
$err = "";
        if ($this->input->post('name') == '') { $err .= "<li>Your name cannot be blank.</li>"; }
        if ($this->input->post('email') == '') { $err .= "<li>Email Address cannot be blank.</li>"; }
        if ($this->input->post('password') == '' || $this->input->post('password2') == '' || $this->input->post('password') != $this->input->post('password2')) {
            $err .= "<li>Password and Password Confirmation must match.</li>";
        }

The reason for the list items, is I am actually sending them back to jquery to be parsed and displayed... ie:

in my controller:
Code:
if ($err != '') {
            $resp['response'] = 'false';
            $resp['msg'] = '<ul>'.$err.'</ul>';
        }
        echo json_encode($resp);

in my js:

Code:
$.ajax({
             url: $(this).attr('action'),
             type: 'POST',
             data: $(this).serialize(),
             success: function(response) {
                 var obj = $.parseJSON(response);
                 if (obj.response == 'false') {
                     $('#regerrDisplay').html(obj.msg);
                     $('#regerrors').show();
                 }
             }

and my form:

Code:
&lt;form id="frmReg" class="right" action = "/auth/register"&gt;
            <ul>
                <li><label for="name" class="desc">Name: &nbsp;&nbsp;&lt;input name="name" type="text" size="30%"&gt;&lt;/label></li>
                <li><label for="email" class="desc">Email: &nbsp;&nbsp;&lt;input name="email" type="text" size="30%"&gt;&lt;/label></li>
                <li><label for="password" class="desc">Password: &nbsp;&nbsp;&lt;input name="password" type="password" size="30%"&gt;&lt;/label></li>
                <li><label for="password2" class="desc">Confirm Password: &nbsp;&nbsp;&lt;input name="password2" type="password" size="30%"&gt;&lt;/label></li>
            </ul>
            <div id="btncenter">
                &lt;input name="reg" type="submit" id = "reg" class="ui-state-default ui-button" id="button" value="" /&gt;
            </div>
        &lt;/form&gt;

So, how can I take form_validation items, turn them into <li> objects and query against them in my code to pass back to jquery?




Theme © iAndrew 2016 - Forum software by © MyBB