I have two pages. The search and the found.
On the search page there are searching fields.
The 'found' page gives back the results of search.
In the template of found page there is a button.
<a class="btn btn-default" href="/search">Back</a>
When I click on the "BACK" button, the input data, which had been typed in, disappears. How could I make a link, which retains the input fields?
You can store the searched variable in the "found" page, retrieve it when you come back to "search," and tell Laravel to "forget" it after you leave that page.
Store in /found:
Session::put("search", Input::get("search_field_name"));
Retrieve in /search when coming back:
Session::get("search");
Forget in /search after you've set the search field variable:
Session::forget("search");
This will display your last search even if you are not coming back from somewhere else. What you can do is only get and forget whenever your URL::previous() is a /found URL.
In case you want to store all the inputs at once, so you don't have to handle each of them sepparately, you can do with Session::put("search", Input::all());
Later on, you can retrieve them both as an array or as an object in order to access its properties:
$input_array = Input::all();
$input_object = (object)$input_array;
Then, access a property called, for instance, "search_term,", you can use the array:
$input_array["search_term"];
Or the object:
$input_object->search_term;
Hope this helps!
Related
I need your help!
I am new to codeigniter and I am trying to pass a fetch parameter from url, get me the value using the following
My example url is the following: http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816
Where what I am rescuing is the value "NroContacto", I have managed to bring me the value with the following line in the controller:
'NroContacto' => $this->input->get('NroContacto');
and show it in the view with the following:
<td>
Numero Folio: <?php echo $nrocontacto; ?>
</td>
that way I have managed to show it in the user's view, but now I can't find a way to save that value in the database when the user clicks the "save" button
You have not shared much information related to your issue. I am assuming that you need two server calls to save the URL parameter into data.
First "http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816", is used to render the page with a form to submit.
Second, When the user clicks the submit button in the form, data is stored in a database.
If this is the case, on first page, you can collect nrocontacto as you have already done in your code and add a hidden input field <input type="hidden" name="nrocontacto" value="<?php echo $nrocontacto; ?>"> inside the form in the view file.
When the user submit the form, you can grab the value using $nroContacto = $this->input->post('NroContacto', true); (this code goes to form processing controller method). Now you can use CI query builder $this->db->insert('tableName', array('field_name' => $nroContacto)); (Note: add other required fields for insert array) to save it to the Database.
Codeigniter support seo friendly urls so you can arrange your url like this
http://example.com/index.php/news/local/metro/crime_is_up
for your url it will be
http://localhost/infocargacasosfinal/index.php/creacionacta/1/103386816
Now you can fetch like this
'NroContacto' => $this->uri->segment(3, 0);
now pass the value to your view page.
You can get detailed documentation about url segments here
https://codeigniter.com/userguide3/libraries/uri.html
I want to know if there is a function where I can go back to my page after I submit a data.
For example, page 1, I submit my name, address and when I click next to page 2, it will show qualification, school. And after that if I want to go back to page 1 and change the value how do I do that? Such as Changing Name from John to John Doe when I go back to page 1.
I look around and saw something like return back, but does that help? Or should I use session?
Currently, I am just using redirects in my controller to bring the user to another page
Controller: (have something like that inside now)
public function UserInfo(Request $request){
$shirtSize = new user_info;
//get input data and some other stuff
$id->user_infos()->save($user_info);
return redirect(url('/userAddInfo/'.$id->id.'/AddInfo'));
}
Use the url()->previous() method:
Back
If you use redirects, you can use this solution to redirect two pages (requests) back.
I have a page called mainPage.php that contain a pagination script which displays a list of pages.
[1][2][3][4][5]
the pagination works fine but when I go to the next page the post data disappear.
for($i=1;$i<=$totalPage;$i++)
{
if($page==$i)
{
echo ' <input type="button" value="'.$i.'"> ';
}
else
{
echo ' <input type="button" value="'.$i.'"> ';
}}
is there any way to get the post data to the next page number after clicking this link:
<a href="mainPage.php='.$i.'">
without the form.
No, it's not possible. When a form is submitted (with method=post) to the server, that's one POST request. If you want to make another POST request, you need to make another POST request. If you click a link, that's not a POST request.
I'm assuming your scenario is something like a search form, which is submitted via POST and which returns several pages of results. In this case, POST is misused anyway. An HTTP POST request should be used for altering data on the server, like registering a new user or deleting a record. Just a search form is not data alteration, it's just data retrieval. As such, your form should be method=get. That will result in a URL with the form values as query parameters:
mainPage.php?search=foobar
You can then trivially create pagination URLs from that:
printf('<a href="mainPage.php?%s">...', http_build_query(array('page' => $i) + $ _GET));
Which will result in:
mainPage.php?search=foobar&page=2
This way all your requests are self contained GET queries.
try <a href="mainPage.php?page='.$i.'"> and get page no. using $_GET['page'];
You can do your job by using the GET method instead of post.If you want to make it in post request you need to do it seperate for each one.
I have a view in my project that has a few steps. The steps are handled through button clicks that take the user to the next step, and along the way, data is stored using JS and AJAX. On the last click, I want a user to make a selection from a list of checkbox selections, and I want to store and use this data to affect the next view (this last button click should redirect to another view).
I can store the data fine with ajax, but what I can't seem to figure out is how to send the data to the next view.
Basically, I want to collect the clicked data, send it back to my controller with ajax, store it, and then use it in a hyperlink (using a route) to effect the next view (using SESSION data):
<input id="btn-next" type="button" name="next" class="next action-button faded" value="Next" />
I hope that was clear. Any ideas on how to do this?
EDIT:
Ok, basically, it works like this:
Step 1:
Series of checkboxes (choose up to 3):
[] [] [] [] []
"Next Button" takes you to step 2 (with js, view is the same)
Step 2:
Series of checkboxes (passed from before) (choose 1):
[id1] [id2]
"Next Button" takes you to another view
In the new view:
It could say something like "You chose id2! Awesome!"
When a user clicks one of these boxes, and then clicks the next button, I want the id info from the box checked, to go back to my controller, be stored in my db (I am already doing this), but also, I want to carry this information to the next view, so that it affects the view using session data (using Session::get in Laravel).
My main question, is how do I pass this data back to the view, while also storing it using ajax and my controller. I assume this needs to be done using a route that takes the id info, but how do I pass that id info to the button hyperlink as shown above?
EDIT 2:
function loadStep3() {
var data = $('#onBoarding-hidden #step2').val();
if(data) {
var result = jQuery.parseJSON(data);
if(result[0]) {
result = jQuery.parseJSON(result[0]);
if(result.name) {
$('#step3 .name').html(result.name);
}
}
}
If I have understood it correctly, you want to store something upto next request. For this, you can use Session::flash('key','val') to put some data in session.
This data will be retained in session for just one request. Reference -
http://laravel.com/docs/session#flash-data
Basically you just need to put the data to the session and from the next request you may access it and in this case you may flash the data to the session only to get it back for the next request. So you may try this:
Session::flash('identifier', 'value');
Then on the next request after you redirect; you may access that data using this;
Session::get('identifier');
So it could something like this:
Session::flash('myId', Input::get('fieldname'));
Then in your view on the next request, get it from the session using:
#if(Sessuin::has('myId'))
<input id="btn-next" type="button" name="next" class="next action-button faded" value="Next" />
#endif
For more information about Session check it on Laravel website.
Update: From your comment I got the idea that you may do it:
if(result.name) {
window.location.href = 'url?name='+result.name;
}
Then on the next request (where you are being redirected) access the name from query string:
$name = Input::get('name');
Now you may use the $name as you want.
I face a problem whenever the user tries to browse to second page via $_GET if they have submitted $_POST data.
if(!isset($_POST['submit'])) {
//search input box
}
else {
//search details output
//pagination code
}
Whenever user press page 2, it shows //search input box back.
I want search to show page 2 and not //search input box back.
This is because you are not sending POST data to the second page.
$_GET and $_POST are set per request. If you want to save the first POST data, you will need to use sessions and store it in the session, or return the POSTed data to your page and have it be resubmitted.
As #Alan says... page 2 isn't receiving POST data, so submit is not set and it thinks the starting form should be shown again. A second GET variable (eg page) to track the results-page, will allow all three pages (start, page1+submit, page2)
if (isset($_REQUEST["page"])) {
//No data received, but reviewing & page-display code should go here
} elseif (isset($_POST["submit"])) {
//POST-processing code goes here
// Page-switching URL should be something like:
// Page 2
} else {
//Nothing posted, not paging - show input
//search input box
}
Don't forget to store your POST data somewhere temporarily so that you have data to display on the pages.
Seams like your logic is twisted. Just look at your code with aditional comments:
// if POST submit is NOT set
if(!isset($_POST['submit'])) {
// show search input box
}
You shouldn't be submitting a search request via POST. Searching is the kind of read-only operation ideally suited to GET requests and the query string. All you have to do then is modify the query string to include something like &page=2 to add pagination to your links.
A better way to do it is to split your form processing into its own script. This makes it more organized and more logical so that you can do things like POST to it via AJAX easier. A tip for solving the other problem I see you are having is if you are having a multipage form you should store the data they submit on previous pages somewhere. A common place is in session data. There are other ways to do it like storing it all in a javascript data object until the form is completed. Look into using a framework, they will help.