【问题标题】:passing checkbox value to show / hide divs and store in database传递复选框值以显示/隐藏 div 并存储在数据库中
【发布时间】:2011-07-31 06:18:41
【问题描述】:

我正在使用 jQuery 1.6.1。这是主要代码:

<div>Sidebar (on/off): <input id="sidebar" name="Sidebar" type="checkbox" value="value1"/></div>

<?php if ($row_rsPage['fullcontent']==1)
 { ?>
        <div id="site_layout_full">
         <?php
      $oFCKeditor = new FCKeditor('FCKeditor1');
      $oFCKeditor -> BasePath = 'fckeditor/';
      $oFCKeditor -> Height = '455';
      $oFCKeditor -> Value = $row_rsPage['pg_cont'];
      $oFCKeditor -> Create();
      ?>
        </div>
    <?php } else { ?>
        <div id="site_layout_content">
        <?php
      $oFCKeditor = new FCKeditor('FCKeditor1');
      $oFCKeditor -> BasePath = 'fckeditor/';
      $oFCKeditor -> Height = '455';
      $oFCKeditor -> Value = $row_rsPage['pg_cont'];
      $oFCKeditor -> Create();
      ?>
        </div>
        <div id="site_layout_sidebar">
         <?php
      $oFCKeditor2 = new FCKeditor('FCKeditor2');
      $oFCKeditor2 -> BasePath = 'fckeditor/';
      $oFCKeditor2 -> Config['CustomConfigurationsPath'] = '../sidefckconfig.js' ;
      $oFCKeditor2 -> ToolbarSet = 'Short';
      $oFCKeditor2 -> Height = '455';
      $oFCKeditor2 -> Value = $row_rsPage['pg_sidecont'];
      $oFCKeditor2 -> Create();
      ?>
        </div>

$row_rsPage['fullcontent']==1 仅显示 1 个 div (site_layout_full),如果不是,则显示 2 个 div(site_layout_contentsite_layout_sidebar)。

现在我想做的是在不提交页面并将值存储到数据库中的情况下切换它。

所以我使用侧边栏来切换状态,但是我如何将这个值输入 MySQL。

最初这在没有 AJAX 的情况下工作,我提交表单然后将其存储在数据库中。但我希望它是交互式的,这样您就不必提交整个页面来更改布局。

我正在使用它来切换(但不起作用):

if (document.getElementById('sidebar').checked) {

      // display fullcontent
      document.getElementById('site_layout_sidebar').style.display = 'none';
      document.getElementById('site_layout_content').style.display = 'none';
      document.getElementById('site_layout_full').style.display = 'block';

  }

  else {

      // display site_content, site_sidebar
      document.getElementById('site_layout_full').style.display = 'none';
      document.getElementById('site_layout_content').style.display = 'block';
      document.getElementById('site_layout_sidebar').style.display = 'block';

  }

我如何将此值(复选框的状态)存储到$row_rsPage['fullcontent']

  • 复选框已选中:$fullcontent0
  • 复选框未选中:$fullcontent1

谁能帮忙解决这个问题?

我改变了这个: 在 javascript 周围添加了 $(document).ready(function() { }。它会触发事件,但我隐藏了 div 标签 site_layout_full 因为数据库中的每个条目都将 fullcontent 设置为 0

所以我实际上隐藏了 div 标签,无法再次显示。

我必须使用 javascript 检查数据库,然后切换,但如何?

所以我可以这样做:

document.getElementById('site_layout_full').style.display = 'none';
document.getElementById('site_layout_content').style.display = 'block';
document.getElementById('site_layout_sidebar').style.display = 'block';

并摆脱if ($row_rsPage['fullcontent']==1)

总而言之,我必须:

  • 检查数据库的全部内容(不是使用 php,而是使用 javascript)
  • 使用该值显示隐藏 div 标签 (javascript)
  • 动态显示/隐藏标签,当用户完成后,我将值提交到数据库(使用 php)

请问如何?

【问题讨论】:

    标签: php javascript ajax checkbox


    【解决方案1】:

    这是部分答案。有问题,但我还没有找到:

    Javascript/ajax 代码:

    $(document).ready(function() {
      $pageid = document.getElementById('pg_id').value;  
      $.post("senddata.php",{ id: $pageid },function (ContentFull) {
          alert("DATA = " + ContentFull);
          if (ContentFull==0) // 1 = Fullcontent - 0 = sidebar
          {
              $('input[name=Sidebar]').attr('checked', true);
          } else {
              $('input[name=Sidebar]').attr('checked', false);
          }
      });
    
    
       $('#sidebar').change(function(){
            $('#site_layout_sidebar').toggle();
            $('#site_layout_content').toggle();
            $('#site_layout_full').toggle();
        }); 
    
     if (document.getElementById('sidebar').checked) {
              // display fullcontent
              document.getElementById('site_layout_full').style.display = 'none';
              document.getElementById('site_layout_content').style.display = 'block';
              document.getElementById('site_layout_sidebar').style.display = 'block';
          }
          else {
              // display site_content, site_sidebar
              document.getElementById('site_layout_full').style.display = 'block';
              document.getElementById('site_layout_content').style.display = 'none';
              document.getElementById('site_layout_sidebar').style.display = 'none';
               }
          });
    

    这是 senddata.php :

        $pid = $_POST['id'];
    
        mysql_select_db($database_conndb, $conndb);
        $query_Recordset1 = "SELECT tbl_pages.pg_id, tbl_pages.fullcontent FROM tbl_pages WHERE tbl_pages.pg_id='$pid'";
    $Recordset1 = mysql_query($query_Recordset1, $conndb) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);
    
    $ContentFull = $row_Recordset1['fullcontent'];
    
    echo($ContentFull);
    
    mysql_free_result($Recordset1);
    

    现在我从请求中获得了正确的数据,但我认为某处存在错误...... 即使警报数据给我 0,我似乎总是得到完整的内容布局

    我做错了什么...

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      相关资源
      最近更新 更多