问题
I have a modal open on click anchor tag and passing the id value in input hidden field using javascript and value is show in this hidden field
<a href="#modal2" data-toggle="modal" data-id="<?php echo $CRow['id'];?>"
id="<?php echo $CRow['id'];?>" class="btn-floating
waves-effect waves-light yellow modal-trigger" title="Test"></a>
<div id="modal2" class="modal">
<form action="lst_applicant.php" method="post" enctype="multipart/form-data">
<div class="modal-content">
<input type="hidden" id="applicantid" name="applicantid" value="" />
<p>Applicant Information</p>
<?php
$applicant_detail=mysql_query("select * from tbl_useraccount where id=".$applicantid);
?>
</div>
<div class="modal-footer">
<button type="submit" name='save' class="btn waves-effect waves-light right modal-close">Save</button>
</div>
</form>
</div>
Here is the code of javascript passing the id value in input hidden field
<script>
$(document).on("click", ".modal-trigger", function () {
var myBookId = $(this).data('id');
$(".modal-content #applicantid").val( myBookId );
});
</script>
Issue is that when I get id in hidden field but cannot get in php variable. how to get id on modal when opening a modal?
When I click on anchor tag for open a modal then on modal there is one input hidden field, in this hidden field show a id value of each anchor tag using javascript, I want that when this hidden field get id value so how to get in php variable? I am getting in php variable but it is not working
回答1:
You can achieve data like this, you just have to provide the details in the anchor modal data
data-id="<?php echo $CRow['id'];?>"
and for name data-name="<?php echo $CRow['name'];?>"
and so on
$(document).on("click", ".modal-trigger", function () {
var myBookId = $(this).data('id');
var name = $(this).data('name');
var email = $(this).data('email');
$(".modal-content #applicantid").val( myBookId );
$("#appli_name").val(name);
$("#appli_email").val(email);
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<a href="#modal2" data-toggle="modal" data-id="23" data-name="John" data-email="john@gmail.com" class="btn-floating waves-effect waves-light yellow modal-trigger" title="Test">Click</a>
<div id="modal2" class="modal">
<form>
<div class="modal-content">
<input type="text" id="applicantid" name="applicantid" value="" />
<p>Applicant Information</p>
<input type="text" id="appli_name"><br>
<input type="text" id="appli_email">
</div>
<div class="modal-footer">
<button type="submit" name='save' class="btn waves-effect waves-light right modal-close">Save</button>
</div>
</form>
</div>
回答2:
You don't have to get details of that data only inside the modal, you can achieve everything before anchor-tag
passing, like how you get id
. And you can pass along the details of that data inside the modal
Example: data-name="<?php echo $CRow['name']; ?>"
and in your javascript var name=$(this).data('name');
<a href="#modal2" data-toggle="modal" data-id="<?php echo $CRow['id'];?>" id="<?php echo $CRow['id'];?>" data-name="<?php echo $CRow['name']; ?>" class="btn-floating waves-effect waves-light yellow modal-trigger" title="Test"></a>
来源:https://stackoverflow.com/questions/53076845/get-the-id-on-modal-when-click-on-anchor-tag-for-open-modal-using-php