Display the ajax data on the same page in laravel - php

I am working in laravel 5.1. I have three pages in routes Home,show,short.
I also have blade file for home and short routes
I have a form on home page .After submit the button on home page I want that
(1). the data will be save at the database via post request which code has been written in a method postdata() which routes is show..
(2). I want to display the results based on the inserted data after doing query from database which code has written in showdata() method of short route .
I want all that happen after clicking the submit button without refreshing the page.but my problem is that i have two routes and in url of ajax method how can i give two url. please suggest me the better and efficient way to do that.

Related

Laravel: Displaying data from db after clicking a button - but staying on the same page

That's the task I am currently facing:
Starting point: I have a form, which accepts data and saves it into the database.
What needs to be done: I need to include a button called "show data" (a different one than the "submit" button in the form, it can even be a clickable div), which will then display the data from the database on the same page, under the form. The button should also enable refreshing the list after new data was inserted into the form.
Now, if the data from the database had to be displayed on a different page, that could be handled easily with routing. However, for this particular task, I need to display the data on the same page.
My question: Is there a pure Laravel/PHP way to solve that problem? I am considering using AJAX (to which I am also new), but if there is some Laravel specific solution then I would prefer that.
The solution needs to work on localhost.
For PHP, without having any help from JavaScript, showing any changes to the database will require a browser refresh. On the front-end, you can use VueJS and Axios to help you with these requests, which do not require the browser to be refreshed.

Harsh redirect back() Laravel5.2

I am having a small issue with my Laravel 5.2 application. The problem is with a page made to adjust database entries.
When data has been adjusted and a button is pressed to update the database, it works great. Problem is, I would like to redirect back() to the previous page, but this page has been set up through data from other pages.
So when I redirect back, it error's and says that certain parameters are not defined. Is there a option, that I can implement a very static redirect to the previous page and all the data that has been loaded in the page?
Like the go back function that web browsers have?
Solution
I added the following code in the Controller function where the page is made to adjust the database entry:
Session::flash('url',Request::server('HTTP_REFERER'));
And as a redirect I have this:
return Redirect::to(Session::get('url'));

PHP MVC Form Submission

I have a question related to form submission done in PHP application that's built in MVC architecture (self-written framework).
All examples that I've seen so far (including existing back-end frameworks) work this way that once form for adding record to database is submitted then certain method of controller is executed [say i.e. addRecord()], which triggers method of appropriate model. If everything goes OK then record is added and controller's method [addRecord() in this example] renders view of "index" page that displays table with records from database.
What I would like to achieve is to render view with form used to add records (the same that I used to add first record) instead of "index". Obviously I can do it easily by just rendering appropriate view from addRecord() (view with the form).
But the tricky point is when you check url you'll see the following:
The first time you enter it will be i.e.
http://project_name/my_controller/create
Once first form was submietted and you return to the view from addRecord() method then url will be:
http://project_name/my_controller/addRecord
What I would like to see is return to the original url, that is http://project_name/my_controller/create
Not sure if this is clear?
PS. Of course I could use AJAX call for form submission (that way I will stay at the same page) but perhaps it's possible to achieve the same without AJAX.
Thanks in advance,
On the controller you will want to submit to the addRecord route and do the processing. Have a check to make sure it was successful and on successful submission you can redirect back to the create route.
It is hard to give an example since you are using a custom made framework. I use slim which has a redirect method for a route. If what you have made does not have something like that then using should do the trick.
header('Location: '.$createUrl);
die(); //or exit

Laravel: Update route without page refresh

I have a page that lists down all todo items stored in a database. The page / view is returned by a controller TodoListController.php.
public function index()
{
// `TodoList` is the model, ie extends Eloquent.
$todo_lists = TodoList::all();
return View::make('todos.index')->with('todo_lists', $todo_lists);
}
What I am trying to do is to add a link that will open up a lightbox for editing an existing todo. Each todo has its own edit link; that is, one link corresponds to one todo item. Once opened, I'd also want to pre-populate the form fields with existing data / values. Upon success clicking the 'Edit' button), the lightbox should close and we immediately reflect the page with the new values ie no page refresh.
Questions:
If I linked the edit form to an update route eg Form::model($list, array('route' => ['todos.update', $list->id], 'method' => 'PUT')), the page would have to refresh, right?
Would it be more advisable to do this via AJAX? Or, maybe that's the only way in order to avoid page refresh?
Thanks.
Yes, javascript/ajax is the only way to update page data without refreshing the page
You'll want to populate the lightbox form using data from the page (or data from an AJAX POST), then when the user submits the form, make an ajax request to your update route, and finally update the page with the data the user entered.

kohana setting url for Ajax Modal Form

I have a Kohana controller, and a route corresponding to it.
also, i want to make a form that will appear as a ajax modal, when a user clicks a link. The problem is: i want this form to be controlled by a different controller action, and of course, because i am using exactly the same data as in the view using the first controller action, i will have exactly the same parameters (in the second controller action).
The problem is that, having the same parameters and only different actions, my both controlers's routes will point in the same page. So...
How can i make an ajax modal form that will use exactly the same variables that the view in which i am putting the link to the modal form is using? is it indicated to make another controller action?
Thank you!
I guess you just should create 2 different views.

Categories