School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,082 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,263 people online right now. Registration is fast and FREE... Join Now!




update database button not working

 

update database button not working, ajax, php, mysql, html

Lilly LaSombra

1 Jun, 2008 - 06:22 PM
Post #1

New D.I.C Head
*

Joined: 19 May, 2008
Posts: 4

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.

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Update Database Button Not Working

1 Jun, 2008 - 06:51 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first of all, remember that javascript is case sensitive. Your button is calling "AjaxFunction" but you named the function "ajaxFunction". So change the "A" in one spot or the other so they match. Because right now it is not calling your function.

Second you use "GetElementById" when it is suppose to be "getElementById". Notice the lower case g.

Lastly you need to use "innerHTML" not "value" of the getElementById returned object.

CODE

document.getElementById("dollars").innerHTML = xmlHttp.responseText;


These three changes should get you up and running assuming your simple.php is all correct and returning content. Just to prove it is working try changing "simple.php" to "http://www.dreamincode.net/index.php" and see if your span tag fills up with data.

You should find it working fine. Enjoy!

"At DIC we be span replacing code ninjas... one of our famous ninja ancestors was named ajax. True story!" decap.gif

This post has been edited by Martyr2: 1 Jun, 2008 - 06:53 PM
User is offlineProfile CardPM
+Quote Post

Lilly LaSombra

RE: Update Database Button Not Working

1 Jun, 2008 - 07:11 PM
Post #3

New D.I.C Head
*

Joined: 19 May, 2008
Posts: 4

Thanks loads! I thought it might have something to do with that getElement line... i never really did understand that line, I was just trying to modify it from other code... I understand the getElement part now, just not the innerHTML part. why does that belong there? It works now, but I don't understand why...

I'm happy with it working, but if anyone cares to explain to me why I would appreciate it. I've searched tutorials for these sort of lines, but I haven't found good examples that help me understand it.
Thanks
User is offlineProfile CardPM
+Quote Post

Lilly LaSombra

RE: Update Database Button Not Working

2 Jun, 2008 - 05:53 AM
Post #4

New D.I.C Head
*

Joined: 19 May, 2008
Posts: 4

Nevermind about explaining... I think I get it now smile.gif I just thought I would also add incase someone else reads this with a similar problem, that the php page I referenced needed a timestamp appended to the url in order to prevent pulling a cached page thus now instead of pressing the button only once, you can press the button to your hearts content and watch the dollars add up! On with the code! Thanks
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:36AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month