javascript - GET PHP variable value from HTML data-id -
i asked a question earlier today in nutshell asks when user clicks button on page -- pop modal comes up, should php variable value.
since there no or post request when popup modal btn clicked means can not variable value using above 2 options
tried following:
`<a id="myid" data-id="<?php echo $userid ?>">link</a> <script> $('#myid').data("id"); </script>`
now have php $userid
value in javascript variable. leads me question efficient way retrieve $userid
variable , insert php variable.
additional info
i found question on not applicable me.
example image of im trying achieve
loop generated php list
$teacherclass = new teachersearch(); $teachers = $teacherclass->showallteachers(); if (is_array($teachers)) { foreach ($teachers $teacher) { $src = $teacher['userid']; <div class="teacher-info"> <p class="teacherlabel"> name: <?php echo $teacher['name']; ?> </p> <p class="teacherlabel"> headline: <?php echo $teacher['headline']; ?> <p class="teacherlabel"> location: <?php echo $teacher['location'] ?> </p> <!--button goes here--> <a id="myid" data-id="<?php echo $teacher['userid'] ?>" data-toggle="modal" data-target="#mymodal">hire <?php echo $teacher['name'] ?></a> }//foreach
example img
modal
<!-- trigger modal button --> <!-- modal --> <div id="mymodal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 id="modaltitle" class="modal-title"></h4> </div> <div class="modal-body"> need perform php here $userid variable </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> </div> </div> </div> </div>
first of all, remove id="myid"
<a></a>
. id must unique. use class.
first_page.php
<?php $teacherclass = new teachersearch(); $teachers = $teacherclass->showallteachers(); if (is_array($teachers)) { foreach ($teachers $teacher) { $src = $teacher['userid']; ?> <div class="teacher-info"> <p class="teacherlabel"> name:<?php echo $teacher['name']; ?> </p> <p class="teacherlabel"> headline: <?php echo $teacher['headline'];?> </p> <p class="teacherlabel"> location: <?phpecho $teacher['location'];?> </p> <!--button goes here--> <a class="openmodal" data-id="<?php echo $teacher['userid'] ?>" data-toggle="modal" href="#mymodal"> hire <?php echo $teacher['name']; ?> </a> </div> <?php } }?>
put below code in footer before end of </body>
tag.
<div id="mymodal" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> </div> </div> </div>
js
<script> $('.openmodal').click(function(){ var id = $(this).attr('data-id'); $.ajax({url:"modal_ajax.php?id="+id,cache:false,success:function(result){ $(".modal-content").html(result); }}); }); </script>
create new file modal_ajax.php.
[note: remember, page name modal_ajax.php used in script tag also. if planning change name of page, change page name there in script tag too. both related.]
<div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 id="modaltitle" class="modal-title"></h4> </div> <div class="modal-body"> <?php echo "id: ".$_get['id'];?> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> </div>
already answered similar question. please have passing data via modal bootstrap , getting php variable?
Comments
Post a Comment