刷新DIV标记的内容,其中包括PHP函数调用

时间:2013-01-02 18:56:39

标签: php jquery html reload

问题的核心我试图使用HTML下拉菜单的值来调用php函数而不重新加载整个页面。鉴于PHP是一种服务器端语言,我已经整合了JQuery来处理onChange事件,希望只重新加载页面的一部分。我想要调用的PHP函数(即wp_cart-button_for_product)打印出一个“添加到购物车”按钮,但与“添加到购物车”按钮关联的变量最终取决于下拉菜单。我想要重新加载的div标签(即按钮重新加载)位于当前页面上,但URL会根据产品而变化。我已经尝试了load和html方法,但没有运气。

<script type = "text/javascript">
  function productchange() 
  {
      var currentrow = $('#productcategory1').val();
      $("#jsvar").val(currentrow);
      $.ajax({
        type: "GET",
        url: "http://www.example.com/wp-content/themes/themeX/storevar.php",
        data: {'jsvar': currentrow},
        complete: function(){
              $('#button-reload').load("http://www.example.com/wp-content/themes/themeX/printbutton.php #button-reload > *");
      //      $('#button-reload').html($('#button-reload',data).html);
      //      $('#button-reload').load(location.href "#button-reload > *");
            }});      
      return false;
  }
  </script>

<div id = "button-reload">
      <?php echo print_wp_cart_button_for_product($products[$_SESSION['currentrow']]["Product"], $products[$_SESSION['currentrow']]["Price"]); ?>
  </div>

  <form method="get" action="http://www.example.com/wp-content/themes/themeX/storevar.php">
      <input type="hidden" name="jsvar" id="jsvar" value="" />
  </form>

Storevar.php

<?php
    session_start();
    $currentrow = $_GET['jsvar'];
    echo "Current Row = $currentrow";
    $_SESSION['currentrow'] = $currentrow;
    echo "Session Currentrow =".$_SESSION['currentrow'];
?>

Printbutton.php

<div id="button-reload">
    <?php echo print_wp_cart_button_for_product($products[$_SESSION['currentrow']]["Product"], $products[$_SESSION['currentrow']]["Price"]); ?>
</div>

2 个答案:

答案 0 :(得分:0)

function productchange() 
{
var currentrow = $('#productcategory1').val();
$('#button-reload').load("http://www.example.com/wp-content/themes/themeX/printbutton.php", {jsvar: currentrow});
}

应该足够了。

您正在执行ajax请求,然后在成功回调中调用load。检查jquery文档http://api.jquery.com/load/

编辑您缺少“session_start();”在你的printbutton.php中。您还可以在其中添加存储代码,这样您只需要进行一次ajax调用。

Printbutton.php

<?php
    session_start();
    $currentrow = $_GET['jsvar'];
    echo "Current Row = $currentrow";
    $_SESSION['currentrow'] = $currentrow;
?>
<div id="button-reload">
    <?php echo print_wp_cart_button_for_product($products[$_SESSION['currentrow']]["Product"], $products[$_SESSION['currentrow']]["Price"]); ?>
</div>

答案 1 :(得分:0)

经过几个小时的实验:

 <script type = "text/javascript">
 function productchange() 
  {
      var currentrow = $('#productcategory1').val();
      alert(currentrow);
      $("#jsvar").val(currentrow);
      $.ajax({
         type: "GET",
         url: "http://example.com/wp-content/themes/themeX/storevar.php",
         data: {'jsvar': currentrow},
         success: function(response){
              alert(response);
              var pathname = window.location.href;
              alert(pathname);
              $('#button-reload').load(pathname + " #button-reload", function(responseTxt, statusTxt, xhr){
              if(statusTxt=="success")
                alert("Content loaded successfully!");
                if(statusTxt=="error")
                  alert("Error: "+xhr.status+": "+xhr.statusTxt);
                  });
  return false;
  }
  </script>