【问题标题】:Update mutiple rows from dynamically generated input field PHP, MySQL,jQuery从动态生成的输入字段 PHP、MySQL、jQuery 更新多行
【发布时间】:2012-06-18 23:12:12
【问题描述】:

我需要一些关于如何从动态创建的输入字段成功更新数据库中的多行的建议。

那么,我所拥有的是:

PHP

<?php
   while ($row = mysql_fetch_array($sql)) {
       echo "<input class='estemated_days' type='text' id='".$row['scpe_id']."' value='".$row['scpe_estemated_days']."'></td>";
   }
?>

这将输出如下内容:

HTML

<input class='estemated_days' type='text' id='718' value='5'>
<input class='estemated_days' type='text' id='719' value='8'>
<input class='estemated_days' type='text' id='720' value='10'>

<input type='button' id='save' value='Save'> <!-- Button to jQuery -->

.....等等

这就是我缺乏知识的地方。我希望 jQuery 做这样的事情:

jQuery

($"#save").click(function () {

  // Get value of id from (".estemated_days") as an identifier, and get the input value it contains
  // Send to /update.php

});

然后,update.php 将执行以下操作:

PHP

<?php

if (isset($_POST['save'])) {

    /*
        Get all of the id's and the value it contain's

        perform:
    */

    mysql_query = ("UPDATE myDatabase SET estemated_days = '$the_value_from_the_input' WHERE scpe_id = '$the_value_of_the_id'");
    //Repeat this for all rows from the webpage
}

?>

我的知识是基本的网络编程,但我真的很想完成这项工作。有人对我应该如何做有建议吗?

【问题讨论】:

  • <input> 标签没有 data-id 属性。为什么不使用id
  • @Mike 哦,我不知道。是的,我想ID会起作用。我将在我的帖子中对此进行编辑。

标签: php jquery mysql input


【解决方案1】:
var values = {};
$('input.estimated_days').each(function(n, el){
   values[ $(el).attr('id') ] = $(el).val();
});

$.ajax(
  {
    type : 'POST',
    url : 'update.php',
    data : {edays: values}
  }
); /// see jquery docs for ajax callbacks 

<?php
   foreach($_POST['edays'] as $id=>$value) // ...

http://api.jquery.com/jQuery.ajax/http://api.jquery.com/attr/

...别忘了清理 mysql 的输入

【讨论】:

  • 谢谢!我会尽快试试这个。
  • 谢谢。你知道为什么这段代码不会更新我的数据库吗?请参阅 pastebin 链接:pastebin.com/p8ks686W 我现在注意到我没有以分号结尾,但是现在添加了它,但仍然没有运气。
  • 在 jquery ajax 中定义一个成功回调,提醒您输出:success : function(data){ alert(data); } - 然后您可以 var_dump $_POST 或转储 mysql 最后一个错误并在弹出窗口中获取该输出。查看 jquery ajax 链接了解回调
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多