944,015 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3337
  • PHP RSS
Aug 7th, 2007
0

Keeping track of the checkboxes selected acrros the page

Expand Post »
I have a list of users(signed for a newsletter).I'm retrieving the list from a database when the admin clicks on a button.
There are maximum 10 users diplayed per page.For example,if I had 30 users in the database,I would have 3 pages of users(Page 1,2,3);
For each listed user ,I have a checkbox on the right side.The checkbox is for when the admin wants to selected a few people to whom send the newsletter.
Ex:
Page: 1|2|3
1.ana checkbox
2.john checkbox
3.mike checkbox
---------------------
10.cindy checkbox
Then,if the admin want to go to another page :
Page : 1|2|3
1.lucy checkbox
2.shinoda checkbox
-------------------------
10.michelle checkbox
and so on
Down this list of users , the admin has 2 button : Send and Sendall
The Send button is used when he wants to send the newsletter only to the people with the checkbox selected

My problem is when the admin wants to select people from different pages(Ex: he selects 2 people from the first page,3 on the second ,1 person on the last page , and then he clicks send)
Because when he clicks the button for another page,the data is lost,so it's hard to keep evidence of the total checkboxes selected
Here is what I got so far :
PHP Syntax (Toggle Plain Text)
  1. function check(){
  2. var i;
  3. var total;
  4. var mail='';
  5. for(var i=0; i < document.form1.mail.length; i++)
  6. {
  7. if(document.form1.mail[i].checked)
  8. {
  9. mail+=document.form1.mail[i].value+',';
  10. }
  11. } window.location='admin.php?page=<?=$k;?>&action=mail&arr='+mail;
  12. }
This is my javascript function I use on the Page buttons
Notice The window.location on the end.This is how the admin browse through the list.The arr variable contains a string with the id of each selected user

PHP Syntax (Toggle Plain Text)
  1. <input type="button" value="<?=$k;?>" onclick="check();">
This are the page buttons.Of course there is a for there somewhere,but I only want to include the important part
PHP Syntax (Toggle Plain Text)
  1. $_SESSION['sel'].=$_GET['arr'];
When the user clicks the button , the page reloads and to keep the mail string while the admin browse trough the pages, I store it in a session(I don't know if this is the best solution)
PHP Syntax (Toggle Plain Text)
  1. <form action="mail.php" method="post" name="form1">
This is for the 2 send buttons(Send and Sendall).The form goes to a different page where it sends mail to the selected users
PHP Syntax (Toggle Plain Text)
  1. while($row=mysql_fetch_array($result))
  2. $v=explode(',',$_SESSION['sel']);
  3. print_r($v);
  4. $bool=0;
  5. for ($i=0;$i<strlen($v);$i++){
  6. if ($v[$i]==$row['id'])
  7. {
  8. $bool=1;
  9. }
  10. <input type="checkbox" name="mail" value="<?=$row['id'];?>" <?php if ($bool==1) echo 'checked'; ?> />
Oki,here I'm taking the string containing all the id's of the selected checkboxex and store it in an array,so I have the checkbox selected when the user comes from page 2 to page 1
I do this with a simple if statement.
This are the checkboxes.Each has the value , the user id in the database;

In the mail page,I'm also using the explode function to the $SESSION['sel'] , and on the array resulted from this ,I'm running the array_unique() function(because everytime the user click on a page,the previous selected checkboxes are again inserted into the $SESSION['sel']

This is a very poor programming solution to solve the problem(I'm aware of that) , so I'm asking if u ladies/gentlemens know a better solution(if you been trough this kind of problem and found a good way of doing it )
Similar Threads
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Autoresponder when submit form
Next Thread in PHP Forum Timeline: phpinfo page has array errors. What for?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC