Simão Abreu
Simão Abreu

Reputation: 13

pass variable to modal via POST

i'm trying to send a variable to pint on a modal but I'm getting Notice: Undefined variable: ordID

I searched on many other requests but none solve my problem.

My code: Table with a to present the modal that will have all information of that line.

<table id="myTable" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
       <thead>
           <tr>
           <th>ID Ordem</th>
           <th>Instalação</th>
           <th>Denominação</th>
           <th>Estado</th>
           <th>Tipo de Manut</th>
           <th>Ver</th>
           </tr>
      </thead>
      <tbody>

     <?php

         require_once 'edp/configdbedp.php';

              $sql="SELECT * FROM ordens";
              //echo $sql;
              $myData=mysqli_query($GLOBALS['con'],$sql);

     while($registos=mysqli_fetch_array($myData)){
       $ordem = $registos['ordem'];
     echo '<tr>';
     echo '<td>' . $registos['ordem'] . '</td>';
     echo '<td>' . $registos['locinstsap'] . '</td>';
     echo '<td>' . $registos['denominacao'] . '</td>';
     echo '<td>' . $registos['pk_idestado'] . '</td>';
     echo '<td>' . $registos['pk_sigla'] . '</td>';
     echo '<td align="center"><a id="getUser" class="modalLink" href="#myModal" data-toggle="modal" data-target="#myModal" data-id="<?php echo $ordem; ?>"><span class="glyphicon glyphicon-edit"></span> Ver</a></td>';
     echo "</tr>";
     }

     //mysqli_close($GLOBALS['con']);
      ?>
      </tbody>
  </table>

My script to send via POST:

    <script>
    $('.modalLink').click(function(){
        var ordem=$(this).data('id');

        $.ajax({
                type:'POST',
                url:'/edp/php/adm/modalordens.php',
                data:'ordID='+ordem,
                success:function(data){
                  $(".modal-content").html(data);
                }
        });
});
</script>

my file modalordens.php

<? $ordID = $_POST['ordID'];?>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
    <h4 class="modal-title" id="fam_id">Ordem <?php echo $ordID;?></h4>
</div>
<div class="modal-body">
    <form id="form1" method="post">
        <b>DetailsOrdem <?php echo $ordID;?></b>
        <hr></hr>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>

I can't print $ordID, I don't know why

Upvotes: 1

Views: 1833

Answers (2)

FAROUK BLALOU
FAROUK BLALOU

Reputation: 728

First you need to pass the ajax data param as an object :

<script>
    $('.modalLink').click(function(){
        var ordem=$(this).data('id');

        $.ajax({
                type:'POST',
                url:'/edp/php/adm/modalordens.php',
                data: { ordID : ordem },
                success:function(data){
                  $(".modal-content").html(data);
                }
            });
    });

</script>

And edit the php opening tag on this line :

<?php $ordID = $_POST['ordID'];?>

Upvotes: 3

Anil
Anil

Reputation: 996

Your ajax post is wrong it should be like

  $.ajax({
                type:'POST',
                url:'/edp/php/adm/modalordens.php',
                data: {ordID : ordem},
                success:function(data){
                  $(".modal-content").html(data);
                }
        });

Upvotes: 2

Related Questions