2014-12-27 29 views
0

我想让每个用户选择他感兴趣的复选框的市场领域[显示/隐藏] - 与JavaScript的div方法,但我不知道要保存每一个用户选择checkbox-div,我使用cookie或将其保存到数据库,我怎么能在PHP中做到这一点?PHP:保存值和Div到另一页

// function for show/hide divs depend on select input 
    <script> 
    function showMe (box) { 
     var chboxs = document.getElementsByName("c1"); 
     var div = "none"; 
      for(var i=0; i<chboxs.length; i++) { 
       if(chboxs[i].checked){ 
        div = "block"; 
        break; 
       } 
      } 
     document.getElementById(box).style.display = div; 
    } 
</script> 

//形式选择店内场

<form action="chooseShop.php" method=""> 
    <input type="checkbox" name="c1" value="Electrionics" Onclick="showMe('cssmenu_1')">Electrionics<br /> 
    <input type="checkbox" name="c1" value="Clothes" Onclick="showMe('cssmenu_2')">Clothes<br/> 
    <input type="checkbox" name="c1" value="perfums" Onclick="showMe('cssmenu_3')" >perfums<br/> 

    <button><a href="index.php">Next</a></button><br /><br /> 
</from> 

// THE Main DIVs 
<div id='cssmenu' > 

     <ul> 
      <li class='active'><a href='#'><span>Much Me</span></a></li> 
      <li class='last'><a href='#'><span>Profile </span></a></li> 


      <li class='has-sub' id="cssmenu_1" style="display:none" ><a href='#'><span>Electrionics</span></a> 
       <ul> 
       <li><a href='#'><span>Product 1</span></a></li> 
       <li><a href='#'><span>Product 2</span></a></li> 
       <li><a href='#'><span>Product 3</span></a></li> 
       <li><a href='#'><span>Product 4</span></a></li> 
       <li class='last'><a href='#'><span>Product 5</span></a></li> 
       </ul> 
      </li> 



      <li class='has-sub' id="cssmenu_2" style="display:none"><a href='#'><span>Clothes</span></a> 
       <ul> 
       <li><a href='#'><span>Product 1</span></a></li> 
       <li><a href='#'><span>Product 2</span></a></li> 
       <li><a href='#'><span>Product 3</span></a></li> 
       <li><a href='#'><span>Product 4</span></a></li> 
       <li class='last'><a href='#'><span>Product 5</span></a></li> 
       </ul> 
      </li> 


      <li class='has-sub' id="cssmenu_3" style="display:none"><a href='#'><span>perfums</span></a> 
       <ul> 
       <li><a href='#'><span>Product 1</span></a></li> 
       <li><a href='#'><span>Product 2</span></a></li> 
       <li><a href='#'><span>Product 3</span></a></li> 
       <li><a href='#'><span>Product 4</span></a></li> 
       <li class='last'><a href='#'><span>Product 5</span></a></li> 
       </ul> 
      </li> 

    </div> 

+0

你知道一点PHP atleast吗? –

+0

是的,我知道PHP和JavaScript,但我仍然新鲜 –

回答

0

这取决于你想要多久来存储信息。 PHP会话是最短的,它们只会持续用户打开标签的时间。

饼干的保质期要长得多,它们会存储在用户的浏览器中指定的天数,您可以设置。

数据库用于存储信息的长期使用。如果你希望你的用户能够永久保存他们的偏好,这是做到这一点的最佳方式。

0

如果这是用户体验,并且您不必将值存储在某个数据库中,我会使用本地存储。

这里是a working jsFiddle选择一个或两个盒子然后点击运行以便内部页面重新加载,你会看到你的选择在重新加载后被记住。

function showMe(box) { 
     //get and loop through all the checkboxes 
     var chboxs = document.getElementsByName("c1"); 
     for (var i = 0; i < chboxs.length; i++) { 

      if (chboxs[i].checked) { 
       // if the box is checked, show group save the selection to local storage 
       document.getElementById("cssmenu_"+i).style.display = "block"; 
       localStorage.setItem("hasGroup" + i, "yes"); 
      } 
      else{ 
       //otherwise set each group in not visible and save state to local storage 
       document.getElementById("cssmenu_"+i).style.display = "none"; 
       localStorage.setItem("hasGroup" + i , "no"); 
      } 
     } 
    } 


window.onload=function(){ 
    //check for saved groups and display them if found 
    var chboxs = document.getElementsByName("c1"); 
    for (var i = 0; i < chboxs.length; i++) { 
     var div = "none"; 
     if (localStorage.getItem("hasGroup" + i) == "yes") { 
      div = "block"; 
      //re-check the box 
      chboxs[i].checked = true; 
     } 
     document.getElementById('cssmenu_' + i).style.display = div; 
    } 
}; 
+0

如何使用PHP SESSIONS将DIV值保存到下一页? –

+0

Youu可以使用php会话,请记住php代码只会在页面加载时运行一次。因此,如果你想使用PHP,你必须编写一个单独的PHP页面来设置和读取会话变量。然后编写一个ajax调用,将复选框发送到该页面,以便它可以更新会话并让php页面返回会话值以设置html页面。如果需要,我可以帮助你。请记住,通过会话,当用户关闭浏览器时,信息全部丢失。本地存储依然存在。 – DelightedD0D

+0

那么完美的方法是什么呢?我觉得你谈论的方式对我来说会很难,如果你能帮助我,我会感到很开心。 所有我需要让用户选择复选框市场领域有兴趣,所以这个选中的框将成为用户字段按钮(如youtube左菜单)。 1-第一页选择字段 2-第二页是index.php [第二页(index.php)保存checkedbox-divs为每个用户登录] –

相关问题