Hold selected state of checkbox in pagination - php

If I check some checkboxes on page 1 and then click Next to go to page 2 and then come back to page 1, the selected checkboxes are no longer checked. Is there any way to resolve this in PHP or JavaScript?

Since the data need to be accessed from different pages, you have to persist it somehow. The easier option is using the session store, or you can use a table in your database (note that the two strategies are not so different when you go to the bare, since you can easy back the session store with a table in your database). Your problem is basically the same as the shopping cart in ecommerce sites.
Note that if you choose the session and the data is stored inside an object, you may need to provide a way to serialize/deserialize it.

uhm.. i would probably go for an AJAX solution.
<script>
function saveMe(){
//ajax function which calls a page that sets the session data
}
</script>
<input onclick="saveMe()" <?php if(isset($_SESSION['chk'])){echo "checked";} ?> id="chk" name="chk" type="checkbox" />

Yes you need to create pagination using jquery because when you reload page to come back from page2 or ...
you will see your selected check box will be unchecked.
Here a reference for you to make jquery data table with pagination
http://www.sprymedia.co.uk/dataTables/example_multi_col_sort.html
http://www.jqueryrain.com/2012/04/best-ajax-jquery-pagination-plugin-tutorial-with-example-demo/

Related

Way to re-populate checkboxes on page?

I have a bit of an odd/unique situation, where I am currently looking for a way to 're-populate' checkboxes on my page.
First some background on how the page/app works.
1.) state1: You get simple search form. Enter in first or last or state, etc., hit submit.
It queries a DB and posts back to the SAME Page (?mode=results).
Inside of a MAIN container DIV, I loop through the returned records, and create (echo) a TABLE with a certain tabular layout to parse the values from each record/row (name, date, ID#, etc).
If I have 5 records returned from DB, I loop through and create 5 tables right after each other, populating each with the unique data from the record/row.
In each one of these tables I create, along with the data from the DB record/row. I also create/add in several check boxes.
Using some jQuery and on $(document).ready, I listend for the onClick event for all these checkboxes. So when clicked, I grab some data from the checkbox, as well as some parent info, and do the following:
a.) I create/add another element to the stage
b.) Make an Ajax call to an external .php script, so that I can update the SESSION with my array I have build (collecting all the user checks/input)
I build the array in JS/jQuery and use the Ajax call to get this data into my PHP session (anyone knows a way to NOT use this external .php script to update the SESSION var, let me know please!)
So to re-cap so far: There is no "FORM" (tags), just check boxes in multiple tables. There really is no 1-button (submit) event that POSTS all the checkbox data anywhere at one time. Each time a checkbox is clicked (or un-clicked), I update my SESSION array. That if/when a user IS ready to 'check out' (for lack of better term) (leave the page and go to another page), the SESSION data/array is 'current' and the data is ready to be used on any following pages.
so far everything posted above works just fine.
The question I have is: If the user wants to go BACK to this MAIN PAGE with all the checkboxes, what nice, simple/elegant solutions do I have to re-populate all these checkboxes?
I'm thinking the only approach I have is the check and see if this SESSION array exists when main page loads (meaning it's a re-visit, not the first time), and try to loop through this array and somehow target elements on my stage to find the correct table, then the checkbox ID?
Usually use jQuery to walk the DOM - not PHP?
And what about toggling the checkbox with PHP?
Is there a nice way to do this?
I don't really see your problem. I recapitulate:
You have a page with several <table> coming from your database (1 row = 1 <table>)
In these tables you have checkboxes
When you click on a checkbox, you do an ajax call to save the clicked checkbox in session
Your question is: how can I repopulate checkboxes when the use come again on the page
I think you already have the answer: just use what you stored in your session. When you build your <table>, and your checkboxes, test if they are in session:
<input type="checkbox" <?php if (!empty($_SESSION['blabla'][$myCurrentCheckboxID])) echo 'checked'; ?> />
If you want something more elegant, just create a nice PHP class or list of functions to generate your checkbox.
thanks for the suggestions.. this is what worked for me in the end (so far)
function sessionCheck($recordID, $checkBoxID){
for($r = 0; $r< count($_SESSION['userPicks']); $r++){
if($_SESSION['userPicks'][$r]['recordid'] == $recordID){
for($z=0; $z < count($_SESSION['userPicks'][$r]['itemsordered']); $z++){
if($_SESSION['userPicks'][$r]['itemsordered'][$z]['name'] == $checkBoxID){
return 'checked="checked"';
}else{
return "";
}
}
}else{
return "";
}
}
}
usage for MY project is like this:
<?=sessionCheck($row["id"] ."-A","Testimonial History") ?>

How to keep radio selected states when hitting browser back button?

