0

我确实有一个动态网格,它使用 PHP 显示 MySQL 表中的项目。我有一个图标,它应该根据当前寄存器的 id 从 MySQL 获取数据,并用相关信息填写表单中的字段:

PHP/HTML 网格

$output .='<tr id="'.$id.'">';
$output .='<td>'.$description.'</td>';
$output .='<td>'.$description_pt.'</td>';
$output .='<td align="right">US$'.number_format($price, 2, '.', ',').'</td>';
$output .='<td align="center">'.$a_c.'</td>';
$output .='<td align="center">'.$note.'</td>';
$output .='<td align="center">'.$note_pt.'</td>';
$output .='<td align="center" class="icon_grid"><a class="editar" title="Open the register." href="#"><img src="images/Write2.gif" width="16" height="16" /></a></td>';
$output .='<td align="center" class="icon_grid"><a class="delete" title="Delete the register." href="#"><img src="images/Trash.gif" width="16" height="16" /></a></td>';
$output .='</tr>';

HTML 表单:

<div id="edita_cadastro">
<form name="form3" id="form3" method="post" action="" >
  <table width="50%" border="0" cellspacing="2" cellpadding="0">
  <tr>
   <td colspan="2"><h3>Edit the current register</h3></td>
  </tr>
  <tr>
   <td align="right"><label for "edt_description"><img src="images/usa.jpg" width="18" height="15" />Description:</label></td>
   <td><input type="text" name="edt_description" id="edt_description" size="45" /></td>
  </tr>
  <tr>
   <td align="right"><label for "edt_description_pt"><img src="images/brazil.jpg" width="18" height="15" />Description:</label></td>
   <td><input type="text" name="edt_description_pt" id="edt_description_pt" size="45" /></td>
  </tr>
  <tr>
   <td align="right"><label for "edt_price">Price:</label></td>
   <td><input type="text" name="edt_price" id="edt_price" size="35" placeholder="ex.: 1.00" /></td>
  </tr>
  <input type="hidden" name="pack_id" id="pack_id" value="<?php echo $pack_id; ?>" />
   <td><input type="hidden" name="editar" value="editar" /></td>
   <td><input type="submit" name="submit" id="submit" value="Save" /></td>
  </tr>
  </table>
  </form>
</div>

现在,PHP get_prices.php

include '../connect_to_mysql.php';

$item_id = $_POST['pk_id']; // Selected item Id

$query  = "SELECT * from packages_prices WHERE id='$item_id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result, MYSQL_ASSOC);

$description = $row['description'];
$description_pt = $row['description_pt'];
$price = $row['edt_price'];

$arr = array( 'edt_description' => $description, 'edt_description_pt' => $description_pt, 'edt_price'=> $price);

echo json_encode( $arr );

最后,jQuery 脚本:

$(document).ready(function(e) {
  $("#tabela tr[id]").on("click", ".editar", function () {  // Tabela is the table's id

    var obj = $(this).closest("tr[id]");

    $.ajax({
           type: "POST",
           url: 'get_prices.php',
           data: { pk_id: obj.attr("id")},
           dataType: "json",
           success: function(data, evt) {
           if (data.success == "true") {
               $("#edt_description").val(data.edt_description);
               $("#edt_description_pt").val(data.edt_description_pt);
         $("#edt_price").val(data.edt_price);

           } else {
               alert('error');
                  }
           }
   });

});

所以,我不知道可能出了什么问题。数据不会到来,当然也不会填满这些字段。有谁知道发生了什么?

谢谢

4

1 回答 1

1

你有一个语法错误,你忘记}); 了你的脚本,所以这可能是它不起作用的原因:

<script>

    $(document).ready(function(e) {
  $("#tabela tr[id]").on("click", ".editar", function () {  // Tabela is the table's id

    var obj = $(this).closest("tr[id]");

    $.ajax({
           type: "POST",
           url: 'get_prices.php',
           data: { pk_id: obj.attr("id")},
           dataType: "json",
           success: function(data, evt) {
           if (data.success == "true") {
               $("#edt_description").val(data.edt_description);
               $("#edt_description_pt").val(data.edt_description_pt);
         $("#edt_price").val(data.edt_price);

           } else {
               alert('error');
                  }
           }
      });

     });

  }); 

</script>
于 2013-09-09T22:56:23.430 回答