weixin_33709364 2013-08-14 19:24 采纳率: 0%
浏览 22

javascript到ajax到php

Need help. I have a page called edit.php. I am trying to send a javascript variable to a modal window that contains php, so that I can run a query to pull data. I don't have much experience with ajax, and I couldn't find a good tutorial for this.

Here is my javascript code with an attempted ajax call:

 <script type="application/javascript">
 $(document).on("click", ".open-EditRow", function () {
   var myGroupId = $(this).data('id');
   $(".modal-body #groupId").val( myGroupId );

   // ajax call
   var url = "" + myGroupId;
   $.get(url, function(data){
   // do something here
   });
 });
 </script>

I know I need to create a separate folder with a file that will convert myGroupId into a PHP variable.

Here is the modal that the myGroupId should be returned to:

 <div class="modal hide fade" id="myEditModal" tabindex="-1" role="dialog" aria-labelleby="myModalLabel" aria-hidden="true">
 <div class="modal-body">
 <form class="well-small" action="" method="POST" id="modalForm" name="modalForm">
 <?php 
   // here is where I need myGroupId returned to
 ?>
 </form>
 </div>
 </div>

Can anyone give me an example of how that separate file should look and how to send it back to the modal on my edit.php file?

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • 游.程 2013-08-14 19:28
    关注

    You forgot to add a key to your URL. This should work:

    var url = "?groupid=" + encodeURIComponent(myGroupId);
    $.get(url, function(data){ // etc.
    

    But jQuery can take care of encoding for you if you do it like this:

    $.get(url, {groupid: myGroupId}, function(data){ // etc.
    

    You'll then be able to read it in PHP with $_GET['groupid'].

    Reference: jQuery.get

    评论

报告相同问题?