希望这是有道理的,并提前感谢任何人都可以提供帮助!
我在表格中显示了一组数据,我可以编辑数据,然后用编辑过的数据更新我的数据库。
<input type='button' value='new row' id='save' onClick="addRow()"/>
<table id='mech_table'>
<tr>
<td></td>
<td></td>
<td><input type='text' size='30' name='value_total'id='mech_value_total' value='£0.00' readonly='readonly'/></td>
<td></td>
<td><input type='text' size='30' name='value_claim' id='mech_claim_total' value='£<?php echo $row_MECHT['mech_total']; ?>' readonly='readonly'/></td>
</tr><tr>
<th>ID</th>
<th>Description</th>
<th>Value</th>
<th>%</th>
<th>Claim</th>
</tr>
<?php while ($MECH = mysql_fetch_array($result)) {
?>
<form name='form_update' method='post' action="<?php echo $editFormAction; ?>">
<tr>
<td><?php echo $MECH['id']; ?><input type='hidden' name='id[]' value='<?php echo $MECH['id'];?>' /></td>
<td class='description_row'><input type='text' size='70' name='description[]' value='<?php echo $MECH['description'];?>' /></td>
<td class='value_row'><input class='mech_value_total' type='text' size='30' name='value[]' value='<?php echo $MECH['value'];?>' /></td>
<td class='percent_row'><input class='percent' type='text' size='10' name='percentclaim[]' value='<?php echo $MECH['percentclaim'];?>' /></td>
<td class='claim_row'><input class='mech_claim_total' type='text' size='30' name='claim[]' value='<?php echo $MECH['claim'] ?>' readonly='readonly'/></td>
</tr>
<?php ++$ia;
}
?>
</table>
<input type='submit' value='save' id='save' /><br /><br /><br />
</form>
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$size = count($_POST['id']);
$i = 0;
while($i<$size){
$description= $_POST['description'][$i];
$value= $_POST['value'][$i];
$percentclaim= $_POST['percentclaim'][$i];
$claim= $_POST['claim'][$i];
$id = $_POST['id'][$i];
$query = "INSERT INTO applicationmech
SET id='$id',
description='$description',
value='$value',
percentclaim='$percentclaim',
claim='$claim'
ON DUPLICATE KEY UPDATE description='$description',
value='$value',
percentclaim='$percentclaim',
claim='$claim'";
mysql_query($query) or die ("Error in query: $query");
$updateGoTo = "preditvaluation.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
++$i;
}
我创建了一个带有javascript的简单按钮,允许我在表格中添加其他行。
function addRow() {
$('#mech_table tr:last').after('<tr><td><input type="hidden" name="id[]" value="" /></td><td class="description_row"><input type="text" size="70" name="description[]" value="" /></td><td class="value_row"><input class="mech_value_total" type="text" size="30" name="value[]" value="" /></td><td class="percent_row"><input class="percent" type="text" size="10" name="percentclaim[]" value="" /></td><td class="claim_row"><input class="mech_claim_total" type="text" size="30" name="claim[]" value="" readonly="readonly"/></td></tr>');
}
我的问题是,当我点击更新按钮时,新行中的数据不会保存。
我一直在尝试许多不同的方法以及如何使其工作但每次都失败。
答案 0 :(得分:0)
新行中的数据将没有id值,因此数据库将无法更新UPDATE操作中的行。您需要服务器端代码执行以下操作:
$newids = array();
if (!empty($row->id)) {
$DB->update('tablename', $data);
} else {
$newid = $DB->insert('tablename', $data);
$newids[] = $newid;
}
// Now send the new ids back to the browser so that they can be put into those rows
// and an update operation will be performed next time
echo json_encode($ids);
在浏览器端,例如:
var ids = jQuery.parseJSON(dataFromAjax);
var inputsNeedingIds = $('input:inputclass').filter(
function() {
return ($(this).val() == "") && ($(this).attr('name') == 'id[]'); });
inputsNeedingIds.each(
function(i) {
this.id.value = ids[i];});
答案 1 :(得分:0)
这里唯一有问题的是,添加了由addRow()函数补充的新表单元素后,DOM没有正确更新。表单将使用在开头显示的表单元素进行提交。
只有在使用Ajax提交时才可以这样做。最简单的方法是http://jquery.malsup.com/form/#ajaxForm,它很容易整合。试试看! ;)
如果form_update是您表单的ID,那么代码将是
$('#form_update').ajaxForm({
target: '#result-sub',
success: function(){
$('#result-sub').show();
$('#form_update').hide();
}
});
添加一个id为'result-sub'的div或其他东西,以回复插入的响应。