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

<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

<?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" />

    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.

      mysamsung;11001293 wrote:

      Storing checked data in session need form submission right ?

      Yes, unless you are using AJAX.

        Pagination I am using , loads new page by clicking link (<< PREVIOUS NEXT >>); no form submission ,no Ajax.

          You also could avoid form submission if you store the data in cookies rather than in a PHP session.

          EDIT:

          mysamsung;11001368 wrote:

          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.

            Cookies , I am not allowed to use Cookies :o. Only two options are in my way ; either use session or attach array with the link to pass the value.

              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.

                Please look my pagination 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 🙂

                $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.

                  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.

                    I changed my code to this,
                    AJAX REQUEST

                    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

                    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.

                      Write a Reply...