duanpanbo9476 2016-08-04 14:05
浏览 62
已采纳

如何将当前符号表中的数据放入ajax响应?

I'm getting data via AJAX. When I get response, I want to put variables from current symbol table (which I get from extract($this->args)) to ajax-response. The ajax.php is something like:

<div class="modal fade <?php echo $class; ?>" id="<?php echo $id; ?>" tabindex="-1" role="dialog" aria-hidden="true"></div>

So the variables in ajax.php should be replaced by variables from current symbol table. This is my code:

private function generate_js( $call_selector, $modal_id ) {
    ob_start();
            extract($this->args);             ?>        
    $("<?php echo $call_selector; ?>").click(function(e){ 
            e.preventDefault();

             if (!jQuery('#<?php echo $modal_id; ?>').length) { 
             jQuery.when(
               jQuery.ajax({
               url: 'ajax.php',
               success: function(response) {
                     jQuery(response).appendTo(jQuery('body'));
                   }
               })).done( function (){
                        jQuery('#<?php echo $modal_id; ?>').modal('show');                                     
                        }); 

            }
            }); 

But unfortunately I get only:

<div class="modal fade " id tabindex="-1" role="dialog" aria-hidden="true"></div>

Any solutions? Thanks! P.S. Vars in current symbol table do exist.

  • 写回答

1条回答 默认 最新

  • dousong2023 2016-08-04 14:38
    关注

    You are not sending your variables trough AJAX - so you are not receiving them back. You should rewrite your code like this:

    <?php
    private function generate_js( $call_selector, $modal_id ) 
    {
      ob_start();
      extract($this->args); ?>        
      $("<?php echo $call_selector; ?>").click(function(e)
      { 
        e.preventDefault();
    
        if (!jQuery('#<?php echo $modal_id; ?>').length) 
        { 
          jQuery.when(
            jQuery.ajax({
              url: 'ajax.php?id=<?php echo $id; ?>&class=<?php echo $class; ?>',
              success: function(response) 
              {
                jQuery(response).appendTo(jQuery('body'));
              }
            })
          ).done( function (){
            jQuery('#<?php echo $modal_id; ?>').modal('show');                                     
        }); 
      }
    }); 
    

    and also change your ajax.php like this:

    <div class="modal fade <?php echo $_GET['class']; ?>" id="<?php echo $_GET['id']; ?>" tabindex="-1" role="dialog" aria-hidden="true"></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?