【问题标题】:jQuery to dynamically toggle between layoutsjQuery在布局之间动态切换
【发布时间】:2012-08-13 08:08:08
【问题描述】:

我希望有人可以在这里为我指明正确的方向。我有一个简单的媒体页面,在 MySql 数据库中有三个布局 '1', '2' and '3'。当我加载页面时,我会检查数据库中设置的布局并使用下面的代码来显示正确的代码块 - 这非常有效。现在,使用 jQuery,我希望能够有 3 个图形按钮在 3 个布局之间动态切换。我想要实现的是:

  1. 默认情况下,布局 1 已设置,图标 1 设置为翻转“开启”状态。
  2. 单击图标二将动态设置 $album['layout'] 为“2”并更新页面,将 DB 从 1 更新为 2,并将图标一更改为翻转“关闭”状态并更改图标二翻转“开启”状态。
  3. 下次用户访问页面时,布局 2 将被设置,图标 2 将处于翻转“开启”状态。

我对 PHP 比较陌生,我刚刚开始了解 jQuery - 但我了解所有基本概念 - 我只是想不出该怎么做,而且我似乎找不到任何东西在线为我指明了实现这一目标的正确方向。任何见解将不胜感激。


php 中用于显示正确代码块的代码

<?php

    if ($album['layout'] == 1) {

        //Display Album Layout 1

    } else if ($album['layout'] == 2) {

        //Display Album Layout 2

    } else if ($album['layout'] == 3) {

        //Display Album Layout 3
    }

?>

