无法使用Cookie使用Pagination php管理复选框

时间:2013-09-05 17:15:10

标签: php javascript cookies pagination

我想通过使用Cookie来管理分页。

我需要从选定的复选框中获取值并通过分页传递该值,这是由php / mysql完成的。如果在另一个页面上用户选择其他复选框,则将其值添加到先前选择的数组中。为此,我写了一个代码,但它没有用。我的代码有什么问题????

以下文件是manual.php文件。

              <script type="text/javascript" src="paginate.js"> </script>   

             <tr>
      <td><strong><a href="javascript:goSelect()">Select</a></strong></td>
              <td width="59"><strong>Sr No.</strong></td>  
      <td width="300"><strong>Question </strong></td>           
      <td width="30"><strong>CorrectAnswer </strong></td>
      <td width="50"><strong>Catagory </strong></td>
      <td><strong>View Details</strong></td>
    </tr>
                <?php
                    include("config.php");      
                    $start = 0;
                    $per_page = 10;
               //$tablename = "testlist";
               $targetpage = "manual.php?id=42";    
                    if(!isset($_GET['page'])){
                     $page = 1;
                     } else{
                      $page = $_GET['page'];
                     }
                      if($page<=1)
                      $start = 0;
                      else
                      $start = $page * $per_page - $per_page;
                       $sql="select * from math order by id";
                       $num_rows = mysql_num_rows(mysql_query($sql));
                       $num_pages = $num_rows / $per_page;
                        $sql .= " LIMIT $start, $per_page";
                        $result=mysql_query($sql) or die(mysql_error());
                        while($row=mysql_fetch_array($result))                  
                        { ?>

                <tr>

    <td><input name="<?php echo $row['id'];?>" type="checkbox" class="remember_cb"></td>
    <td><center><?php echo htmlspecialchars_decode($row['id'], ENT_QUOTES); ?></center>                       
             </td>      
     <td style="padding-left:25px;margin:15px"><?php echo $_SESSION['question']=  
                             htmlspecialchars_decode($row['question'], ENT_QUOTES); ?></center> 
                          </td>
    <td><center><?php echo htmlspecialchars_decode($row['correctAnswer'], ENT_QUOTES); ?>            
             </center></td>
    <td><center><?php echo htmlspecialchars_decode($row['category'], ENT_QUOTES); ?>
              </center></td>
    <td><a href="<?php echo "manual_update.php?id=".$row['id']."&mode=update";?                           
              >"id="dialog_10">View</a></td>

            </tr>

     <?php } ?>
    <tr>
    <td><strong><a href="javascript:goSelect()">Select</a> </strong></td>
    <td><strong>Sr No </strong></td>
    <td><strong>Question </strong></td>         
    <td><strong>CorrectAnswer </strong></td>
    <td><strong>Category </strong></td>
    <td><strong>View Details</strong></td>
    </tr>

                   <?php
                       if($page > 1){
                       $prev = $page - 1;
                       $prev = " <a href='$targetpage&page=$prev'>prev</a> ";
                       } else {
                        $prev = "";
                       }
                       if($page < $num_pages){
                       $next = $page + 1;
                       $next = " <a href='$targetpage&page=$next'>next</a> ";
                        }
                    else
                    {
                 $next = "";
                      }
                  echo $prev;
                          echo $next;
                        ?>

paginate.js

                           var aa_checkbox;

                           function init_checkbox(){
                             //setup blank cb cookie
                              if(!Cookie.read('checkbox'))
                             {
                               Cookie.write('checkbox', JSON.encode({}));
                            }

                             //setup "associative array" to match what is currently in the cookie
                             aa_checkbox = JSON.decode(Cookie.read('checkbox'));


                              //set up each checkbox with class="remember_cb"
                             $$('input.remember_cb').each(function(el){

                            //mark checked if it is in the cookie
                             if(aa_checkbox[el.name]){
                             el.checked = 'checked'
                              }

                             //setup onclick event to put checkbox status in the 
                             if(el.checked){
                            aa_checkbox[el.name] = 1;
                               }else{
                             delete(aa_checkbox[el.name]);
                              }   
                            })
                            })

                    //save aa_checkbox back into cookie upon leaving a page
                       window.onbeforeunload = function(){Cookie.write('cb',
                                                       JSON.encode(aa_checkbox));};

                             setup_form();

                            return true;
                          }

                       function setup_form(){
                          //set up form so that it adds the inputs upon submit.

                  $$('form.remember_cb_form').each(function(form){
                   form.addEvent('submit', function(ev){

                   //clean up previously inserted inputs
                   var aa_hidden_insert = $$('input.hidden_insert');
                    $each(aa_hidden_insert, function(el){ 
                   el.parentNode.removeChild(el);
                          })

                          var el_form = this;

                //insert hidden elements representing the values stored in aa_checkbox
                $each(aa_checkbox, function(i_value, s_name){
                      if(i_value){ 
                 var el_input = document.createElement('input');
                   el_input.type = 'hidden';
                   el_input.value = i_value;
                   el_input.name = s_name;
                   el_input.setAttribute('class', 'hidden_insert');
                     el_form.appendChild(el_input);
                   }
                      });
                     });
                    });
                 }

               window.addEvent('domready', init_checkbox);

              function selectall()
                {
    var selectAll = document.forms[0].length;

    if(document.forms[0].topcheckbox.checked  == true)
        {
            for(i=1;i<selectAll;i++) 
            {
            document.forms[0].elements[i].checked  = true;
            }
        }
    else
        {
            for(i=1;i<selectAll;i++)

            document.forms[0].elements[i].checked = false;
        }
                     } 

              function goSelect()
               {
         var select =  document.forms[0].length;
            var checkboxes=""
             for(i=1;i<select;i++)
            {
               if(document.forms[0].elements[i].checked==true)
              checkboxes+= " " + document.forms[0].elements[i].name

             }

            if(checkboxes.length>0)
             {
               var con=confirm("You have selected ...........");         
            if(con)
             {

                   window.location.assign("submit.php?recsno="+checkboxes)

    }
}
else
{
    alert("No record is selected.")
}
         }

0 个答案:

没有答案