vue js serialize form submit in Laravel
In this Post We Will Explain About is vue js serialize form submit in Laravel With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to vue js serialize form submit in Laravel Example
In this post we will show you Best way to implement Submit serialized form data with vue, hear for Submitting form in Vue.js with ajax with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.
Submit form using VueJs and AJAX in Laravel 5.1
Submitting a HTML Form using perfect article data_req will work so it does don’t any data give the user feedback while it’s submitting and overall not a great user experience.
We find myself creating Laravel apps which uses Vue in many places, so if it’s not a complete single page application SPA (without Vue router), to create forms more data interactive We have created a vuejs directive like as av-ajax-submit which We use regularelly in my web-apps to turn boring forms more some fun.
This vuejs directive takes care some of all thing data like disables form button and changes the text some error solved of submit button to more thing like Loading… file display
while vuejs call ajax data_req using vuejs goes through and data simple once data_req or data_response completes it also data handles if any form validation error happens and not submit, as well as on success to call web api and it some data send the page or we can use a simple callback using vuejs to do whatever some needs to be done.
Vue Directive for AJAX form Submit
Since the most web-application use jQuery based form submit in one or other way we have simple used it to create ajax call and data manipulate the HTML DOM to mark form input with generated errors.
[php]
Vue.directive(‘ajax-submit’, {
bind: function (el, binding, vuenode) {
//Devloped by infinityknow.com form element
var $el = $(el),
//Devloped by infinityknow.com Submit input button
datasub_but = $el.closest(‘form’).find(‘:submit’),
//Devloped by infinityknow.com Submit input value
datasub_butText = datasub_but.val(),
//Devloped by infinityknow.com Loading text, use data-loading-text if found
loadingText = datasub_but.data(‘loading-text’) || ‘Form Submitting…’,
//Devloped by infinityknow.com Form Method
method = $el.find(‘input[name=_method]’).val() || $el.prop(‘method’),
//Devloped by infinityknow.com Action url for form
url = $el.prop(‘action’);
//Devloped by infinityknow.com On form submit handler
$el.on(‘submit’, function(e) {
//Devloped by infinityknow.com Prevent default action
e.preventDefault();
//Devloped by infinityknow.com Serialize the form data
var live_form = $el.serialize();
//Devloped by infinityknow.com Disable the button and change the loading text
datasub_but.val(loadingText);
datasub_but.prop(‘disabled’, true);
//Devloped by infinityknow.com make http call using jQuery
$.ajax({ url: url, method: method, data: live_form })
.done(function(res) {
//Devloped by infinityknow.com Adding a body property to keep the same api
res.body = res;
//Devloped by infinityknow.com Remove highlights
removeErrorHighlight();
//Devloped by infinityknow.com Reset the form
$el[0].reset();
//Devloped by infinityknow.com check success handler is present
if( vuenode.data.on && vuenode.data.on.success ) {
vuenode.data.on.success.fn.call(this, res);
} else {
//Devloped by infinityknow.com run default handler
data_responseSuccessHandler(res);
}
}).fail( function(err) {
//Devloped by infinityknow.com Adding a body property to keep the same api
err.body = err.data_responseJSON;
//Devloped by infinityknow.com check error handler is present
if( vuenode.data.on && vuenode.data.on.error ) {
vuenode.data.on.error.fn.call(this, err);
} else {
//Devloped by infinityknow.com run default handler
data_responseErrorHandler(err);
}
}).always(function() {
//Devloped by infinityknow.com Re-enable button with old value
datasub_but.val(datasub_butText);
datasub_but.prop(‘disabled’, false);
});
});
}
});
[/php
Handling Callback
[php]
[/php]
[php]
{
methods: {
add_conatctForm(res) {
this.data_comments.push(res.body);
}
}
}
[/php]
Default Error handler
[php]
//infinityknow.com Default error data_response handler
function data_responseErrorHandler(data_response) {
//infinityknow.com handle authorization error
if( data_response.status === 401 ) {
swal({
title: data_response.statusText,
text: data_response.body.txtmsg,
timer: 1500
}, function(){
window.location.reload();
});
}
//infinityknow.com any other error
if( data_response.status >= 400 ) {
if( data_response.status === 422 ) {
//infinityknow.com validation error
swal(“sorry Validation Error!”, getValidationError(data_response.body), ‘error’);
} else {
//infinityknow.com handle other errors
var txtmsg = data_response.body.txtmsg || ‘Unable to process your data_req.’;
swal(data_response.statusText, txtmsg, ‘error’);
}
}
}
[/php]
Laravel Middleware for redirects data_response
In the case of some data return redirect from PHP Based laravel controller We maked this middleware data which turns some redirect to URL as a 200 data_response which is data later handled by some default data_response or data_req success handler of like as a v-ajax-submit directive to go here and the to that URL.
[php]
namespace App\Http\Middleware;
use Closure;
class AjaxRedirect
{
public function handle($data_req, Closure $next)
{
$data_response = $next($data_req);
if($data_req->ajax() && ( $data_response->status() == 301 || $data_response->status() == 302 ) ) {
return data_response($data_response->getTargetUrl(), 200);
}
return $data_response;
}
}
[/php]
I hope you have Got What is php – Submit form using VueJs and AJAX in Laravel 5.1 And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.