【问题讨论】:

    标签: php jquery ajax layout toggle


    【解决方案1】:

    似乎 Ajax 是您的问题的解决方案。

    使用 jQuery,您可以轻松地更新页面。您甚至可以切换整个样式表,请参阅this question 了解更多信息

    使用 Ajax,您可以向服务器发送调用以更新存储在数据库中的值,而无需刷新页面。有关让 JavaScript 与 PHP 对话的更多信息,请参阅 this question


    例子:

    显示的页面我们称之为index.php

    <?php require_once("ajax.php"); //include your functions ?>
    
    <html>
      <head>
        <title>Toggle</title>
        <!-- include jQuery -->
        <script 
          src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
          type="text/javascript"></script>
        <!-- include your javascript -->
        <script src="js/functions.js" type="text/javascript"></script>
      </head>
      <body>
    
        <!-- This div will hold the current album layout -->
        <div id="albumLayoutDiv">
          <?php
            // Gets the album specified in $album['layout'] so it will
            // display on page load. This is defined in ajax.php
            echo GetAlbumLayout($album['layout']);
          ?>  
        </div>
    
        <input id="layout1" type="button" value="layout 1" /> 
        <input id="layout2" type="button" value="layout 2" /> 
        <input id="layout3" type="button" value="layout 3" /> 
    
      </body>
    </html>
    

    如您所见,GetAlbumLayout 未在此处定义,我已将其移至名为 ajax.php 的外部文件:

    <?php
    
    function GetAlbumLayout($layout) {
      if ($layout == 1) {
    
        // UPDATE THE DATABASE TO LAYOUT 1
        return $htmlForLayout1; // Display Album Layout 1
    
      } else if ($layout == 2) {
    
        // UPDATE THE DATABASE TO LAYOUT 2
        return $htmlForLayout2; // Display Album Layout 2
    
      } else if ($layout == 3) {
    
        // UPDATE THE DATABASE TO LAYOUT 3
        return $htmlForLayout3; // Display Album Layout 3
    
      }
    }
    
    // Here is where we look at the values that were passed though
    // Ajax. Remember the we used POST, and set the values in 'data'?
    // You can see all of that here. You get the values by using 
    // $_POST['key'] = value. In this case I am using "action" to 
    // determine what PHP function we want to call.
    
    // If 'action' is set from the Ajax call
    if(isset($_POST['action']) && !empty($_POST['action'])) {
    
      $action = $_POST['action'];
    
      switch($action) {
        // We set action to 'changeLayout' in the Ajax call
        case 'changeLayout': 
    
          // likewise, we set "layoutNum" to be the number that the
          // user clicked on. We access this value in the same was as
          // the action
          GetAlbumLayout($_POST['layoutNum']);
          break;
    
        /* 
        case 'anotherAction' : 
          someOtherFunction();
          break; 
        */
    
        // ...etc... Add more here if you want to perform different
        // ajax calls down the road
      }
    }
    
    ?>
    

    现在终于是 Ajax 调用和将其结合在一起的 Javascript functions.js

    // This function fetches the layout number that we want from
    // php and updates the "albumLayout" div on the page when
    // successful.
    function changeLayout(layoutNumber) {
    
      // Start the Ajax call
      $.ajax({ 
        // set the url to your ajax.php file. This is what this
        // Ajax call will POST to.
        url: '/php/ajax.php', 
        type: 'post',
        // the data can be thought of as the paramaters that you can
        // use on the PHP side of things. Think of it as a dictionary
        // or a map of values. You can pass in anything here that you
        // need in PHP to call a function
        data: {
          action: 'changeLayout', // what we want to do
          layoutNum: layoutNumber    // the layout that was requested
        },
        // After we get the results back from PHP, it is stored as a 
        // string inside of output. Ajax is async - this won't happen 
        // until the results are received from PHP. In this case, I am
        // updating the albumLayout div with the output gathered from
        // the PHP function `GetAlbumLayout(layoutNumber)`
        success: function(output) {
          $('#albumLayout').html(output);
        }
      });
    }
    
    /* document ready waits till the DOM is fully loaded to do anything */
    $(document).ready(function() {
    
      // When the user clicks button 1
      $('#layout1').click(function() {
        changeLayout(1);
      });
    
      // When the user clicks button 2
      $('#layout2').click(function() {
        changeLayout(2);
      });
    
      // When the user clicks button 3
      $('#layout3').click(function() {
        changeLayout(3);
      });
    
    });
    

    我没有测试提供的任何代码,但它应该能让你朝着正确的方向前进。基本上,您最初使用数据库中的值加载页面。用户将单击页面上的按钮来更改布局。您对服务器进行 Ajax 调用以 UPDATE 数据库的默认值,并返回要在页面上显示的新 HTML。成功后,您将页面上的 HTML 更新为从 PHP 收集的新 HTML。

    祝你好运!如果我误解了您的问题,请告诉我。

    【讨论】:

    • 嘿特雷弗-感谢您的链接-我似乎无法将该信息与我的问题联系起来。我必须在单击按钮时在我的 PHP IF 语句之间交换/切换代码块。所有代码在第一次加载时都存在于页面上,我只需要在它们之间切换,然后将最后一个布局“状态”保存为新的默认值,这样用户下次访问该页面时就会显示。非常感谢任何帮助。
    • Chris 解决了您使用 jQuery 在网站上切换它的问题,但问题仍然是如何从 PHP 获取结果并更新页面并将最后的布局状态设置为默认值。我会更新我的答案,以便更清楚,但在您提供更多代码之前它将保持一般性。
    • 我在代码中提供了一个包含 cmets 的完整示例,应该可以帮助您进一步了解发生了什么。
    • Trevor - 非常感谢您花时间整理这些内容。我正在检查您的所有代码并尝试将它们放在一起。我有一个简单的问题...您在 ajax.php 文件中有“return $htmlForLayout1”...我是否在同一个 ajax.php 文件中创建一个包含我所有布局 1 html/php 的 $htmlForLayout1 变量...或者用我的html/php代码替换“$htmlForLayout1”?
    • 这些只是临时变量,举个例子——你可以用你想要在 Ajax 调用之后显示在页面上的 HTML 替换它们。如果您愿意,可以将require_onceinclude 其他PHP 文件放入ajax.php 以使用它们的功能。
    【解决方案2】:

    听起来像是典型的前端工作。让你成为一个小小提琴,因为这个问题有点过于宽泛,无法直接回答。

    http://jsfiddle.net/mNbLa/1/

    【讨论】:

    • 嘿 Chris - 感谢 jsFiddle 之类的,我很感激。这解决了一个问题......但是我将如何保存该按钮状态,以便下次用户访问该页面时,会显示相同的布局和按钮状态。此外,当单击按钮时,我需要更改 PHP IF 语句之间的代码块。那是我更大的问题。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多