validation - calling laravel route in javascript -
can call laravel route in javascript?
i trying implement front-end validation jquery validate plugin. reason doing front-end validation instead of validation function provided laravel using bootstrap modal login form , not want user dismiss form upon clicking "submit" button.
currently doing this:
$("#modal-form-login").validate({ errorclass: "error", errorplacement: function(error, element) { error.insertafter(element.parent("div")); }, rules: { email: { required: true, email: true }, password: { required: true, minlength: 4 } }, messages: { email: { required: "メールアドレスが必要です", email: "有効なメールアドレスを入力してください" }, password: { required: "パスワードが必要です", minlength: jquery.format("少なくとも {0} 文字") } }, submithandler: function(form) { $.ajax("http://mywebsite.com/login",{ datatype: 'json', data: $('#modal-form-login').serialize(), success: function(response) { console.log("ok!"); } }); } });
i understand post-validation handling should in submithandler, , want call dologin route (which reachable via {{ url: to("dologin") }}
in blade), can achieve in javascript?
you don't want making php calls in javascript. work, javascript file have served php. js , css files should static , should not served php. question should asking "how inject values javascript?" pull urls out of dom.
i'd set login form this:
<form id="modal-form-login" method="post" action="{{ url::to("dologin") }}">
then i'd update submit handler this:
submithandler: function(form) { $.ajax($('#modal-form-login').attr('action'), { datatype: 'json', data: $('#modal-form-login').serialize(), success: function(response) { console.log("ok!"); } }); }
as can see here, i'm pulling url action of form.
Comments
Post a Comment