Categories

Wednesday, December 25, 2024
#919814419350 therichposts@gmail.com
LaravelLaravl 5.7VueJs

Vue Laravel Tutorial Part 6 – Form Submission

Vue Laravel Tutorial Part 6 – Form Submission - The Rich Post

Hello to all, welcome to therichpost.com. In this post, I will continue with Vue Laravel Tutorial Part 6 – Form Submission.

Form Submission is the very important part of every website for collecting user data.

Vue Laravel Tutorial Part 6 – Form Submission

In this, I have used Vue- Axios Form Submission.  Axios is use or display data.

With the default integration of Vue in laravel, things look easy and we will not face any problem related to cons like other.

Axios for get data:
axios .get('url') .then(response => (this.info = response)) 
Axios for post data:
axios .post('url') .then(response => (this.info = response)) 
Now come to the point:

First before start this tutorial, you need to check old Vue laravel Tutorial Series must.

One thing must say, To understand this post, you need to see 
past Vue Laravel Tutorials Posts and here are the links:

https://therichpost.com/vue-laravel-tutorial-part-1

https://therichpost.com/vue-laravel-tutorial-part-2

https://therichpost.com/vue-laravel-tutorial-part-3

https://therichpost.com/vue-laravel-tutorial-part-4-form-validation

https://therichpost.com/vue-laravel-tutorial-part-5-vue-routing

 

Here is the working and tested code, you need to follow:

 

1. Very first, you need to update your resources\js\components\adduser.vue file:

I have used axios post in it for submission the form and you need to just update this code with last code.

<template>
    <div>
        <h2>Add User</h2>
        <p v-if="errors.length">
            <b>Please correct the following error(s):</b>
            <ul>
              <li v-for="error in errors">{{ error }}</li>
            </ul>
        </p>
          <form @submit="checkForm" id="createAdministrator">
            <div class="form-group">
              <label for="email">Email:</label>
              <input v-model="email" type="email" class="form-control" id="email" placeholder="Enter Email" name="email">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
          </form>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        },
        data() {
        return {
            errors: [],
            state: {
                email: ''
            }
        }
    },
    methods:{
    checkForm: function (e) {
       
      this.errors = [];

      if (!this.email) {
        this.errors.push('Email required.');
      }
    else
    {
    var formContents = jQuery("#createAdministrator").serialize();
    
    
      axios.post('/admin', formContents).then(function(response, status, request) {
                    alert(response.data);
                }, function() {
                    console.log('failed');
                });
    }
    

      e.preventDefault();
    }
  }
    }
</script>

 

 2. Second, you need to make new controller file  AdminController.php in app\Http\Controllers folder and add below code into it:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController extends Controller
{
     public function store(Request $request)
    {
    //here you can insert query, I have just added json data to confirm that code is working fine
    	return json_encode("Record Updated Succeessfully");
    }
}

 

3. Now, you need to add below code into your routes/web.php file:
Route::post('/admin', 'AdminController@store');

 That is it, after submit the form, you will get an alert message “Record Updated Successfully”.

form submission data

This is just example working, if you want go deeply or If you have any query related to this post, then please do comment or ask question.

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

4 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.