【问题标题】:How to pass variables to bootstrap modal form and run a mysql query如何将变量传递给引导模式表单并运行 mysql 查询
【发布时间】:2019-07-30 03:54:48
【问题描述】:

我有一个包含两个表格的页面:客户和车站。一个 sql 查询用数据填充这些表。我在表格的每一行上定义了一个按钮来打开一个模态编辑表单。对于客户:

<table id = "admin_table_customers"  class="table rtl table-sm table-dark table-hover">
  <tr>
    <th>customer_id</th>
    <th>name</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
  </tr>
  <?php foreach($query_customers as $customer) { ?>
    <tr>
      <td><?php echo h($customer['CUSTOMER_ID']) ?></td>
      <td><?php echo h($customer['CUSTOMER_NAME']) ?></td>
      <td><button type="button" id="button_edit_customer" class="btn btn-primary feed-id" data-toggle="modal" data-target="#edit_customer" data-id="<?php echo h(u($customer['CUSTOMER_ID'])) ?>">edit</button></td>
      <td><button type="button" id="button_delete_customer" class="btn btn-primary feed-id" data-toggle="modal" data-target="#delete_customer" data-id="<?php echo h(u($customer['CUSTOMER_ID'])) ?>">delete</button></td>
    </tr>
  <?php } ?>
</table>

当我在 chrome 中检查源代码时,一切(id 和 data-id)看起来都很好。 我使用这个脚本来检查是否将正确的值传递给模态表单。

$(document).ready(function () {
  $("#button_edit_station").click(function(evt){
    alert($(this).attr("id"));
    alert($(this).data('id'));
  });
  $("#button_edit_customer").click(function(evt){
    alert($(this).attr("id"));
    alert($(this).data('id'));
  });
});

第一个问题:每个表格第一行的按钮显示正确的值,但其他按钮不显示对话框。
我想将每个表的 ID 传递给模态表单并执行查询以填写表单。我想我可以设法将 ID 传递给模态,但是我应该如何以及何时执行查询来填写表格?
这是我的表格:

<div id="edit_customer" class="modal fade" role="dialog" data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header rtl">
        <h4 class="modal-title">edit customer</h4>
      </div>
      <div class="modal-body rtl">
        <form id = "admin_form_edit_customer" action = "" method = "post">
          <div class="md-form mb-5">
            <input type="text" id = "form_edit_customer_id" class="form-control validate">
            <label data-error="wrong" data-success="right" for="form_edit_customer_id">customer_id</label>
          </div>
          <div class="md-form mb-5">
            <input type="text" id = "form_edit_customer_name" class="form-control validate">
            <label data-error="wrong" data-success="right" for="form_edit_customer_name">name</label>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">save</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">cancel</button>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

  • 显示按钮的显示方式

标签: javascript php jquery mysql bootstrap-modal


【解决方案1】:

按照以下步骤进行

  1. 在按钮单击事件“edit_modal(id,name)”上调用函数
  2. 在你的页面上写一个函数

函数 edit_modal(id,name){

// assign value in modal form
$('#form_edit_customer_id').val(id);
$('#form_edit_customer_name').val(name);

//Display modal
$('#edit_customer').modal('show');

}

【讨论】:

  • 谢谢!使用此解决方案,所有按钮都可以完成工作,但所有按钮都将第一行的 id 传递给模态。
【解决方案2】:

我使用不同的方式来实现我的目标。这是我改编的链接: PHP Ajax Insert Data in MySQL By Using Bootstrap Modal

【讨论】:

    猜你喜欢
    • 2016-04-03
    • 2015-01-27
    • 2014-01-05
    • 1970-01-01
    • 2016-04-14
    • 2020-08-03
    • 2017-08-23
    • 2020-08-19
    • 1970-01-01
    相关资源
    最近更新 更多