HELP!!! Edit content after XMLHTTPRequest

aditya61 0 Tallied Votes 305 Views Share

The above code will get the user information from page "getuser.php". The output of getuser.php has some numericals populated in 5 rows. The 6th row i.e., the table footer has the sum of all numericals populated in the above 5 rows.

The numericals in the first 5 rows are populated in textboxes. The issue here is, after the data is loaded in the main page and I try to edit the numericals in the textboxes, the sum (totals) present in the 6th row are not affected. How do I call the same function after I edit data in textboxes in the main page.

Please help...

main.php
<html>
<head>
	<script type="text/javascript">
		function showUser(str)
		{
			var url="getuser.php";
			url=url+"?q="+str;
			
			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==0 || xmlhttp.readyState==1 || xmlhttp.readyState==2 || xmlhttp.readyState==3 || xmlhttp.readyState==4)
				{
					document.getElementById("txtHint").innerHTML="Loading...";
				}
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
				}
			}
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
		}
	</script>
</head>
<body>

	<form>
		Select a User:
		<select name="users" onchange="showUser(this.value)">
			<option value="1">A</option>
			<option value="2">B/option>
			<option value="3">C</option>
			<option value="4">D</option>
		</select>
	</form>
	<br />
	<div id="txtHint">
		<b>Person info will be listed here.</b>
	</div>

</body>
</html> 


getuser.php

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'username', 'password');


if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("[dbname]", $con);

$sql="SELECT * FROM [table_name] WHERE login = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Maths</th>
<th>Physics</th>
<th>Chemistry</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['sname'] . "</td>";
  echo "<td>" . $row['fname'] . "</td>";
  echo "<td>" . $row['maths'] . "</td>";
  echo "<td>" . $row['physics'] . "</td>";
  echo "<td>" . $row['chemistry'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>
scrappedcola 11 Posting Whiz in Training

If you set an on change event for the fields that you want to affect the totals at the bottom you can detect when the user makes a change in the input boxes and then update the totals field (and also make an ajax call to update the db). An input with an onchange event could look like this:

<input type='text' onchange='handleOnTextUpdate(this)' id='someIdHere' />
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.