Laravel: Update route without page refresh - php

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.

Related

How to go back without asking to confirm resubmission of the form

I have a web page that loads all the data from a mysql database called datalist.php
From this page I can edit record by record with a button that redirects you to an editdata.php page adapted to the fid of the record.
Once edited as they want to see the changes, I don't redirect them to the main one letting them see the changes and simply clicking back or with a button they return to the datalist.php without any problem.
The button is this
echo "<p id='parrafo'><a style='padding:1px 20px'class='button rounded-0 primary-bg text-white w-0 btn_1 boxed-btn' href='javascript:history.back() '><--</a></p>";
PROBLEM
I added a search engine where the displayed data can be filtered.
When they use the search engine from datalist.php, I direct them to a page called search engine.php where, through a post method, I store what they are looking for in a variable and the data that users want appears.
But when they edit a filtered record, it is edited without problems, but when they go back, they return to the search engine.php and the message appears:
"Confirm form resubmission In order to display correctly, this web page needs the data you entered earlier. You can submit that data again, but that will cause the page to repeat all previous actions. Press Reload to submit the data and display the page.
Hit the page refresh button to resubmit the data needed to load the page."
Of course, if they update, they come back when the filtered data comes out.
Isn't there any way to store the variable used in the search so that when I go back I don't get this error or any solution??
simple! when user will submit form for that variable instead of making post request
option1: just use get request __url__?variable=... but this will not remember the variable when you go back
option2: store the variable in the cookie and just go to next page (eg. window.location.href = '...';). and in next page access the cookie from php.
If you are wanting to show the form to the user as a confirmation, but without the possibility of another post, then remove the form element and the button. Display all other boxes as they are (with the values populated from the POST array).
And display another message telling them that it has been successful.
You are using PHP, you can achieve this easily with it. If you are unsure, then post a short version of your code in a separate question.

Laravel 5: Multiple submit buttons to save data while navigating the page

Page one.php has a form with a "Next" button that submits the data to the controller and then redirects to page two.php
On page two.php at the bottom of the form there is a "Previous" button that I want to still save the data but go back to page one.php and also another "Next" button that saves the data and then redirects to three.php and so on.
In Laravel is there a way to do this without duplicating controllers and making it more complicated?
In addition to the buttons there is a navigation menu that would have the same pages so if the user modifies any data they could also click the nav button and then data would be submitted and then go to that next page that they clicked. Is this possible or do I have to have ajax contently listening for form input changes and then posting them to the controller?
Make use of sessions. Store the data in session and on every page check if session var exists. After user submits form, empty the session
https://laravel.com/docs/5.1/session#basic-usage

Display the ajax data on the same page in laravel

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.

How to pass values from lightbox to main page

My main application opens an user page. There are New / Edit / Delete buttons on the same page.
I am using Datatables Editor to display records. When I click on New button an lightbox opens up and the datatables editor build-in plugin shows add user template. When this template is submitted a new record is created. The backend has a AUTO_INCREMENT key which I want to be passed back to main page such that I display a message 'User ID : XXXXX has been created'
The issue is that even though I pass it as a session variable the main page which I believe since it is already loaded and cannot be refreshed after lightbox page is submitted - it never receives the updated session variable. As such session variable though set dynamically when user submits lightbox from backend is never seen in main page
How can I achieve the desired output?

Ajax issue while creating PHP pages and Popup window for user input

Actually I have to create one page that is showing all employee information like (name,age,salary,etc) and one add button. If i clicked on add it will open one pop up window which will take user input of (name,age,salary); after submit it will update my database and my first page without refreshing the page.
i have created 3 file.
1) Display.php where i have to display employee information and add button.
2) Add.html i passes this file to popup method on click of add button.
3) Update.php where i wrote database query on action of submit button.
Now i am confused where i should write Ajax ? that will help me to display the record on my first page.
Thanks in advance.
You only need 2:
Display.php in here you display the info and add button, have a seperate <div> that is hidden but shown with anonClick javascript function. In this div you have your submit button which will fire the ajax query.
2.Update.php This will receive the ajax query and return the new data to the first page.
You can put the javascript functions in their own file and call them into display.php if you want.

Categories