I agree there's a lot of people asking questions about Laravel and AJAX at the moment, and it is pretty frustrating when a bit of Googling will provide the answers, but that's probably the least-helpful code example I've seen, ever.
Here's a much simpler, clearer, well-commented example any beginner should be able to copy and past directly into their routes file to get up and running in less than a minute:
It is good code but it would not help a newbie, a newbie needs to see how everything works in the views and the controllers and in the methods everything, for example I am returning something from a look up table so just the ajax alone don't help them they need to see how it all mingles with PHP. With which if you read the whole post you would see I refer them to videos on the subject from an expert, read the post. You have people asking this over and over and over again when there are excellent free expertly done videos on the subject that shows everything I repeat shows everything. I wish this forum had a mandantory search first built in. Your post doesn't even show how a request is sent back from the controller to jquery.
Ok it almost doesn't get any easier than this. To post with ajax, first the update code is
the same weather you are posting with laravel the regular way or ajax.
Below is controller code that does an update to a database.
public function petupdate(){
$petid = $_POST['petid'];
$petname = $_POST['petname'];
$species = $_POST['species'];
$sex = $_POST['sex'];
$ownerid = $cln->fixint($_POST['ownerid']);
$petowner = $cln->fixnull($_POST['petowner']);
$ostreet = $_POST['ostreet'];
$odate = $cln->fixint($_POST['odate']);
$ocheck = $cln->fixcheck(filter_input(INPUT_POST, 'ocheck'));
$dogpic = $_POST['dogpic'];
$postdata = [
'petname' => $petname,
'species' => $species,
'sex' => $sex,
'ownerid' => $ownerid,
'petowner' => $petowner,
'ostreet' => $ostreet,
'odate' => $odate,
'ocheck' => $ocheck,
'dogpic' => $dogpic
];
\Illuminate\Support\Facades\DB::table('pets')
->where('petid',$petid)
->update($postdata);
return redirect(Session::get('areturn'));
This is a typical jquery ajax post, that serializes the form and posts the data
through the controller. Form not shown as everyone should know how to make
a form and post it. Only difference is you are calling your jquery function to
send the data to the the laravel controller.
$("#petForm").submit(function(){
dataString = $("#petForm").serialize();
$.ajax({
type: "POST",
url:'<?php echo "editpet";?>',
data: dataString,
dataType: "json",
success: function() {
alert("updated");
}
});
return false;
});
The url comes from whatever route you have named for this controller method.
url:'<?php echo "editpet";?>',
There commited and simple. This should answer how to post with ajax.
Remember controller code is same weather ajax post or a regular laravel / php post.
Now if anyone cannot understand this simple technique yet, please, please stop what
you are doing and watch and learn from the free videos I mentioned in the thread.
If you know how to use ajax in html/php, there's no difference when using it on laravel. Good thing is you can use Laravel's route() helper so the url on your ajax will get the correct path.
I agree that laravel router really helps matters but just remember a lot of new people may not even know how to correctly use it in regular PHP that's where those videos really come in handy it teaches someone from scratch how to use jquery. Now if we can get them to actually watch and learn from the videos.
If there are any new devs reading this thread, please do not use this code. It's terrible. The author clearly is new to programming as well (hopefully!) and does not understand concepts like namespaces, security or even just using Laravel Facades. Let alone CSS and JavaScript.
IMHO, it'd be best if this thread was removed.
You are nuts, this is standard way of getting data from a lookup table to fill several fields. Name spacing has nothing to do with anything here. The code works for single page apps or MVC. Else you show another way with code, I bet you can't. How am I not using JavaScript?
Buddy you sound like a very dumb person. You need to show your way with full code or shut up. I mean all, nothing left out. And I will be trying your code. Your code needs to work.
In fact in a business application, it's standard to have a popup lookup table for things like shipper, customer, etc.
I look so forward to trying the code you come up with for a popup lookup to auto fill some form fields. I will be like a kid around Christmas, I can't wait. Please please hurry and post your code so I can try it.
And hopefully others agree, that they can't wait to see your code.
A few points to illustrate what @shabushabu is saying:
From a more practical point of view:
(I'm going to stop there)
And now you're being combative and rude to people.
Your advice in the other thread about newbies learning jQuery and PHP before coming to Laravel is good, but unfortunately this thread, however well-intentioned, is not going to help them in that endeavour.
shabushabu said:
IMHO, it'd be best if this thread was removed.
Agreed.
Thanks for the input, but how is this a simple example. It involves some complex code looking up values from another table. This is example code, of course I sanitize input as needed on a production application. And implement csrf tokens. The jquery ajax is a standard way of getting that data. I want to see shabushabu's code, I'm looking forward to seeing how he uses jquery ajax to accomplish an equal task of filling several fields from a lookup table. I can't wait.
@jimgwhit sorry pal but I have to agree with @shabushabu and @davestewart. You are not using Laravel to its full potential. A bunch of include statements on your Controller, Echos everywhere, Eloquent Model not being used correctly, Using of super globals. This thread should be deleted. You should have shown how to use simple ajax with Laravel. I know you work hard for this code but there a lot of room for adjustments.
I agree it was thrown up fast and not cleaned up good. But can we all wait on the code shabushabu is going to show us. I have had to actually do lookup tables at a trucking co. But I am ready and willing to see shabushabu's way of accomplishing this. I am looking forward to learning something new, or at least a different approach. I just don't see how the jquery ajax can differ that much. In fact the code I use comes from the jquery manual. Please hurry shabushabu and put your code here as many folks can benefit from it.
jimgwhit said:
Buddy you sound like a very dumb person. You need to show your way with full code or shut up. I mean all, nothing left >out. And I will be trying your code. Your code needs to work
This is not a pissing contest, mate. Have a look at the example from @davesteward, if you want to learn something new. The one you dismissed.
@davestewart, you mean this lavary/laravel-menu ? actually, its not mine, I just contributed to it(report bugs etc..). not much.. haha.. @jimhwhit, you need to relax bro.
shabushabu said:
jimgwhit said:
Buddy you sound like a very dumb person. You need to show your way with full code or shut up. I mean all, nothing left >out. And I will be trying your code. Your code needs to work
This is not a pissing contest, mate. Have a look at the example from @davesteward, if you want to learn something new. The one you dismissed.
I want to see your way of doing a lookup table, to fill several fields on a form. I believe my jquery was sound. I admit my laravel was a mess. I would never transfer secure data like this, but filling in shipper or receiver info is not a big deal.
oh man.. This code takes me back to 1990s..
using AJAX/JQUERY is not as hard as you have made it to be...
you do everything the NORMAL way first and make sure it works (ie form submits to right controller/page etc)... then
you can make this generic enough to be re-used on multiple forms..
Courtesy of Laracast tutorials on Ajax/Jquery..
Would you give a detailed example of filling several fields with your technique, including your code? My technique works, other people say there's another way, but not one has put up code showing the auto filling of several fields. Like this https://onedrive.live.com/?id=72C2FB003E9E7860%21118&cid=72C2FB003E9E7860&group=0&parId=72C2FB003E9E7860%21107&authkey=%21AJK71sIpP45kMVA&o=OneUp. Have to hit arrow to play, notice several fields are auto filled. Don't be like others, show code. I show code and it actually working. I want to learn your easier way.
@shez1983 please an example with full code and a video showing it in action. With at least 3 fields being autofilled. Oh and no dropdowns, remember the user may need to over ride a value. An excellant example, at the trucking co, you might be filling in these fields:
Bubba's Trucking Co. //company
999-999-9999 //phone
Bubba //point of contact
But on one particular load, you're told that Billy Bob at 888-888-8888 is handling it, so you see values have to be able to be over-ridden. Of course since I have written real world applications, I am aware of this already. But I really want to learn your easier technique, please hurry and put up your full example for me and others to learn from.
In fact I have never seen on any of the laravel forums a concrete example of auto-filling 3 or more fields from a popup lookup table, but yet in industry it is commom to have lookup tables to fill in things like shipper, receiver, carrier, etc.
Here is a simple example I wrote, although it might not have the best practice. Hopefully can still benefit someone. Since I no longer use Laravel, I am not going to show any PHP code, as backend, I am using Express.js instead, but the idea using router and repository pattern should be very similar to Laravel.
https://github.com/awsp/jquery-ajax-tutorial
Just follow the README and it should be good. This example is using SQLite in-memory database, so it should work for anyone even if you don't have any DBMS installed.
Most of the client side JavaScript code is located at, https://github.com/awsp/jquery-ajax-tutorial/blob/master/views/index.hbs
Tho, it's like a 1 hour project, let me know if there might be potential bugs. :D
Finally, @jimgwhit when we are talking about making complex UI or even SPAs like you had mentioned, jQuery is definitely not the best choice here, we should be using at things like React, Backbone, Angular, etc. I don't think the "Industries" only use jQuery for their projects.
Cheers.
Well bad code or no bad code, jimgwhit is trying to help. It is very difficult to find tutorials that go from basic to the next step intermediate or even just between those two, I think that is why they ask here. I have spend a lot of hours/days as well on the jquery and laravel research and I just cannot find that move beyond beginner where you want to start integrating it in your website. So we have to start asking and hoping that someone can help and understand that we are not pro's but learners.
Thank you for the good reply here I realize that my laravel code was a bit sloppy but if someone would look at the example they could easily write the larval code correctly but my jquery part was sound that's pretty much standard practice but again thank you for the nice reply. In real world application definitely use the laravel request methods and not regular post like I did, but other than that someone with PHP knowledge should get the general idea of how it all fits together. But also, it works.
@andrewtweber show your code and video of how it works, easy to comment, harder to give example. You are pathetic for criticizing without offering a better example. And I used this code at a trucking co., worked great. Was used to lookup carrier, shipper, etc info.
Furthermore, it's not bad code, rather thrown up quick not formatted good, I admitted that. But it's sound working code. Code doesn't have to be formatted pretty to work good.
Also I do not like using js libraries, or js except for things like database lookups. Js libraries are way over used by people who are totally without a clue.
Anyway sport, how about get your act together.
@awsp thanks for sharing code, I can't believe the other people who criticize, but refuse to supply their example. Hum??? And I mean an example like I gave actually filling in three or more fields automatically not just some generic hogwash. I will even carry this a step further I say there is no super short way of doing this without quite a bit of code, a little two liner won't do it.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community