How can I send data from one page to another using Laravel? - php

So I'm using laravel 5.1 and I'm stuck in a weird situation.
I have 2 models Seasonand Tournament and lets say the url to add a Season model is ..../seasons/add and to view the season would be ..../seasons/(id)... standard stuff right?
But now I want to add a tournament from the link ..../seasons/1 and so I click a link that takes me to ..../tournaments/add... how can I send the Season model to that page without submitting a form with hidden input?
Currently I have this setup ..../seasons/1/tournaments/add using blade to generate the links. But this method just doesn't feel right....
Thanks.

How can I send data from one page to another using Laravel?: I would suggest that you do this from your controller. Take a look at Redirecting With Flashed Session Data, it might come in handy.
From the Flash Data documentation: "[...] it will only be available during the subsequent HTTP request, and then will be deleted [...]"
You can send your model, static values or whatever you want using ->with:
redirect('route')->with('key', 'value');

Related

Share form data with another view in Laravel

I have a Laravel Application with some Angular JS, I'm using Laravel to do the routing as it's easier for my team (who rarely use JS at all) to understand later, I have a route that allows me to create briefings, this is done first by selecting some options (which are passed as an array to the Laravel Controller and sent to the view) and then generating the project
Now, when I have the project generated I'm taken to another view with a sidebar, this sidebar allows me to change to different subsections of the briefing, for instance, there's a section for Development and another for Design.
These are different routes in Laravel that take the user to different forms, the problem is that these forms need to generate conditionally with blade's #if command and in order to do that, I need to get the original options array from my generator view into all of these other views.
How can this be achieved in Laravel 5.2?
Did you try to use SESSION ?
On submit a form, use SESSION to save some data that you need and you can easily find it at any moment with SESSION.

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

Save GridView state when clicking on edit button and returning back

First of all, let me explain the scenario:
I have a GridView in template and it works fine. Now imagine that a user did the following actions:
He applied some filters on data
He also sorted by some columns
What I want to do - is to save value of his actions, i.e save values of sorting columns and values of filters when he clicks on "edit -> save" and returns back.
It's something similar to flash, but for attributes. Is this even possible to do with Yii2's built-in tools?
Well, after assigning the parameters to your search model and before you return the data provider you could simply store the search model in the session and then load it again when the user returns to the grid view.
In one application I even use this method to store the filters in the user settings rather than the session so they persist between sessions, ie. after user logs out and back in again.

Proper way to pre-fill form after server-side validation failure, involving two separate php files

A very basic question. I have a form in one file form.php, I post it to another file processForm.php which does the server-side validation and processing. I am not using any framework.
Now, in case of form validation failure, I need to display the form again with all the values prefilled, without using a javascript history.back() from the processForm.php. What is the clean and proper way to do this so that I have all the posted values available again in form.php and can prefill them?
This is easy if the form submission happens to the same page, but this is how I got this and I cannot make the submission into the same page. So what would you do? Store the values in session? Curl post? Send the values using GET to form.php?
Why or why not? Please mention pros and cons.
Go read up on the MVC pattern.
You can't implement an interactive program without implementing a model, a view and a controller - the point is that your code should be structured to implement each of the three concerns as a single entity, be that as functions (or function trees), classes (or class trees) or files. And the three components within the pattern should be structurally grouped.
So if you want to the user to arrive at (say) second page after successfully filling in a form at first page, but to stay on first page when the form fails the validation, then a simple way to implement this would be to have first page implement the model view and controller, i.e. to both populate/generate the form and be the target for the form. Then if it receives a valid request sent from the form, send a redirect to second page.
This avoids the need for each page to load and process the MVC code for the preceding page as well as the current one - although that approach reduces the number of round trips to the browser which can help with performance.
NB using POST does not preclude the use of variables in the URL - indeed, I recommend using GET variables to indicate the data you wish to manipulate and POST variables to show how they should be manipulated.

Getting $_POST variable from table

I'm trying to build a sort of resource allocation form. I'd like to be able to print a table from a database, and then allow users to click on each cell that they would like to reserve. Also, being able to drag and select multiple cells. Then send all of this via $_POST to another php script.
Problem is, I have no idea where to start.
Any suggestions?
The first and most critical thing you're going to need from what you described is a bunch of hidden fields to store the information you're interested in. You would have to write javascript code on the client side to store the users interaction with your page into these hidden fields.
To receive data via POST, you will need <input type="hidden" name"some_field"> for every bit of data you wish to "know" about that was changed on your page. Table information is not transmitted in a POST operation if it's just text, so you can't see the layout of the modified table on post back to the server.
If you don't have to POST this data to another form, it is probably a better idea to make callbacks via XMLHTTPREQUEST as the user interacts with your page, but I don't know the requirements of what you're trying to do.
I wrote one for my school recently; the trick is to either use buttons/links or addEventListener the cells to JavaScript. If you want the source code to my app, download this zip file:
http://azabani.com/files/busbook.zip
Edit:
My system works in the following way:
addEventListener to cell clicks, calling book()
book() then sets location to book.php
book.php does the database work
book.php sets the location header to immediately go back to the viewer
The system knows which week view to go back to based on session variables.

Categories