views:

1125

answers:

5

Hello.

I need to get values from selected checkboxe's and pass that values through pagination witch is done by php/mysql. And if on another page user select other checkboxes add their values to array of earlier seleced.

I need this for product compare page. In short i get checkbox values store them add checkbox values from other pages in pagination and when user selects compare send that array to compare page.

Anybody know how to do this? Or any related examples would be appriciated?

A: 

There are different ways to maintain state across pages, including cookies, session variables, hidden inputs, passing them in the querystring, persisting to database, etc. In this case I would probably use a session variable.

For more info on PHP session see PHP Sessions.

RedFilter
Yes i know that. But i know how to get values from checkboxwith js or ajax in my case. But problem is how to save that array i get to the session var and add other values from pagination. All done by the one page.
A: 
simon622
-1 for using JavaScript dependent form submission methods which degrade to a link to the top of the page when a perfectly good input type="submit" exists (and can be styled)
David Dorward
That makes no sense whatsoever. Its perfectly acceptable to use javascript to perform a form submission. Since a submit button cannot be styled to look like a link.. unless im missing something..
simon622
Yes but that button should be clicked only when user selects all the checkboxes he want to show in comparison view.
exactly, which is why using javascript to post the selection to the server makes complete sense.. and therefore did not deserve a -1!!!
simon622
Yes that's true..
A: 

One solution would be to load all of the pages at once in their own divs inside of a form. When you click on a new page link, hide all divs except the one you need to show. That way all checkboxes are submitted in the same form, making it easier to handle server-side. That of course depends on how heavy each individual page is and how many pages there are.

Another solution would be to keep a session variable, tracking what was clicked. Each time someone clicks to go to another page. POST to the server a list of the checkboxes.

geowa4
A: 

You could do it by using javascript to store the checkbox selection in a cookie. Here's some sample code to get you going in the right direction.

var aa_checkbox;

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

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


  //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 
    el.addEvent('click', function(){
      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);

There is a working demo here and a more thorough explanation here

james.c.funk
A: 

Unfortunately you don't have any real obstacle you cannot resolve. Actually you do not want to think about your very simple task or to read any manual. You just want that somebody gives you a full working solution for your complex problem.

I guess it is not good. This resource is not for lazy people, but for sharing of experience and knowledge of some tricks and technologies.

Alexander Elgin