简体   繁体   English

将PHP变量传递给模式窗口

[英]Passing PHP variable to modal Window

I am following the solution given at Pass PHP variable to bootstrap modal 我正在遵循将PHP变量传递给Bootstrap模态的解决方案

In my case, the trigger link passes the right value to the AJAX method, I have ckecked it in the developer consoler from the browser: 就我而言,触发器链接将正确的值传递给AJAX方法,我已经在浏览器的开发人员控制台中对其进行了修改:

<td><a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="8">Edit</a></td>

Then, the modal windows opens, but the test value from info-doctor.php is not shown in it, not the test value and not the text "NO DATA". 然后,将打开模式窗口,但其中未显示info-doctor.php中的测试值,而不是测试值,也不显示文本“ NO DATA”。

What is wrong in my implementation? 我的实现有什么问题?

Here you have my code: 这里有我的代码:

Trigger: 触发:

$mostrar = '<td><a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="'.$row['id_doctor'].'">Edit</a></td>'; 

JS: JS:

  $('#myModal').on('show.bs.modal', function (e) {
        var rowid = $(e.relatedTarget).data('id');
        console.log(rowid)
        $.ajax({
            type : 'post',
            url : 'info-doctor.php', //Here you will fetch records 
            data :  'rowid='+ rowid, //Pass $id
            success : function(data){
            $('.fetched-data').html(data);//Show fetched data from database
            }
        });
     });

PHP: PHP:

<?php

if($_POST['rowid']) {
    $id = $_POST['rowid']; //escape string

 }
 else {
     echo "NO DATA";
 }
?>

EDIT 编辑

This is the complete JS: 这是完整的JS:

<script type="text/javascript" language="javascript" >


            $(document).ready(function(){


        $('#myModal').on('show.bs.modal', function (e) {
        var rowid = $(e.relatedTarget).data('id');

        $.ajax({
            type : 'post',
            url : 'info-doctor.php', //Here you will fetch records 
            data :  'rowid='+ rowid, //Pass $id
            success : function(data){
            $('.fetched-data').html(data);//Show fetched data from database
            console.log(rowid)
            }
        });
     });
       var dataTable =  $('#employee-grid').DataTable( {
                processing: true,
                serverSide: true,

                ajax: "employee-grid-data.php", // json datasource
                 //send it through get method

                language: {
        processing:     "Procesando datos...",
        search:         "Buscar:",
        lengthMenu:    "Mostrar _MENU_ doctores/as",
        info:           "Mostrando del doctor/a _START_ al _END_ de un total de _TOTAL_ doctores/as seleccionados" ,
        infoEmpty:      "Mostrando doctor/a 0 al 0 de un total de 0 doctores/as",
        infoFiltered:   "(filtrados de _MAX_ doctores/as)",
        infoPostFix:    "",
        loadingRecords: "Procesando datos...",
        zeroRecords:    "No hay doctores/as que cumplan los criterios",
        emptyTable:     "Noy hay datos que cumplan los criterios",
        paginate: {
            first:      "Primero",
            previous:   "Anterior",
            next:       "Siguiente",
            last:       "Ultimo"
        },
        aria: {
            sortAscending:  ": activer pour trier la colonne par ordre croissant",
            sortDescending: ": activer pour trier la colonne par ordre décroissant"
        }
    }

                } );

               var colvis = new $.fn.dataTable.ColVis( dataTable, {
                    buttonText: '<img src="images/down.gif" >',
                    activate: 'mouseover',
                    exclude: [ 0 ]  
                   } );
               $( colvis.button() ).prependTo('th:nth-child(1)');



            } );
        </script>

And this is the complete PHP for testing the issue: 这是用于测试问题的完整PHP:

<?php

     echo "NO DATA";

?>

You don't need AJAX to pass a variable if it is already set on the primary page especially if it's already a data attribute on the link. 如果已经在主页上设置了变量,则不需要AJAX传递变量,特别是如果它已经是链接上的data属性。 Use the data attribute on the modal link: 在模式链接上使用data属性:

<a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="'.$row['id_doctor'].'">Edit</a>

Specifically, you have it available via data-id="'.$row['id_doctor'].'" 具体来说,您可以通过data-id="'.$row['id_doctor'].'"

So.... 所以....

   $('#myModal').on('show.bs.modal', function (e) {
       var rowid = $(this).attr('data-id');
        /*
        proceed with rest of modal using the rowid variable as necessary 
        */
     });

OR 要么

     $('#myModal').on('show.bs.modal', function (e) {
       var rowid = $('#custId').attr('data-id');

     /*
       (This may need better targeting than an id which appear as though it would repeat resulting in invalid markup.) 
       proceed with rest of modal using the rowid variable as necessary 
     */
     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM