【问题标题】:Click on row to pre-populate form with it's data only shows first record单击行以预填充表单,其数据仅显示第一条记录
【发布时间】:2017-05-17 18:32:08
【问题描述】:

我有 hound_view.php,它根据搜索/过滤条件显示记录。当用户点击一行时,会显示一个模态框,其中包含一个用于编辑猎犬信息的表单,我希望根据点击的行预先填充该表单。

hound_view.php 中的一行被点击时 hound_record_click_event.js 将其 data-id 发送到查询数据库的 fill_hound_records.php每行数据都分配一个变量,然后传递给 edit_hound_records.php

中的模式

我的问题是,当我点击任何一行时,我只会获取第一行的数据来填充,但在 fill_hound_records.php 我回显变量以确保每一行都在捕获它的正确信息,它是。

请注意:此代码易受SQL injection 攻击,请勿直接复制。您应该使用mysqliPDO 准备好的语句和this post 中所述的绑定参数。

hound_view.php

  <tbody class="clickable-row" data-toggle="modal" data-target="#editModal" data-id="<?php echo $row['RegNumber'] ?>"> 

hound_record_click_event.js

 $(window).ready(function() {
      //bind the event using jquery not the onclick attribute of the button
      $('.clickable-row').on('click', updateClick);
 });

 function updateClick() {
  var dataid = $(this).data("id");
  $.ajax ({
         type: 'POST',
         url: "fill_hound_record.php",
         data: { dataid : dataid },
         success: function( result ) {
                alert(result);
         }
     });
 };

fill_hound_record.php

<?php
//include database configuration file
include('db.php');

$hounddataid = $_POST["dataid"];

//get rows
$prefillsql = "SELECT * FROM Hounds WHERE RegNumber = '$hounddataid'";
$prefillquery = mysqli_query($con,$prefillsql)or die(mysql_error());
$prefillnumrows = mysqli_num_rows($prefillquery);

if($prefillnumrows > 0){

  while($row = mysqli_fetch_array($prefillquery)){

    $prefillbreed = $row['Breed'];
    $_SESSION['pfbreed'] = $prefillbreed;
    $prefillregnumber = $row['RegNumber'];
    $_SESSION['pfregnumber'] = $prefillregnumber;

    echo $_SESSION['pfregnumber'];
}}
?>

edit_hound_record.php

  <input type="text" class="form-control" id="regnumber" name="regnumber" placeholder="Reg Number" value="<?php echo $_SESSION['pfregnumber']; ?>">

【问题讨论】:

  • 您的代码容易受到SQL injection 攻击。您应该使用mysqliPDO 准备好的语句和this post 中描述的绑定参数。
  • 谢谢亚历克斯,我知道并计划回去纠正这个问题,因为这是我的第一个生产规模的 mysql/php 项目,我正在学习很多东西。
  • “计划回去” 不要养成这样想的坏习惯。从一开始就写正确,不需要额外的努力。
  • 虽然我同意,但我最初并没有知识,否则我一开始就会这样做,因此我说我打算回去。

标签: php jquery mysql json ajax


【解决方案1】:

因为没有关于boostrap版本的信息,如果表格是用插件生成的,我建议你改变这一行:

<tbody class="clickable-row" data-toggle="modal" data-target="#editModal" data-id="<?php echo $row['RegNumber'] ?>"> 

与:

<tbody class="clickable-row" data-toggle="modal" data-target="#editModal">

data-id 是点击行的当前第二个单元格文本。为了得到这个值,你可以使用:

$(e.target).closest('tr').find('td:eq(1)').text()

$(window).ready(function () {
    //bind the event using jquery not the onclick attribute of the button
    $('.clickable-row').on('click', updateClick);
});

function updateClick(e) {
    var dataid = $(e.target).closest('tr').find('td:eq(1)').text();
    $.ajax({
                type: 'POST',
                url: "fill_hound_record.php",
                data: {dataid: dataid},
                success: function (result) {
                    alert(result);
                },
                error: function () {
                    $('#editModal').find('.modal-body p').text(dataid);
                }
            }
    );
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<table class="table table-bordered">
    <thead>
    <tr>
        <th>Breed</th>
        <th>Reg Number</th>
        <th>Call Name</th>
        <th>Reg Name</th>
    </tr>
    </thead>
    <tbody class="clickable-row" data-toggle="modal" data-target="#editModal">
    <tr>
        <td>AH</td>
        <td>024193</td>
        <td>Nia</td>
        <td>Dog</td>
    </tr>
    <tr>
        <td>AH</td>
        <td>022222</td>
        <td>Nia</td>
        <td>Dog</td>
    </tr>
    </tbody>
</table>

<div class="modal fade" tabindex="-1" role="dialog" id="editModal">
    <div class="modal-dialog" role="document">
        <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>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                <p>One fine body&hellip;</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

【讨论】:

  • 这感觉更干净并且会使用,但我仍然在同一点上。警报显示在 fill_hound_record.php 中回显的每一行的正确 id,但是当分配给会话变量并在 edit_hound_records.php 中调用时,它只返回第一个行 id 在RegNumber 字段。
  • @TylerS.Smith 因为警报打印正确的值,您需要在相应的字段中设置此值:$('#regnumber').val(result)。为了帮助你,我只创建了一个演示。我希望这可以帮助你更多。让我知道。谢谢
  • @TylerS.Smith 就像你可以看到我在最后添加了一个模态片段,而不是你在 PHP 中创建它。最终的 html 页面应该类似。
  • @TylerS.Smith 你如何显示模态?我假设你的 data-target="#editModal"。如果您以不同的方式执行此操作,则可以使用模态事件:show.bs.modal。我希望这会对你有所帮助。
【解决方案2】:

对于其他有类似问题的人;在 fill_hound_record.php 中,我在 $hounddataid 变量之前添加了一个 if 语句,因此在单击一行之前我不会收到未定义的索引。

if (isset($_POST["dataid"])) { 
  $hounddataid = $_POST["dataid"];
  ...
  }

我删除了 edit_hound_record.phpfill_hound_record.php 中的 value 属性,我创建了一个数组,其中包含填充表单所需的所有值并在 json 中编码.

$arr = array(
  'breed'=>$prefillbreed,
   ...
   );

echo json_encode($arr);
exit();

最后我添加了

dataType: 'json',
 success: function( data ) {
    $("#breed").val(data.breed);
    ...
    }

到我的 hound_record_click_event.js,它将所选的 json 数组值 data.breed 发送到 id 为 breed 的表单中的字段.

【讨论】:

    【解决方案3】:

    首先,我认为您的&lt;tbody&gt; 应该是一个表格行&lt;tr&gt;

    <tbody class="clickable-row" data-toggle="modal" data-target="#editModal" data-id="<?php echo $row['RegNumber'] ?>"> 
    

    其次,在你的 JS 中,你要获取 data-id:

     $(this).attr("data-id") // will return the string "123"
    

    或 .data()(如果您使用较新的 jQuery >= 1.4.3)

    $(this).data("id") // will return the number 123
    

    【讨论】:

    • tbody 和 tr 具有相同的结果,如果将属性移动到 tr,则在技术上可以省略 tbody。并且 $(this).data("id") 被分配给 var dataid。从 fill_hound_record.php 回显时,data-id 一直是正确的,但在实际填写表单时,它只填充第一行 data-id。
    • 好的,你能在 PHP 渲染 ID 等之后从 DOM 中发布一些示例 HTML 表格行吗?
    【解决方案4】:

    您的 java 脚本出错。改变

        $(this).data ('id') to $(this).attr ('id')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 2014-08-12
      相关资源
      最近更新 更多