Jump to content

I need to open a Bootstrap modal and pass a php var using jquery ajax


rdkurth

Recommended Posts

I need help with adding some ajax to my jquery script to pass a php var to a modal. I can open the modal with the bit of jquery below but I am at a lose on how to add any ajax to this jquery script to take the $id from the link to the modal. 

 

 

The jquery I am opening the modal with

$(document).ready(function(){

    $(".launch-modal").click(function(){

        $("#editexpenses").modal({

            keyboard: false

        });

    }); 

});

I am opening the modal with this link to the jquery

<a id="<?php $id ?>" title="Edit this item" class="launch-modal" href="#editexpenses">edit</a>

The modal

<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">×</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      
      
      
        
     
     	<?php $id = $_POST['expID'];
                echo 'id' . $id;
                
                
                var_dump($_POST); 
                 ?>	 
     
     
     
      </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 -->


Link to comment
Share on other sites

Why would you need any Ajax for this?

 

$(".launch-modal").click(function() {
  var id = $(this).attr('id');
});

I just tried your code and the problem is it just shows the last id number in a list of records. The leak that I am using to go to the modal is a list of item and the link grabs the id number that is selected and opens the modal to shows all of the fields for that id number so it can be edited.Thats way I need to do it with ajax

This is what I have tried but it does not pass the id it does open the modal

$(document).ready(function(){

    $(".btn").click(function(){

        $("#myModal").modal('show');
        
        var id = $(‘id’).val();
        $.ajax({
        type: "POST",
        url: expenses.php,
        data: "id=" + id,
        success : function(data){$('#id').val(data)},
        dataType: html
});
    });

});
Link to comment
Share on other sites

  • 5 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.