rdkurth Posted March 23, 2014 Share Posted March 23, 2014 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 --> Quote Link to comment https://forums.phpfreaks.com/topic/287209-i-need-to-open-a-bootstrap-modal-and-pass-a-php-var-using-jquery-ajax/ Share on other sites More sharing options...
trq Posted March 24, 2014 Share Posted March 24, 2014 Why would you need any Ajax for this? $(".launch-modal").click(function() { var id = $(this).attr('id'); }); Quote Link to comment https://forums.phpfreaks.com/topic/287209-i-need-to-open-a-bootstrap-modal-and-pass-a-php-var-using-jquery-ajax/#findComment-1473658 Share on other sites More sharing options...
rdkurth Posted March 24, 2014 Author Share Posted March 24, 2014 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 }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/287209-i-need-to-open-a-bootstrap-modal-and-pass-a-php-var-using-jquery-ajax/#findComment-1473662 Share on other sites More sharing options...
atxaga Posted September 16, 2014 Share Posted September 16, 2014 Hi rdkurth, I'm having the exact same problem a few months later (I can pass the variable but it's always the last in the list of results rather than the id for each result). I wonder if you found out how to do this? Any help much appreciated! cheers Quote Link to comment https://forums.phpfreaks.com/topic/287209-i-need-to-open-a-bootstrap-modal-and-pass-a-php-var-using-jquery-ajax/#findComment-1491338 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.