【问题标题】:Passing PHP variable outside PHP OOP class (from separate PHP file) onto node value of a HTML <span> tag in a JQuery function将 PHP OOP 类之外的 PHP 变量(来自单独的 PHP 文件)传递到 JQuery 函数中 HTML <span> 标记的节点值上
【发布时间】:2011-10-01 22:31:23
【问题描述】:

可以将 PHP 变量中的引用传递给 JQuery 函数,该变量取自实例化对象类 (PHP) 的范围内,以便它呈现为数字或字符串以显示添加的项目数?我这里有一个类似这样的代码:

<!--in cart.php-->
$(document).ready(function() {
    $('div#cart').replaceWith('<div id=\'cart2\'><span><?php echo ('.**$itemcount**.'); ?> </span> <a href=\'viewcart.html\' class=\'view-cart\'>View Cart</a><a class=\'checkout\' href=\'checkout.html\'>Checkout</a></div>');
});  

$itemcount 是一个存储在 PHP 外部文件 store.php 中的变量

非常感谢您的帮助!

【问题讨论】:

    标签: php jquery reference scope


    【解决方案1】:

    至少可以说一个有趣的问题......

    假设 store.php 看起来像这样:

    <?php
    class store {
        private $_itemcount = 0;
        public function getItemCount(){
    
             return $this->_itemcount;
    
        }
    }
    ?>
    

    从那里我们在 cart.php 中像这样:

    <?php
        $store = new store();
        $itemcount = $store->getItemCount();
    
        echo <<<html
        $(document).ready(function() {
            $('div#cart').replaceWith("<div id='cart2'><span>$itemcount</span><a href='viewcart.html' class='view-cart'>View Cart</a><a class='checkout' href='checkout.html'>Checkout</a></div>");
        });
    html;
    ?>
    

    这样,我们可以返回值并在javascript中使用它。

    【讨论】:

      【解决方案2】:

      store.php

      <?php $itemcount = 10; ?>
      

      在购物车.php 中

      $(document).ready(function() {
          $('div#cart').replaceWith('<div id=\'cart2\'><span><?php require 'store.php'; echo $itemcount; ?> </span> <a href=\'viewcart.html\' class=\'view-cart\'>View Cart</a><a class=\'checkout\' href=\'checkout.html\'>Checkout</a></div>');
      });
      

      【讨论】:

        【解决方案3】:

        这里有许多适用于字符串或数字的答案,但如果您的要求变得更加复杂,您就会遇到问题。

        如果需要,您可以将 PHP 对象序列化为 JSON,这将允许您创建 JavaScript / JQuery 代码可以直接使用的 JavaScript 对象。

        假设您的 cart.php 脚本可以访问 $phpObject 所代表的 store.php 中的对象:

        例如

        ...
        $(document).ready(function() {
           var jsPhpObject = <?php echo json_encode($phpObject); ?>; // doesn't need to be inside document.ready if required elsewhere
           $('div#cart').replaceWith("<div id='cart2'><span>" + jsPhpObject.property + "</span><a href='viewcart.html' class='view-cart'>View Cart</a><a class='checkout' href='checkout.html'>Checkout</a></div>");
        });
        ...
        

        【讨论】:

          【解决方案4】:

          store.php

          <?PHP
          class SomeClass
          {
              public $insideMemberNum;
          
              public function __construct()
              {
                  global $outsideNum;
                  $insideNum = 2;
                  $this->insideMemberNum = 2;
          
                  // 1. $outsideNum = $insideNum; (this will make $outsideNum = 2, even outside this class)
                  // 2. $outsideNum = &$insideNum; (this will make $outsideNum = 2, but only wihtin this scope, after it will go back to 1)
          
                  // so youre options are...
                  //  1. pass by value
                  //  2. create a public variable and store the number in there
                  //  3. create a private variable and a public function to return the value of that variable like someone else suggested
              }
          }
          ?>
          

          incart.php

          <?PHP
          
          
          $outsideNum = 1;
          echo $outsideNum . "<br />";  // prints out 1
          
          include("store.php"); // include the external file
          $obj = new SomeClass(); // instansiate the object
          
          
          echo $outsideNum . "<br />"; // option 1 prints out 2, option 2 prints out 1
          echo $obj->insideMemberNum . "<br />"; // prints out 2
          
          ?>
          

          【讨论】:

            猜你喜欢
            • 2011-02-09
            • 2022-06-23
            • 2013-12-11
            • 1970-01-01
            • 2019-07-09
            • 2018-09-05
            • 2019-09-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多