我有以下代码:
<?php
while ($row = mysql_fetch_array($result))//here $result gives me multiple records
{
echo "<tr><td>" .$row['id']. "</td><td>" .$row['username']. "</td><td>" .$row['start']. "</td><td>" .$row['end']. "</td><td>" .$row['zone']. "</td><td><form id='deleterecord'><input type='hidden' id='deleteKey' name='deleteKey' value=" .$row['id']. " /></form><input type='submit' id='offbtn' name='key' value='Turn Off'/></td></tr>";
}
echo "</table>";
?>
================================
jQuery("input:submit[name=key]").live("click", function(){
jQuery.post("updater", jQuery("#deleterecord").serialize(), function(data){
alert("The Maintenance Page is Off");
location.reload();
});
});
=========================
<?php
$NUM = $_POST['deleteKey'];
$db =& JFactory::getDBO();
$query = "UPDATE`ecare_joomla`.`mps_scheduler` SET endTime=NOW() WHERE id=$NUM";
$db->setQuery($query);
$result = $db->query();
$row = mysql_fetch_assoc($result);
?>
现在,每当我单击与某个特定行关联的“关闭”按钮时,假设我单击了第 3 行,那么第一条记录就会被删除,而不是第三条记录。
我的理解,就是从 DOM 中遇到的 First Form 中选取值。
虽然 ID 应该是唯一的,但您已经回显了多个具有相同 ID 的表单.. 最好使用 CLASSES .. 问题的原因是您使用 jQuery("#deleterecord").serialize() .. 这将输出具有该 id 的第一个元素的第一个值,以便您可以使用 $('form').on('submit');然后使用 $(this) 获取此表单值
jQuery(document).ready(function(){
}).on("submit","#deleterecord" ,function(){
jQuery.post("updater", jQuery(this).serialize(), function(data){
alert("The Maintenance Page is Off");
location.reload();
});
});