I am trying to create a dynamic system with check boxes for an web based game that I am trying to make using Ajax, PHP and MYsql
Is there a possible way to detect whenever a checkbox is selected/unselected, pull up information from the database and display information based on user's selection dynamically on the website without refreshing the entire page?
So far I have got this ..
My form is being rendered by PHP from a database Like so:
<form method="POST" action="">
<table cellspacing = 6 cellpadding = 2 >
<tr bgcolor=#dce3b5>
<td><b>Select </b></td>
<td><b>Country</b></td>
<td><b>Player Name</b></td>
<td><b>Points</b></td>
<td><b>Total Points</b></td>
<td><b>Season Rank</b></td>
<td><b>Cost</b></td>
</tr>
while($row = mysql_fetch_array($result))
{
$name = $row['Pname'];
$PHM = $row['PHM'];
$nation = $row['nation'];
echo '<tr>';
echo '<td>'.'<input type="checkbox" name="team[]" value= "'.$name.'" onClick ="showUser(this.value)">'.'</td>';
echo'<td>'.$nationf.'</td>';
echo '<td>'."$name".'</td>';
echo '<td>'."$row[points]".'</td>';
echo '<td>'."$row[tpoints]".'</td>';
echo '<td>'."$row[seriesrank]".'</td>';
echo '<td>'."$row[PHM]".'</td>';
echo '</tr>';
}
I am detecting whenever the check box is being clicked with
onClick ="showUser(this.value)"
In the code above.
MY javascript component to this code is :
<script type="text/javascript">
var xmlhttp;
function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getuser.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
</script>
The php file .... getuser.php works like this:
$q=$_GET["q"];
// connecting to database and stuff...
$temp = mysql_query("SELECT PHM FROM current WHERE Pname = '$q' ");
$row = mysql_fetch_assoc($temp);
$money =$row['PHM'];
echo $money;
and then I am displaying the dynamic element with this
<div id="txtHint"><b>Changing element goes here.</b></div>
Basically, a certain amount of money is associated with a name in my database and I am selecting the names with a check box
eg: Bob ----> 100 Rob ----> 200 Nancy --> 300 Ron ----> 50 Jack ----> 30
If I check Bob and Rob, The dynamic element should say 300, If I uncheck Rob, it should change to 100 without me pressing the submit button.
As of now, it displays the money associated with the last check box I clicked on... I have no idea how to detect all the check boxes that are checked.
If anyone could help me out, it would be great... Thanks in advance :)