问题描述
我需要一些有关如何从动态创建的输入字段中成功更新数据库中多个行的建议.
所以,我有这个:
I need some advice on how to successfully update mutiple rows in my database from dynamically created input fields.
So, what I have is this:
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 -->
..... etc.
.....etc.
这是我缺乏知识的地方.我希望jQuery做这样的事情:
jQuery
Here is where my knowledge is lacking. I want jQuery to do something like this:
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
Then, the update.php
would do something like this:
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
}
?>
我的知识是基本的Web编程,但是我真的很想做这项工作.有人得到我应该怎么做的建议吗?
My knowledge is basic web programming but I would really like to make this work. Anyone got advice on how I should do it?
推荐答案
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的输入
... and don't forget to sanitize the input for mysql
这篇关于从动态生成的输入字段PHP,MySQL,jQuery更新多个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!