Support the ongoing development of Laravel.io →
Requests Input Session

Say you want to edit a record and post the form using jquery.
A prerequisite for this: You should already be familiar with Authentication / Security
and posting forms the regular way in laravel. Wouldn't hurt to watch some videos on
youtube, and grasp the basics of jquery. And learn how to include it if you are using
the form helper. This is just a basic example but it works.

Here is the simple technique used.

Edit form with jquery code at bottom, you could have a separate jquery file for this,
but keeping it simple for this and showing only one field being updated.

FORM:

<html>
<head>
<script type="text/javascript" src="include/jquery.js"></script>
<script type="text/javascript" src="include/myjq.js"></script>
</head>
<body>
</body>
<div style="margin-right:auto;margin-left:0px; width:600px;">
<!-- Can also be used for regular laravel post -->
<form id="peditform" method="post" action="pupdate">  

<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type='hidden' name='petid' value="{{ $data->petid }}">
<table style="border:none;">
<tr>
<td class="aright">petname:</td>
<td>
<input type="text" name="petname" id="petname" value="{{ $data->petname }}">
</td>
</tr>
<table>
    <p style="margin-left:50px;"><input type='submit' name='updatepet' value='updatepet'></p>
</form>
<a id="updatejq" href="">  [updatejq ]  </a>
<!-- Above line calls the jquery function -->
</div>
<?php
$vurl = Session::get('areturn');
echo "<a href=".$vurl.">back</a>";
?>
</body>  

<script>

$(function() {

$("a#updatejq").click(function() {
	//No special header needed, form is serialized, token goes right through
	//no different than a regular laravel post.
     dataString = $("#peditform").serialize();
     alert('here');
     $.ajax({
        type: "POST",
        url:'<?php echo "pupdate";?>',  //Notice here you just echo out the route to controller method.
        data: dataString,
        //dataType: "json", //////////json not applicable at all here. Commented out.
            success: function() {
            alert('updated');
            //You could even have a modal popup
            //here to notify an update.
                
                }
 	});
 return false;
 });
});  ///end all jquery



</script>
</html>

controller code for the update

 public function pupdate(Request $request){
            $petid = $request->input('petid');
            $petname = $request->input('petname');
            $postdata = [
            'petname' => $petname
            ];
            \Illuminate\Support\Facades\DB::table('pets')
            ->where('petid',$petid)
            ->update($postdata);
            return redirect(Session::get('areturn'));

Routes

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
$router->group(['middleware' => 'auth'], function($router)
    {
	Route::get('login', 'Auth\AuthController@getLogin');
	Route::post('loggin', 'Auth\AuthController@postLogin');
    	Route::get('pedit', array('uses' => 'PetsController@petedit'));
    	Route::post('pupdate', array('uses' => 'PetsController@pupdate'));
    });

Route::get('/', 'WelcomeController@index');

Route::get('home', 'HomeController@index');

Route::get('owner/mytest2', array('uses' => 'PownersController@mytest2'));





Route::get('logout', 'Auth\AuthController@getLogout');
Route::get('register', array('uses' => 'Auth\AuthController@getRegister'));
Route::post('doregister', array('uses' => 'Auth\AuthController@postRegister'));


Route::controllers([
	'auth' => 'Auth\AuthController',
	'password' => 'Auth\PasswordController',
]);

I know I do not have the best formatted code, but it works.
Anyway jquery is that simple to use..
And for those who wish to criticize the example, do so, but first show
your complete example of posting using jquery, so all can learn.
And I use controllers, I do not like cramming code in the routes.

Last updated 3 years ago.
0

Your article very good

I always code like you :)

But i think bester on success

success: function(response) {
	console.log(response);
	if(response.status == 'success'){
		// Action for success submit (update,post)
	}
	else{
		// Action for error
	}
}
0

Thanks, you are right I should have planned for a possible error. Maybe between my code and your added code, it will help someone. I admit my first post a couple weeks ago on this was hard to read the code.

Last updated 10 years ago.
0

I've extended the jquery ajax writing my own exception handler to show the user a js msg box.

When an error occurs in the laravel controller it throws an exception which will output a json string with the exception message as a response to the user. The ajax transport listener will show appropriate msg with the response.

0

I hate to spoil the party but do we really want this code on the forum as a Laravel example?

0

Well this is the way to post using jquery, and newbees ask this question over and over. Funny you didn't offer your solution with an example. No offence, but put up code or shut up should be the way it works.

Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jimgwhit jimgwhit Joined 13 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.