2

I have below code:

<?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);
?>

Now whenever, i click the Turn Off button associated to some particular row, lets assume, I clicked for Row 3, then the first record gets deleted, rather than third.

I understand, it is picking the value from First Form encountered in DOM.

Please help.

OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 30 '15 at 18:15

2 Answers2

2

while ID should be unique you already echo out more than one form with same ID .. better to use CLASSES for that .. the reason for your problem that you use jQuery("#deleterecord").serialize() .. which will output the first values from the first element with that id so you can use $('form').on('submit'); and then use $(this) to get this form values

jQuery(document).ready(function(){
}).on("submit","#deleterecord" ,function(){ 
      jQuery.post("updater", jQuery(this).serialize(), function(data){
      alert("The Maintenance Page is Off");
      location.reload();
      });
 });  
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • I did this change but now the button click is not responding.. I have not changed ".live" to ".on" and "click" to "submit", as I have old jquery library. and I cannot change it – OM The Eternity Apr 30 '15 at 17:07
  • @OMTheEternity my final answer updated .. I believe that will work but I'm not sure about old jquery library .. If that code not work try to change your form IDs to Classes may be .. Good Luck – Mohamed-Yousef Apr 30 '15 at 17:22
0

Jquery live is long gone because of performance issues.

See the above page and replace live for on.

http://api.jquery.com/on/

Diogo Cunha
  • 1,194
  • 11
  • 23