Updating Data with Ajax in Laravel

Hey there, could you walk me through the process of updating with Ajax?

I'm working with Laravel

I prefer using HTML and Ajax only

Here are my routes:

Route::post('/post/homepage', 'AdminController@HomePage');

Answer №1

To start, it's important to give your route a name:

Route::post('/post/homepage', 'AdminController@HomePage')->name('post.create');

Next, you can create your HTML form:

<form id="myForm">
{{csrf_field()}}
<label for="name">Enter Article Name :</label>
<input id="name" name="articleName" type="text" required>
<button type="submit">Save</button>
</form>

Remember that {{csrf_field()}} will add the CSRF field to the form. Alternatively, you can use:

<input type="hidden" name="csrf_token" value="{{csrf_token()}}">

For handling ajax interactions, I'll utilize jQuery:

<script type="text/javascript">
    $(document).ready(function (){
        $('#myForm').submit(function (e) {
            e.preventDefault(); //Prevent form submission
            var dataflow=$(this).serialize(); //Obtain input values
            $.post('{{route('post.create')}}', dataflow, function (data){ //post.create refers to the route name
                //Upon response from server, take necessary actions
            });
        });
    });
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I effectively exclude API keys from commits in Express by implementing a .gitignore file?

Currently, my API keys are stored in the routes/index.js file of my express app. I'm thinking that I should transfer these keys to an object in a new file located in the parent directory of the app (keys.js), and then include this file in my routes/in ...

"Encountering an issue where ng-repeat doesn't reflect updates after adding a new value to the array. Attempted to use $scope.$

Whenever the chat is opened and a name is clicked, it triggers $scope.openChat(user) which adds a name to the $scope.chat.openChats array. The ng-repeat is supposed to monitor this array for any new values but does not update. I attempted using $scope.$app ...

Storing data from an HTML table in a view using MVC4 - a step-by-step guide

Currently, I have an HTML table displayed on a webpage where users can input data at runtime. My goal is to store all the rows from this table into a database through the use of a controller. It's similar to how a WinForms application would save multi ...

Error: Attempting to access an undefined property ('useParams') which cannot be read

Hey there, I'm currently facing some challenges with the new react-router-dom v6. As I am still in the learning phase of this latest version, I could really use some assistance. import React from 'react' function Bookingscrren({match}) { ...

Sorting one column in datatables.net triggers sorting in another column

Facing a major issue that I'm struggling with. I am using datatable.net to showcase data retrieved from my MySql database. I have followed the API guidelines meticulously in order to sort columns by name in ascending order (and toggle to descending). ...

Troubleshooting Issue 415: Adding and modifying database records through jQuery AJAX in ASP.NET 6.0 MVC Application

Implementing the Add/Edit functionality for categories in my project led me to utilize a modal (pop-up) to transfer data to the backend without refreshing the page. To achieve this seamless interaction, I opted for ajax integration. However, encountering a ...

What strategies can I implement to stop Iframes from disrupting the browser's history when interacting with them?

In my particular situation, I utilize Iframes to display Grafana on my page, which showcases beautiful and user-friendly graphs. After examining, I noticed that interactions like zooming in or out on the graph using mouse clicks trigger a type of refresh ...

A guide on how to effectively simulate a commonJS module that has been imported in

How can I successfully mock a commonJS style module in Jest that I am importing for an external connection? The module is called 'ganblib.js' and it's causing some trouble when trying to mock it with Jest. Any advice or suggestions would be ...

Guide on setting up the installation process of Gulp jshint with npm?

Having trouble with the installation of JSHint. Can anyone point out what I might be doing incorrectly? This is the command I am using: npm install --save-dev gulp-jshint gulp-jscs jshint-stylish Getting the following error message: "[email protect ...

Having trouble replicating the progressive response from the server in node.js

Hey there, I recently dipped my toes into the world of node.js and decided to give it a shot. Following Ryan Dahl's tutorial (http://www.youtube.com/watch?v=jo_B4LTHi3I) as my starting point, I reached a section around 0:17:00 where he explains how s ...

Steps for including a font ttf file in Next.js

As a newcomer to Nextjs, I am eager to incorporate my own custom fonts into my project. However, I find myself completely bewildered on how to execute this task (my fonts can be found in the "public/fonts/" directory). The contents of my global.css file ar ...

The (window).keyup() function fails to trigger after launching a video within an iframe

Here is a code snippet that I am using: $(window).keyup(function(event) { console.log('hello'); }); The above code works perfectly on the page. However, when I try to open a full view in a video iframe from the same page, the ke ...

Finding the iframe document on Chrome

I've been attempting to dynamically adjust the height of an iframe based on its content, but I'm facing issues in the latest version of Chrome. In Chrome, 'doc is undefined' error is showing up. Surprisingly, everything works perfectl ...

Utilizing Promises with Chained .then() Functions

I am struggling with simplifying the readability of my code. I have separated it into two main functions, but I am still dealing with nested .then() statements. I am looking for advice on how to structure these functions more effectively. It is important ...

Identifying a specific string value in an array using jQuery or JavaScript

Currently, I am working on checking for duplicate values in an array that I have created. My approach involves using a second array called tempArray to compare each value from the original array (uniqueLabel) and determine if it already exists. If the val ...

Include a button alongside the headers of the material table columns

I am looking to customize the material table headers by adding a button next to each column header, while still keeping the default features like sorting and drag-and-drop for column rearrangement. Currently, overriding the headers requires replacing the e ...

Sending a parameter value when onClick function is called

Currently, I am learning React JS and facing a challenge with passing parameters (like button ID or tab value) to the Tab onClick event. It appears that I'm receiving 'undefined' results when trying to pass attribute names as parameters. Ple ...

How do I fix the scroll top issue caused by Jquery fadeIn?

Currently facing an issue with the jQuery fadeIn (or fadeOut) method. I have constructed an article rotator which is functioning properly. However, a problem arises when the page is scrolled to the bottom and the articles rotate. The fadeIn (or fadeOut) me ...

One method for assigning a unique identifier to dynamically generated buttons with jQuery

<table border="0" class="tableDemo bordered"> <tr class="ajaxTitle"> <th width="2%">Sr</th> <th >SM</th> <th >Campaign</th> <th >Day1</th> <th >Da ...

Tips for excluding files in a webpack configuration for a Vue application during the production build

I am attempting to remove an Html file named "dev.html" from the final product build. What configurations do I need to make in webpack for this? I understand that rules need to be applied, but where exactly do I need to configure them? Below is a snippe ...