Support the ongoing development of Laravel.io →
posted 8 years ago
Database
Last updated 1 year ago.
0

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:

https://gist.github.com/davestewart/1db1c4b56a85fc46e1f8

Last updated 8 years ago.
0

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.

Last updated 8 years ago.
0

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.

Last updated 8 years ago.
0

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.

0

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.

0

Also, don't forget Laracast.

0

@jimgwhit - Are you a moderator?

0

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.

0

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.

Last updated 8 years ago.
0

@jimgwhit

A few points to illustrate what @shabushabu is saying:

  • You're using unfiltered superglobals which leave you at risk of SQL injection
  • You're building arrays to pass to models manually, rather than using something like Eloquent $fillable
  • You're have some DB::table()->update()s as well as Eloquent models for some reason
  • You're using PHP and echos, foreach, and string concatenation, rather than blade templating
  • You're using raw includes, when you could be extending, composing, or using traits
  • Where you're building HTML strings (which you shouldn't be) you're escaping double quotes where you could be using single and double quotes

From a more practical point of view:

  • You have 95% irrelevant code for a simple example
  • You've included a shitload of controllers, models and routes, for some reason (login is never spelled loggin by the way)
  • your code is littered with commented lines
  • you pasted your code in chunks in the thread, rather than using a gist, or repo (as there are so many files)

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

Last updated 8 years ago.
0

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.

Last updated 8 years ago.
0

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

Last updated 8 years ago.
0

Hey @beanmoss, looks like we write the same kind of plugins ;)

http://kohana.davestewart.io/docs/modules/nav/

Last updated 8 years ago.
0

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.

0

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.

0

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

0

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.

0

where is the laravel?

0

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

  1. set a class on the form ie. ajax-form
  2. check for this class in jquery and stop form submitting
  3. get all the post data via serialize
  4. get the url via form action attribute
  5. submit ajax request & get response..

you can make this generic enough to be re-used on multiple forms..

Courtesy of Laracast tutorials on Ajax/Jquery..

0

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

Last updated 8 years ago.
0

@shez1983 the code please.

0

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

0

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

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.

Last updated 8 years ago.
0

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.

0

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.

Last updated 8 years ago.
0

What a train wreck

0

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

Last updated 8 years ago.
0

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

Last updated 8 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.

© 2024 Laravel.io - All rights reserved.