使用jquery mysql从click中填充多个字段

时间:2013-09-09 20:22:34

标签: php jquery mysql

我有一个动态网格,使用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');
                  }
           }
   });

});

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

谢谢

1 个答案:

答案 0 :(得分: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>