Created
January 22, 2014 08:23
-
-
Save nicopenaredondo/8555263 to your computer and use it in GitHub Desktop.
simple ajax snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Route::post('ajax',function(){ | |
| if(Request::ajax()) | |
| { | |
| $input = array( | |
| 'password' => Input::get('password') | |
| ); | |
| $rules = array( | |
| 'password' => 'required' | |
| ); | |
| $validator = Validator::make($input, $rules); | |
| if($validator->passes()) | |
| { | |
| $data = array('status' => true, 'errors' => $validator->getMessageBag()->toArray()); | |
| return Response::json($data); | |
| }else | |
| { | |
| $data = array('status' => false, 'errors' => $validator->getMessageBag()->toArray()); | |
| return Response::json($data); | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="validation-errors" style="display:none;"></div> | |
| <div class="row-fluid"> | |
| <div class="span12"> | |
| <h4>1. Forgot Pincode ?</h4> | |
| <form id="forgot-pincode-form" class="form-horizontal"> | |
| <div class="control-group"> | |
| <label class="control-label">Password</label> | |
| <div class="controls"> | |
| <div class="input-append"> | |
| <input class="span12 m-wrap medium" id="password" type="password" name="password"> | |
| <div class="btn-group"> | |
| <button type="button" class="btn" id="forgot-pincode-submit">Submit</button> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| $(document).ready(function(){ | |
| $("#forgot-pincode-submit").click(function(){ | |
| $.ajax({ | |
| url : 'ajax', | |
| type : 'POST', | |
| data : { | |
| 'password' : $("#password").val() | |
| }, | |
| beforeSend: function(){ | |
| $("#validation-errors").hide().empty(); | |
| }, | |
| success : function(data,status){ | |
| if(data.status === true){ | |
| $("#validation-errors") | |
| .text('Validation Success') | |
| .css({'display' : 'block'}) | |
| }else{ | |
| $("#validation-errors") | |
| .text(data.errors.password) | |
| .css({'display' : 'block'}) | |
| } | |
| }, | |
| error : function(xhr, desc, err){ | |
| console.log('XHR' +xhr); | |
| console.log('Desc' +desc); | |
| console.log('Err' +err); | |
| } | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment