【问题标题】:Pass selected dataTable row to another dataTable row in another php page将选定的 dataTable 行传递给另一个 php 页面中的另一个 dataTable 行
【发布时间】:2019-07-29 02:14:33
【问题描述】:

我有这两张表,分别是table1.php和table2.php

我可以使用这个脚本将数据传递给一个modal,这个脚本在table1.php中,它可以通过modal将数据传递给table2.php,我现在关心的是如何将数据直接传递给table2.php SQL 查询?由于它的方式,我只能使用传递给 HTML 元素的模式的数据。有没有办法传递模态值并将其转换为 PHP 的 SQL 查询?

$(function(){
  $("body").on('click', '.edit', function (e){
    e.preventDefault();
    $('#edit').modal('show');
    var id = $(this).data('id');
    getRow(id);
  });

我正在尝试将选定的数据行传递到另一个 PHP 页面,以便它将成为过滤第二个 PHP 表的参数。

这是我的table1.php

<form type="POST" action="table2.php">
              <table id="example1" class="table table-bordered">
                <thead>
                  <th>Series No.</th>
                  <th>Account Type</th>
                  <th>Tools</th>
                </thead>
                <tbody>
                  <?php
                    $sql = "SELECT * FROM accounttype";
                    $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                      echo "
                        <tr>
                        <td>".$row['seriesno']."</td>
                          <td>".$row['accounttype']."</td>
                          <td>
                            <button class='btn btn-success btn-sm edit btn-flat' data-id='".$row['seriesno']."'><i class='fa fa-edit'></i> Edit</button>
                            <button class='btn btn-danger btn-sm delete btn-flat' data-id='".$row['seriesno']."'><i class='fa fa-trash'></i> Delete</button>
                          </td>
                        </tr>
                      ";
                    }
                  ?>
                </tbody>
              </table>
              </form>

这将是我的 table2.php

<table id="example2" class="table table-bordered">
                <thead>
                  <th>Series No.</th>
                  <th>Account Type</th>
                </thead>
                <tbody>
                  <?php
                    $id=$_POST['seriesno'];
                    $sql = "SELECT * FROM accounttype where seriesno='$id'";
                    $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                      echo "
                        <tr>
                        <td>".$row['seriesno']."</td>
                          <td>".$row['accounttype']."</td>
                        </tr>
                      ";
                    }
                  ?>
                </tbody>
              </table>

问题是 table2.php 无法接收到 $_POST,这似乎是什么问题?

编辑:这是我的模式 (table2.php) 我需要将数据传递到表中,以便 $id=$_POST['seriesno']; 可以工作。

<div class="modal fade" id="edit">
    <div class="modal-dialog" style="width:100%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">
                    <input type="hidden" class="decid" id="id" name="id">

  <table id="example2" class="table table-bordered">
                <thead>
                  <th>Series No.</th>
                  <th>Account Type</th>
                </thead>
                <tbody>
                  <?php
                    $id=$_POST['seriesno'];
                    $sql = "SELECT * FROM accounttype where seriesno='$id'";
                    $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                      echo "
                        <tr>
                        <td>".$row['seriesno']."</td>
                          <td>".$row['accounttype']."</td>
                        </tr>
                      ";
                    }
                  ?>
                </tbody>
              </table>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 你在哪里发帖到 table2?
  • @MoisheSchwartz $id=$_POST['seriesno'];,我正在尝试将选定的 dataTable 值传递给 table2.php
  • @pjustindaryll,我上面的评论者询问您尝试将数据提交到另一个 .php 脚本的表单在哪里?显示您的模态 html。
  • @SergheiLeonenco 抱歉,我正在尝试将其提交到另一个 .php 页面。
  • @SergheiLeonenco 我可以使用脚本将数据传递给模态,这个脚本在table1.php中,它可以通过模态将数据传递到table2.php,我现在关心的是我将如何传递数据直接进入 table2.php 的 SQL 查询,而不是通过模态传递?

标签: php jquery html ajax sqlsrv


【解决方案1】:

所以.. 问题是您必须在同一页面上提交:并在同一页面的模式窗口中解析 $_POST 数组,因为它包含在此处。 试试这个:

table1.php:

<form method="POST" action="table1.php">
          <table id="example1" class="table table-bordered">
            <thead>
              <th>Series No.</th>
              <th>Account Type</th>
              <th>Tools</th>
            </thead>
            <tbody>
              <?php
                $sql = "SELECT * FROM accounttype";
                $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                  echo "
                    <tr>
                    <td>".$row['seriesno']."</td>
                      <td>".$row['accounttype']."</td>
                      <td>
                        <button class='btn btn-success btn-sm edit btn-flat' data-id='".$row['seriesno']."'><i class='fa fa-edit'></i> Edit</button>
                        <button class='btn btn-danger btn-sm delete btn-flat' data-id='".$row['seriesno']."'><i class='fa fa-trash'></i> Delete</button>
                      </td>
                    </tr>
                  ";
                }
              ?>
            </tbody>
          </table>
          </form>

table2.php

<div class="modal fade" id="edit">
    <div class="modal-dialog" style="width:100%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">
                    <input type="hidden" class="decid" id="id" name="id">

  <table id="example2" class="table table-bordered">
                <thead>
                  <th>Series No.</th>
                  <th>Account Type</th>
                </thead>
                <tbody>
                  <?php
                    if(isset($_POST['seriesno'])){
                    $id=$_POST['seriesno'];
                    $sql = "SELECT * FROM accounttype where seriesno='$id'";
                    $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                      echo "
                        <tr>
                        <td>".$row['seriesno']."</td>
                          <td>".$row['accounttype']."</td>
                        </tr>
                      ";
                    }
                    }

                  ?>
                </tbody>
              </table>
            </div>
        </div>
    </div>
</div>

希望对你有帮助

【讨论】:

  • 它没有用。数据来自Table1,然后传递给Table2。
  • 请发布您的table2.php
  • 已发布。我的模态和数据表在 table2.php 中
  • 我重读了您的整篇文章并有一个问题:您是如何组织代码的?为什么调用 modal 的 jquery 脚本在 table1.php 中,为什么你的 modal 位于 table2.php 中?它们都包含在一个 index.php 中吗?或者你是如何组织文件的?
  • 抱歉,我的 jquery 脚本位于 table1 中。然后将 table2 添加为主页中的include()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 2014-10-20
  • 2021-01-25
  • 2021-05-09
  • 1970-01-01
  • 2011-07-06
  • 2012-01-01
相关资源
最近更新 更多