I need a ajax way of updating the mysql table.

Following is my code that gets the data from mysql table plus ajax function that will send data to processing file. Problem is it is sending every time only the data of the top row ( form). How can send data of every cell(feild) as and when updated?

here is the code..

<?php
session_start();

require_once '..common/microchart.php';
require_once '...../common/mysqlfile.php';
require_once '../../../common/signals.php';   

?>
<html>
<head>
<script>
function afsubmit()
{
var actual=document.getElementById("actual").value;
var month=document.getElementById("month").value;
var objcode=document.getElementById("objcode").value;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","bsc_actual_ajax.php?actual="+actual+"&month="+month+"&objcode="+objcode,true);
xmlhttp.send();

}
</script>
</head>

<body>

<div id="txtHint" >response here</div>
<?php

//session variable

      //$employee_code=$_SESSION['ecode'];

      //$q="apr";
      $employee_code=1233455;

      // trendsignal(80,100);  //testing

     // $q=$_GET["q"];
      $q="apr";
      $con=$mc;

      //$con = mysql_connect('localhost', 'root', '');
      if (!$con)
      {
        die('Could not connect: ' . mysql_error());
      }
      mysql_select_db($db, $con);
      $sql="SELECT objective_name, measure, fq_reporting, tg_$q, ac_$q, perspective,
       objective_code FROM scorecards 
      where employee_code=$employee_code order by perspective ASC";

      $result = mysql_query($sql);

      //for testing the sesion variables

      //echo $_SESSION['valid_user'];
      //echo $_SESSION['ecode'];
      //echo $_SESSION['bsc_id'];
     // echo $_SESSION['group_code'];



      echo "<table>";
      echo "<thead>";
      echo "<th width='25%'>Strategic objectives</th>";
      echo "<th width='25%'>Measures/KPI</th>";
      echo "<th width='1%'>Fq</th>";
      echo "<th>Target</th>";
      echo "<th>Actual</th>";
      echo "<th>Status</th>";
      echo "<th>Trend</th>";
      echo "<th width='12%'>12mon</th>";
      echo "<th>persp</th>";
      echo "</thead>";

      while ($row=mysql_fetch_array($result))

      {
        echo "<tr>";
        echo "<td>$row[0]</td>";
        echo "<td>$row[1]</td>";
        echo "<td>$row[2]</td>";
        echo "<td>";
        echo "<form>";
        echo "<input type='text' size=8 id='target'name='target' value='$row[3]' onchange='afsubmit();'>";
        echo "<input type='hidden' id='month' name='month' value='$q'>";
        echo "<input type='hidden' id='objcode'name='objcode' value='$row[6]'>";
        echo "</form>";
        echo "</td>";
        echo "<td>";
        echo "<form >";
        echo "<input type='text' size=8 id='actual' name='actual' value='$row[4]' onchange='afsubmit();'>";
        echo "<input type='hidden' id='month' name='month' value='$q'>";
        echo "<input type='hidden' id='objcode' name='objcode' value='$row[6]'>";
        echo "</form>";

        echo "<td>";
        dosignal_positive($row[3],$row[4]);
        echo "</td>";
        echo "<td>";
        trendsignal($row[3],$row[4]);
        echo "</td>";
        echo "<td>";
        microchart($row[6]);
        echo "</td>";
        echo "<td>$row[5]</td>";
        echo "</tr>";
      }

   echo "</table>";
?>

</body>
</html>

Can you psl help?

Recommended Answers

All 2 Replies

Your inputs all have the same ID / Name so when JS does the getElementById it just gets the first element it finds on the DOM. You could convert everything into an array and turn it into JSON, you'd have to look up how to do this with standard JavaScript as I only know how to do it with jQuery. The other option would be loop through all your forms and turn it into an XML and send that to be dealt with by PHP (see this tutorial for more on dealing with XML: http://www.ibm.com/developerworks/library/wa-ajaxintro7/index.html)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.