HI, I can't seem to get my ajax call working

Posted 2 years ago by JavonLegend

I'm trying to check if the username field exists before a user submits a form, are there any recommendations on the using the current version of Laravel

here's my current setup which returns a javascript error

The view

              {{ Form::open(array('post' => 'Auth\[email protected]'), ['id' => 'signup-form1', 'enctype' => 'multipart/form-data']) }}
                
                <input type="hidden" name="_token" value="{{ csrf_token() }}">

                <div class="container">
                    <div class="row form-div">
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="form-group"> <span class="skew"><span class="inside"><span class="icon-mwtMsg custum-font-icon"> </span></span></span> <span class="skew"> <span class="inside">
                                    <input type="email1" placeholder="Email Address" name="email1" id="regemail" onBlur="blur()">
                                    </span> </span>
                                    <div class="clearfix"></div>
                                    <div class="erroremail register-error-block"></div>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group"> <span class="skew"><span class="inside"><span class="icon-mwtTag custum-font-icon"> </span></span></span> <span class="skew"> <span class="inside">
                                    <input type="text" placeholder="Gamer Tag" name="gamertag" id="gamertag">
                                    </span> </span> 
                                    <div class="clearfix"></div>
                                    <div class="errortag register-error-block"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <p class="Create-account-btn">
                    <button class="btn btn-skew btn-red" type="submit" name="register" id="register-btn"><span>Create Account</span></button>
                </p>
            {{ Form::close() }}

The Script

$("#signup-form1").blur({ errorElement : 'div', errorLabelContainer: '.errorTxt', ignore: [], rules: { email:{ required:true, email:true, remote: { url: "register", type: "post", data: {email: $("input[email1='email1']").val(), _token: $('input[name=_token]').val()}, dataFilter: function (data) { var json = JSON.parse(data); if (json.msg == "true") { return "\"" + "Email address already in use" + "\""; } else { return 'true'; } } } }, gamertag: { required: true, alphanumeric:true }, }, messages: { email: { required: "Email address is required.", email: "Please enter valid email address.", remote: "Email address already in use!" }, gamertag: { required: "Gamer Tag is required.", alphanumeric: "Gamer Tag must contain alphanumeric string." } }, errorPlacement: function(error, element) { //$(element).closest('.skew').append() if (element.attr("name") == "email" ) { $(".erroremail").append(error); } else if (element.attr("name") == "gamertag" ) { $(".errortag").append(error); } else { error.append($('.errorTxt span')); } }, invalidHandler: function (event, validator) { //display error alert on form submit
                },
                highlight: function (element) { // hightlight error inputs
                    $(element).closest('.skew').addClass('has-error'); // set error class to the control group
                },
                unhighlight: function (element) { // revert the change done by hightlight
                    $(element).closest('.skew').removeClass('has-error'); // set error class to the control group
                },
                success: function (label) {
                    label.closest('.skew').removeClass('has-error'); // set success class to the control group
                },
                submitHandler: function(form) {
                    //console.log(form);
                    $("#signup-form1").submit();
                    // Adding a New Post
                }
            });

The Route

Route::post('register', 'Auth\[email protected]');

and The Controller

public function checkUsername(Request $request){ //$user = Register::where('email', Input::get('email'))->get(); $user = Users::where('email', $request->email1); if($user->count() == 1) { return Response::json(array('msg' => 'true')); } return Response::json(array('msg' => 'false')); }

Please sign in or create an account to participate in this conversation.