php - 将 PHP 变量传递给模态并在 MySQL 查询中使用它

标签 php html mysql bootstrap-modal

我有一个带有多个编辑按钮的表格。每个编辑按钮都应该打开一个模式,我试图将delivery_id传递给它,这样我就可以在MySQL查询中使用它

echo "<td><button type='button' class='btn dt_buttons' data-toggle='modal' data-id='$delivery_id' data-target='#editModal'>Edit</button></td>";

在模态中检索该值并将其用作变量的最佳方法是什么?我认为只使用 $delivery_id 就可以了,但这当然太简单了!

模态中的代码:

<div id="editModal" 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">&times;</button>
        <h4 class="modal-title">Edit Purchase</h4>
      </div>
      <div class="modal-body">
      <?
        $query      =   "SELECT id, supplier_id, date as del_date, delivery_number, po_number, cost_value FROM store_purchases WHERE id = $delivery_id";
                echo $query;
        $retval     =   f_select_query($query, $datarows);

        $lint_product_id                =    f_htmlspecialchars_decode($datarows[0]->id , ENT_QUOTES);


        $supplier_id            =    intval($datarows[0]->supplier_id);

        $delivery_date          =   $datarows[0]->del_date;

        $delivery_number            =    intval($datarows[0]->delivery_number);

        $lint_unit_cost                 =    f_htmlspecialchars_decode($datarows[0]->cost_value , ENT_QUOTES);
        $lint_unit_cost                 =    floatval($lint_unit_cost);
        $lint_unit_cost                 =    number_format($lint_unit_cost, 2);



$department_id_dropdown     =   f_get_dropdown("supplier_name", "supplier_name", "supplier_master", $supplier_id, "id", " store_id = $store_id", '', '', '', false, false, true);
?>
      <div class="container-fluid" id="div_user_master" class="ae_form" >
        <form id="myForm" action="/platformDev/create_subscription.php" method="POST">
        <?
        echo "Supplier Name: <td class='text-right' id='department_id' style='width:20%;'> $department_id_dropdown </td> <input id='purch_id' name='purch_id' class='form-control purch_id' value='$product_id' type='hidden'/>";
        echo "Delivery Date: <span class='required_field'><i class='fa fa-star fa-sm'></i>   </span> <input class='form-control' tabindex='3' id='date' name='date' value= '$delivery_date' type='text'/>   <br/>";
        echo "Delivery Number: <input type='text' id='unit_cost' name='unit_cost' class='form-control unit_cost' style='width:80%;' value='$delivery_number' />";
        echo "Invoice Cost: <input type='text' id='unit_cost' name='unit_cost' class='form-control unit_cost' style='width:80%;' value='$lint_unit_cost' /></div>";
?>



        </form>
    </div>
      </div>
      <div class="modal-footer">
        <button class="btn form-btns btn-primary"  style="float: left;" data-dismiss="modal" id="customButton">Add Purchase</button>
        <button type="button" class="btn  dt_buttons close_this ajax_forms" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>

最佳答案

下面的代码将从单击的按钮获取data-id值。

您可以使用此模板:

$('#editModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget);
  var delivery_id = button.data('id'); // delivery id here


  var modal = $(this)
  modal.find('.modal-title').text('Delivery #' + delivery_id);
  modal.find('.modal-body').html('content here');
});

https://getbootstrap.com/docs/4.0/components/modal/ https://getbootstrap.com/docs/3.3/javascript/#modals

关于php - 将 PHP 变量传递给模态并在 MySQL 查询中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50349089/

相关文章:

html - 在 bulma 中垂直居中元素

php - 如何防止网页中出现奇怪的字符

javascript - 什么是K9加密算法

javascript - 页面加载后加载JS代码

html - Visual Studio 2010 是否支持 HTML 5?

php - 如何将mysql中的两列合并为一列?

python - scrapy抓取多个页面,提取数据并保存到mysql中

php - SugarCRM SugarLogic 公式

php - 中文/日文字母的正则表达式

html - :before psuedo element changes into share icon怎么办