简体   繁体   English

使用jquery ajax将php变量传递给模式

[英]Pass php variable using jquery ajax to a modal

I am having a problem with my jquery ajax script. 我的jquery ajax脚本有问题。 I can not get it to pass the variable to the modal. 我无法将变量传递给模态。 I have been messing with this all weekend and can not figure out way it does not pass the variable. 我整个周末都在混乱,无法弄清楚它不会传递变量的方式。

This is the link to call the modal and the id I am trying to pass 这是调用我尝试传递的模式和ID的链接

echo '<img src="./images/see.png" class="open-editexpenses" data-target="#editexpenses" data-id="' . $value['expid'] . '" >';

This is the jquery ajax script 这是jquery ajax脚本

$(document).on('click', '.open-editexpenses', function() {
                var id = $(this).data('id');
                $.ajax({
                    type: "POST",
                    url: "expenses.php",
                    data: {id: id},
                    success: function() {
                        $('#editexpenses').modal('show');
                    }
                });
            });

This is the modal I am opening 这是我正在打开的模式

<div class="modal fade" id="editexpenses" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">

      <?php

        echo $id;
         var_dump($_POST);
         //var_dump($_GET); 
                 ?>  



      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

$(x).data('id') is not the same as $(x).attr('data-id') , which is probably what you're after to read the data-id attribute of your img element. $(x).data('id')$(x).attr('data-id') ,这可能是您读取img元素的data-id属性所需要的。

See jQuery Data vs Attr? 看到jQuery Data vs Attr吗? for more info. 有关更多信息。

I'm going to assume the 'modal' is expenses.php and also that you're using a jQuery plugin for $('#editexpenses').modal('show'); 我将假设“模态”是expenses.php ,并且您还将jQuery插件用于$('#editexpenses').modal('show'); to mean anything. 什么意思

From what I can see you haven't actually added the response from expenses.php into the DOM. 从我可以看你有没有实际添加从响应expenses.php到DOM。 You can do this by altering the success function in your AJAX call: 您可以通过更改AJAX调用中的success函数来完成此操作:

success: function(html) {
    $('body').append(html);
    $('#editexpenses').modal('show');
}

Here I've just added the HTML returned to the end of the page, but jQuery has plenty of other ways you can insert into the DOM: https://api.jquery.com/category/manipulation/ 在这里,我刚刚添加了返回页面末尾的HTML,但是jQuery还有许多其他方式可以插入DOM: https : //api.jquery.com/category/manipulation/


As an aside, be careful about assuming that the $id variable in expenses.php exists. 顺便说一句,小心假设$id变量expenses.php存在。 This works when PHP's register_globals setting is on, but this is considered extremely bad practice and was deprecated in PHP 5.3.0 and actually removed in 5.4.0. 当启用PHP的register_globals设置时,此方法有效,但是,这被认为是非常糟糕的做法,在PHP 5.3.0中已弃用,而在5.4.0中已被删除。 Just use $_POST['id'] instead. 只需使用$_POST['id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM