[RESOLVED] How to remember checkbox through pagination
Results 1 to 10 of 10

Thread: [RESOLVED] How to remember checkbox through pagination

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    13

    resolved [RESOLVED] How to remember checkbox through pagination

    Help needed.I searched but failed to find solution.Here is my problem,

    I have page contains list of data from database and checkbox for each row in the list. Going through pages using pagination, have to remember checkbox till the page submits for process.

    On pagination there is no form , it just opens a new page.

    What i have done is created a script to store checked data to an array . I want to pass that array with the link(pagination).

    Here is the script
    Code:
    <script>
    function sel_check(file_id,chk_id)
       {
      	var lfckv = document.getElementById(chk_id).checked;
    		if(lfckv==true)
    		{
    			 myCheckbox_array.push(file_id);
      		}
    		else
    		{
    			for(var i=0; i<myCheckbox_array.length;i++ )
       			{ 
      				if(myCheckbox_array[i]==file_id)
      				myCheckbox_array.splice(i,1); 
      			} 
    		}
        }
    </script>
    and this is how pagination looks

    Code:
    <?php 
    $select_budget="SELECT bdg_id,name FROM tb_budget ";
    $pager = new pager($select_budget,'num',$dispaly_rows,"&page=&view=list&bdg_id=$bdg_id&",$self_url);
    $count=0;
    while(list($bdg_id,$name)=$db->fetch_array($pager->result))
    {
    $count++;
    <tr>
      <td><input type="checkbox" name="ckbx_[<?php echo $count;?>]" id="ckbx_grant<?php echo $count;?>"  onclick="sel_check(<?php echo $bdg_id;?>,'ckbx_<?php echo $count;?>')" /></td>	
      <td><?php echo $name; ?></td>
    </tr>
    }
    
    echo $pager->show();
    ?>
    <input type="submit" />

  2. #2
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Storing checked data in session need form submission right ? Right now I have an array of checked item which i want to send to another page.

  3. #3
    Syntax Error
    Join Date
    Feb 2003
    Location
    San Diego, CA
    Posts
    443
    Quote Originally Posted by mysamsung View Post
    Storing checked data in session need form submission right ?
    Yes, unless you are using AJAX.

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Pagination I am using , loads new page by clicking link (<< PREVIOUS NEXT >>); no form submission ,no Ajax.

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,353
    You also could avoid form submission if you store the data in cookies rather than in a PHP session.

    EDIT:
    Quote Originally Posted by mysamsung View Post
    Pagination I am using , loads new page by clicking link (<< PREVIOUS NEXT >>); no form submission ,no Ajax.
    Why does that matter? Neither of the two recent suggestions (AJAX and storing the data in cookies rather than sessions) depend upon or interfere with what you're doing for pagination.
    Last edited by bradgrafelman; 04-11-2012 at 12:28 AM. Reason: merged

  6. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Cookies , I am not allowed to use Cookies . Only two options are in my way ; either use session or attach array with the link to pass the value.

  7. #7
    Syntax Error
    Join Date
    Feb 2003
    Location
    San Diego, CA
    Posts
    443
    You want to save the value of a checkbox without submitting a form. The way I see it the only options are javascript or AJAX. You could use javascript to pass the value of the checkbox in the URL or you could use AJAX to store the value in the session.

  8. #8
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Please look my pagination code
    Code:
    <?php 
    class pager
    {
        var $sql;
        var $getvar;
        var $rows;
        var $start;
        var $getvar_val;
        var $pager;
        var $result;
        var $link_others;
        var $self_url;
        function __construct($sql,$getvar,$length,$link_to_url,$self_url)
        {
            $this->result=null;
            $this->sql = $sql;
            $this->getvar = $getvar;
            $this->rows = 0;
            $this->start = 0;
            $this->getvar_val = 1;
            $this->pager = null;
            $this->SetLength($length);
            $this->GetStart();
            $this->doQuery();
            $this->link_others=$link_to_url;
            $this->self_url=$self_url;
        }
    
        //Sets $length
      function SetLength($length)
      {
          $this->length = (int)$length;
          if($this->length<0)
          {
              $this->length = 0;
          }
      }
    
      function doQuery()
      {
          $sql = trim($this->sql);
          $sql = substr($sql,6);
          $sql = 'SELECT SQL_CALC_FOUND_ROWS '.$sql.' limit '.$this->start.', '.$this->length;
          $this->result = mysql_query($sql)or die(mysql_error());
          $sql = "SELECT FOUND_ROWS()";
          $result = mysql_query($sql);
          $this->rows = mysql_result($result,0);
      }
    
      //getvar_val() gets the
      //$getvar_val is a positive integer - > 0
      function Set_Getvar_val()
      {
          $this->getvar_val = (int)$_GET[$this->getvar];
          if($this->getvar_val<1)
          {
              $this->getvar_val = 1;
          }
      }
    
      //Gets the start of the data
      function GetStart()
      {
          $this->Set_Getvar_val();
          $this->start = (($this->getvar_val-1)*$this->length);
      }
    
        function show($next="Next",$last="Previous",$separator="")
        {
            $array = $this->pager2();
            $str = array();
            foreach($array as $k => $v)
            {
                if($k!='next'&&$k!='last')
                {
                    if($k==$this->getvar_val)
                    {
                        $str[] ="<li class='new_active'><a href='#' >".$k."</a></li>";
                    }
                    else
                    {
                        $str[] = '<li ><a href="'.$v.'">'.$k.'</a></li>';
                    }
                }
            }
            $str = implode($separator, $str);
            $rt = $array['last']===null?"":'<li class=\'next\'><a href="'.$array['last'].'">'.$last.'</a></li>'.$separator;
            $rt .= $str.$separator;
            $rt .= $array['next']===null?"":'<li class=\'next\'><a href="'.$array['next'].'">'.$next.'</a></li>';
            return $rt;
        }
    
         //Returns an array of the links arround the given point
        //['next'] => next page
        //['last'] => last page
        function pager2($margin=10)
        {
            $path = $_GET[$this->getvar];
            $newpath =$this->self_url."?";
           
            $newpath .= $this->getvar;
            $linkpaths = array();
            $current = $this->start / $this->length + 1;
            $pages = ceil(($this->rows/$this->length));
            $pagerstart = $current-$margin;
            $pagerstart = ($pagerstart<1)?1:$pagerstart;
            $pagerend = $current+$margin;
    
            $pagerend = ( $pagerend > $pages ) ? ( ceil( ( $this->rows / $this->length ) ) ): $pagerend;
    
            for($i=$pagerstart;$i < ($pagerend+1);$i++)
            {
                $linkpaths[$i] = $newpath."=".($i).$this->link_others;
            }
            if($linkpaths[($current+1)]!=null)
            {
                $linkpaths['next']=$linkpaths[($current+1)];
            }
            if($linkpaths[($current-1)]!=null)
            {
                $linkpaths['last']=$linkpaths[($current-1)];
            }
            return $linkpaths;
        }
    }
    ?>
    Attaching checkbox array to URL we need to use innerHTML right ? OR "how to attach javascript variable to PHP?!".

    This is the URL
    Code:
    $pager = new pager($select_budget,'num',$dispaly_rows,"&page=&view=list&bdg_id=$bdg_id&",$self_url);
    OR "How to use AJAX to store session using this pagination?!"

    Because the << PREVIOUS NEXT >> button created by the above pagination code.
    Do I need to change pagination code.

  9. #9
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Yes , I need to call AJAX on every click on checkbox; I was thinking about call AJAX on clicking (NEXT >>) button. Ok let me try this.

  10. #10
    Junior Member
    Join Date
    Apr 2010
    Posts
    13

    resolved Resolved

    I changed my code to this,
    AJAX REQUEST
    Code:
    function sel_check(bdg_id,chk_id)
       {
      	var lfckv = document.getElementById(chk_id).checked;
    		if(lfckv==true)
    		{
    			 var url = "MANAGEMENT/BUDGET/ajax.php";
    				var pars = "check_id="+bdg_id+"&type=add";
    
                    var myAjax = new Ajax.Request(
    			url,
    			{
    				method: 'get',
    				parameters: pars,
    				onComplete: showResponse_checksession
    			});
    		}
    		else
    		{
    			var url = "MANAGEMENT/BUDGET/ajax.php";
    			var pars = "check_id="+bdg_id+"&type=remove";
    				
                    var myAjax = new Ajax.Request(
    			url,
    			{
    				method: 'get',
    				parameters: pars,
    				onComplete: showResponse_checksession
    			});
    		}
        }
    and AJAX RESPONCE
    Code:
    session_start();
    	if (!isset($_SESSION['checked'])) {
    		$_SESSION['checked'] = array();
    	}
    	
    	if($_REQUEST[type]=='add')
    	{
    		$_SESSION['checked'][]=$_REQUEST[check_id];
    	}
    	
    	if($_REQUEST[type]=='remove')
    	{
    		$pos = array_search($_REQUEST[check_id], $_SESSION['checked']);
    		unset($_SESSION['checked'][$pos]);
    		$_SESSION['checked']= array_values($_SESSION['checked']);
    	}
    And now array is in session variable $_SESSION['checked']

    Thank you for responding to my request.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •