I'm sorry I'm new, but I have racked my brain on this:
I am writing an interactive web game as sort of a learning project. I came across this problem with a button, so I wrote up a simple code demonstrating what I am trying to do... but can't.
here is the main page:
CODE
<html>
<head>
</head>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.GetElementById("dollars").value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","simple.php",true);
xmlHttp.send(null);
}
</script>
<input type="button" onclick="AjaxFunction()" value="Click here to get 1$"/>
</br>
You have $<span id="dollars"></span>
</body>
</html>
here is the other php page that i referenced using the xmlhttp:
CODE
<?php
$con = mysql_connect("localhost","anyone","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("money", $con);
mysql_query("update tabletest set dollars = dollars+1");
mysql_close($con);
$con = mysql_connect("localhost","anyone","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("money", $con);
$result = mysql_query("select * from tabletest");
while($row = mysql_fetch_array($result))
{
echo $row['dollars'];
}
mysql_close($con);
?>
of course I have a mysql database already set up called 'money' with a table called 'tabletest' with a datafield called 'dollars' that is an int, and there is already one row of data in the table. I have no problem accessing/altering my database on other pages I have made, and php is also working fine on my server. I know my code is flawed, I just can't figure out how. I am trying to make it so that when the button is pressed it checks the database to see how much money you have, adds one to it, and displays how much money you now have in the span element.
I am just doing this as a fun challenge, but it isn't so much fun anymore when something as simple as this bogs me down... please help.