开发者

Using javascript and $_SESSION's to record wanted user information over pagination

开发者 https://www.devze.com 2023-01-31 04:58 出处:网络
What I have so far is a paginated page, showing all users in a database system. Each page shows 30 users and has a checkbox next to each user, what I need is a way for users to select and deselect the

What I have so far is a paginated page, showing all users in a database system. Each page shows 30 users and has a checkbox next to each user, what I need is a way for users to select and deselect these users and for these selections to propagate through, so if the user goes back to page 1 from page 2, all users from page 1 will still开发者_高级运维 be checked.

I also need a way to record this information, so that once the user has looked at all the pages and clicks a submit form all checked users information can be processed. I am thinking of using javascript to record the information and php sessions to store it, but with the way I am trying now, when a user clicks a checkbox, it is not ticked.

Does anyone have a better way of doing this/see how I can fix this problem?

Thanks.

<script type="text/javascript">
function log_export($str) {   
document.check.data.value = $str;
document.check.submit();
}
</script>

<?php
if(isset($_POST['data'])) {
    echo $_POST['data'];
}
?>

<form name="check" method = "post" action = "">
<input type="hidden" name="data">
    <input type="checkbox" name="A" onclick="log_export('1')" />
    <input type="checkbox" name="B" onclick="log_export('2')" />
    <input type="checkbox" name="C" onclick="log_export('3')" />
</form>


Few ways you could do this, but I'd avoid using javascript to do it. You could use an array in your $_SESSION to keep the list across pages.

<form name="check" method = "post" action = "">
    <input type="checkbox" name="person[A]" />
</form>

(note: The form elements are named person[A], person[B]...etc, so they can be accessed as an array in php and make your life easier.)

Then in the php you can store this in the session...

$_SESSION['saved_list'] = $_REQUEST['person'];

This way the session variable saved_list will contain the array person with all the checked boxes in it. You'll need to be careful not to overwrite the array each time however, so adding...

$_REQUEST['person'] = array_merge($_SESSION['saved_list'], $_REQUEST['person']);

...before this should keep them (if I'm remembering my merge functions correctly).

Alternatively, you could just use html to save the checkboxes already ticked. When page 2 receives the results from page 1, it could print them out as hidden elements at the end of the page 2 form. This way they can exist across pages, however this could become a bit unwieldy with 30 names a page.

I'd suggest storing it in a php session array, this shouldnt really involve using Javascript, it just over-complicates matters.


Why you don't change your form method to GET and make the pagination link so it contains every parameters passed to the form and the page number. I think it make everything more simple to handle that case with parameters passed on the URL against posted one.

you can create those links like this:

for ($i =0; $i < $max_page; $i++){
 echo "{$_SERVER['REQUEST_URI']}?$_SERVER['QUERY_STRING']&p={$i}";

and you just have to change your backend to use $_GET instead of $_POST.

0

精彩评论

暂无评论...
验证码 换一张
取 消