提问者:小点点

如何将一个jQuery ID发送到Bootstrap模态以在PHP函数中使用它?


我知道有很多类似的帖子,但没有真正帮助我。我正在尝试使用两个参数在引导模式下运行一个PHP函数,其中一个参数是需要从jQuery转出的行ID。我可以发送它并以div id显示它,但是如何为PHP函数转换这个参数呢?

jQuery

$(document).ready(function() {
    $("a").click(function(){
        var hrefs = $(this).attr("href"); 
        $("#modal_form").attr('action',hrefs); 
        var ids = hrefs.split("=").pop();   
        $("#sid").html(ids);        
     });
});

Bootstrap模式

<!-- Update Record Modal -->
<div class="modal fade" id="updateModal" tabindex="-1" aria-labelledby="updateModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-xl">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="updateModalLabel">Update Translation</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
    <form action="translation.php" id="modal_form" method="post">
      <div class="modal-body">
      
      <div id="sid"></div> /* I'm getting the right ID here*/
      
        <?php  selectedTranslation($dbh, $ids);   ?> /*I need to run this function*/
        
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" name="update" class="btn btn-primary">Save changes</button>
      </div>
    </form>
    </div>
  </div>
</div>

php函数

function getAllTranslationById($dbh, $id){
    ...
    <?php 
    
        $sqlSelect = "SELECT * FROM xxxx";
    
    $result = mysqli_query($dbh, $sqlSelect);
    while ($row = mysqli_fetch_array($result)) {
        ?>
        <tr>
            <td><input type="text" class="form-control" value="<?php echo $row['swrd_text'] ?>" readonly="readonly"></td>
            <td><input type="text" class="form-control" value="<?php echo $row['swrd_context'] ?>" ></td>
            <td><a href="translation.php?sid=<?php echo $sid  ?>" data-toggle="modal" data-target="#updateModal"><img alt="Link" src="images/edit.png" title="Update this row"></a></td>
        </tr>
    <?php }?>
   </table>
<?php }

php函数

function selectedTranslation($dbh, $sid){
    
    ....
    
    <?php 
    $sqlSelect = "SELECT * FROM xxxx ";
    
    $result = mysqli_query($dbh, $sqlSelect);
    while ($row = mysqli_fetch_array($result)) {
        ?>
        <tr>
            <td><input type="text" class="form-control" value="<?php echo $row['swrd_text'] ?>" readonly="readonly"></td>
            <td><input type="text" class="form-control" value="<?php echo $row['swrd_context'] ?>" ></td>          
        </tr>
    <?php }?>
   </table>
<?php }?>

共1个答案

匿名用户

只是不可能像你想要的那样

那你能做些什么呢?

使用Ajax。

  <div class="modal-body">
     <div id="sid"></div> /* I'm getting the right ID here*/
     <div id="ajaxResult"></div>
  </div>



  // JS Codes
  $(document).ready(function() {
    $("a").click(function(){
       var sid = ""; // set your `sid` here

       $.ajax({
         url: "/url_here", //url to your PHP page
         type: "post",
         data: {id: sid},
         success: function(data) {
               $("#ajaxResult").html(data); //result html
         }
       });        
    });
  });