I am handling a survey multistep form with many radio sets.
They have some default states
User operates over them to select his preferences.
Now the client wants when hitting either browser back button or the side navigator links (a set of links pointing to previous stages of the survey) to have the radio states the user previously chose.
Here is how the navigator looks like:
<ul>
<li>Step1</li>
<li>Step2</li>
<li>Step3</li>
</ul>
There is only one $_SESSION that collects data throughout the survey.
I know I can load information directly from $_SESSION but I need to replace the default states when the request comes from the navigator links/ back button.
After posting form, you can save values in the session. When accessing previous page again (or even first load it), read session's values and check specified radios. If you access page first time, there will be no data in the session, so no one will be checked.
You can also store selected values lively with JavaScript and cookies.
There are two main approaches to this, basically you are saying that the chosen state of the radio's need to be saved between different pages of your form.
So when you return to a page you were on previously but without submitting, the choices should still be the same.
There are two key approaches, server side with PHP or client side with Javascript
PHP
You can set any link on the website to submit the form and you could save the radio selections, then when you create the form you can check the previously selected value
This information could be saved in $_SESSION but they'd need to submit the form each time they changed the page (you could change the links to do this)
Javascript
You could write some javascript that remembered the content of the selections and stored them in the users browser as a cookie, this could update everytime they clicked a button. When the page loads the javascript would check for cookies (or local storage) and would load the previous options.
When they finish and submit the form you'd clear the javascript
Basic example
$('form input[type=radio]:checked')
Once I have more information I'll flesh out this answer further
A very simplified possible solution:
<?php
session_start(); // this needs to be at the top of all pages using $_SESSION (before anything else) else $_SESSION will always be empty
if($_POST['vehicle']){
$_SESSION['checkbox1'] = $_POST['vehicle'];
}
print_r($_SESSION); // remove this line after testing. It will show you the contents of the $_SESSION, which is handy for seeing when variables are set within it
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
<input type="checkbox" name="vehicle" value="Bike"<?php if($_SESSION['checkbox1'] == "Bike"){ echo " checked"}?>>I have a bike<br />
<input type="checkbox" name="vehicle" value="Car"<?php if($_SESSION['checkbox1'] == "Car"){ echo " checked"}?>>I have a car <br />#
<input type="submit" value="Go" />
</form>
</body>
</html>
You could add a unique id generated at the first page in a hidden input area that when posted could form part of the session variable name. From there the information would be indexed in the $_SESSION under that variable name making it easier to pull the data back out or to create a new form session as it were.
Hope I haven't lost you.

Php, passing data between pages without using the url?

I have a php page that has a form that asks for an e-mail. When you press the send button, it gets to another php page, which gets the form data and does its stuff. I need to then be able to go back to the old page (the one that contained the form) and give it some data so that it will be able to change itself and say "You've sent your e-mail successfully, and will not display the form.
How do I do it?
Sessions probably
http://us2.php.net/manual/en/book.session.php
You can either use sessions or cookies, to not depend on the URL cookies have always to be enabled.
Check the PHP Manual (Sessions and Cookies).
Options:
1) Set a cookie (or use a session variable, which is kind of the same thing)
2) Use a separate thank-you page. After you've processed the form, redirect to http://www.mysite.com/thankyou
3) Process the form on the same page as itself. If your form is at http://www.mysite.com/myform, then at the top of that page have a little
if ($_POST)
// process form
// display thank you
else
// display form
Good luck!
If the user is just seeing data that they've entered anyway, you can just use hidden form fields:
<input type="hidden" id="lang" name="lang" value="en" />
That way you can continue to POST new forms and pass the data down the lane. That's the easiest thing to do without having to write a single extra line of PHP code.
You could also store each section in a database and save each section as-added. That would give you the added benefit of having partial data in the case of a browser crash or whatever, depending on how many parts your form is. You could then pass just an ID to the DB table row and retrieve the data for display.

How to store the value?

I want to make the application like below.
First there is a page with four radio buttons. The user clicks on any of the radio buttons but doesn't save and he goes to another page. Then if he comes back to same page with the radio buttons it shows the selected radio button before he goes to another page.
How can I do it by using jQuery and PHP?
You can either save the value in a database, or in a cookie (with javascript, jquery: settings cookie with jquery
EDIT: As palasso said, you can also use php sessions instead of the cookie, all depends on your need.
For example, the user can change the cookies you created (they are just .txt files) and alter the information within.
In your situation, this dont seem to be a problem (becose the cookie depends on the checkboxes the user clicked).
So, you should go with database storage if:
Need to store the checks for long time;
Need to do complex elaboration on them;
Else, go with cookies/session.
First of all you have to start session in your php script. Then use ajax request on a radio button click storing the selected value in the session.
Sessions and cookies are the easier options. I prefer sessions.
Add a click event to the radio button
When clicked save the selected option into a php session
Each time the page is loaded check if the selected value exists in your session and load it.

How to select items from multiple pages using PHP, HTML, etc?

On a site that I'm working on, I'm displaying results from a database query. Each page shows 15 results, so if there are 45 results returned from the query, they will be displayed on 3 separate pages, each containing 15 results.
I want the user to be able to select any number of items from the first page, then switch to the other pages and select items from those pages as well, while still remembering the items that were selected on the first page. (Eventually, when the user has selected all the items, he will click a Submit button.) So if the user navigates back to the first page, the items that he checked should show up as being selected. On pages that I've seen on the Web, when the user navigates back to the first page, all the checkboxes are cleared again (which is not what I want).
Any ideas on the best way to achieve this? Thanks.
Remembering the selections in-between pages means you have some state that needs storing somewhere. There are a variety of ways to do it, but it boils down to one of these...
Keep it on the client (e.g. cookies)
Keep it on the server (e.g. in a database)
Keep sending it backwards and forwards (e.g. hidden form field, as in ASP.NET viewstate)
A common abstraction offered by platforms such as PHP and ASP.NET is the idea of "session". The actual implementation of session state might be some combination of those 3 possibilities above, and it probably offers the easiest route. I suggest you look at session state in PHP.
use hidden inputs to carry over the results from the previous set
<!-- hidden fields -->
<input type='hidden' name='whatever[]' value='whatever'/>
<!-- checkboxes -->
<input type='checkbox' name='whatever[]' value='whatever'/>
You could do many things.
Save as a JavaScript cookie
Save as a session by using AJAX on each click
Hide each page as a "tab" so they are actually there but just not shown.

Categories