vue js serialize form submit in Laravel

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 – 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.

READ :  AngularJS Simple Line Charts using JSON

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.

Vue.directive(‘ajax-submit’, {
bind: function (el, binding, vuenode) {
//Devloped by form element
var $el = $(el),
//Devloped by Submit input button
datasub_but = $el.closest(‘form’).find(‘:submit’),
//Devloped by Submit input value
datasub_butText = datasub_but.val(),
//Devloped by Loading text, use data-loading-text if found
loadingText =‘loading-text’) || ‘Form Submitting…’,
//Devloped by Form Method
method = $el.find(‘input[name=_method]’).val() || $el.prop(‘method’),
//Devloped by Action url for form
url = $el.prop(‘action’);

READ :  Vue Js simple Registration Input Form Components

//Devloped by On form submit handler
$el.on(‘submit’, function(e) {
//Devloped by Prevent default action

//Devloped by Serialize the form data
var live_form = $el.serialize();

//Devloped by Disable the button and change the loading text
datasub_but.prop(‘disabled’, true);

//Devloped by make http call using jQuery
$.ajax({ url: url, method: method, data: live_form })
.done(function(res) {
//Devloped by Adding a body property to keep the same api
res.body = res;

//Devloped by Remove highlights

//Devloped by Reset the form

//Devloped by check success handler is present
if( && ) {, res);
} else {
//Devloped by run default handler
}).fail( function(err) {
//Devloped by Adding a body property to keep the same api
err.body = err.data_responseJSON;

//Devloped by check error handler is present
if( && ) {, err);
} else {
//Devloped by run default handler
}).always(function() {
//Devloped by Re-enable button with old value
datasub_but.prop(‘disabled’, false);

READ :  Simple Vue datepicker Example

Handling Callback


…//some code


methods: {
add_conatctForm(res) {

Default Error handler

// Default error data_response handler
function data_responseErrorHandler(data_response) {
// handle authorization error
if( data_response.status === 401 ) {
title: data_response.statusText,
text: data_response.body.txtmsg,
timer: 1500
}, function(){

// any other error
if( data_response.status >= 400 ) {
if( data_response.status === 422 ) {
// validation error
swal(“sorry Validation Error!”, getValidationError(data_response.body), ‘error’);
} else {
// handle other errors
var txtmsg = data_response.body.txtmsg || ‘Unable to process your data_req.’;
swal(data_response.statusText, txtmsg, ‘error’);

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.

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;


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( readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article( Are Most Always Welcome.

Leave a Comment