Jump to content

External script with ajax


ZeroShade

Recommended Posts

I need help... not that kinda help either. I'm playing with php a bit... and what I did was create a separate file that deals with creating a random number and storing it in a database as well. Then I basically need that $select variable returned so that I can use it in my javascript. Can anybody help me out?

<?php   $username = "Martin";  $password = "Legends";  $hostname = "localhost";  $dbh = mysql_connect($hostname, $username, $password)	or die("Unable to connect to database.");  mysql_select_db ("RandomNumber", $dbh);  srand((double)microtime()*1000000);  $rand = rand(1, 5);  $row = mysql_query('SELECT * FROM `number`');  $result = mysql_num_rows($row);  if ($result == "0")  {	mysql_query("INSERT INTO number (Number) VALUES ($rand)");  }  else  {	mysql_query("UPDATE number SET Number = '$rand'");  }  $select = mysql_query('SELECT COUNT( * ) AS `Rows` , `Number` FROM `number`');  mysql_close($dbh);    echo $select;?>

var rand should equal what $select is in the php file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >	<head>		<title>Guess My Number!</title>		<script language="javascript" type="text/javascript">			var XMLHttpRequestObject = false;						if (window.XMLHttpRequest)			{				XMLHttpRequestObject = new XMLHttpRequest();			}			else if (window.ActiveXObject)			{				XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");			}			else			{				alert("Your browser does not support AJAX.");			}						function getRandomNumber()			{				var randNum = document.getElementById("guess").value;				var url = "rand.php?guess=" + escape(guess);				url = url + "?dummy=" + new Date().getTime();				request.open("GET", url, true);				request.onreadystatechange = check;				request.send(null);			}						function check()			{				if (XMLHttpRequestObject)				{					if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)					{						var rand = 						var guess = document.getElementById("guess").value;														if (!isNaN(guess))						{			 							if (rand == guess)							{								alert("You guessed the number!");								location.reload(true);							}							else if (guess > 5 || guess < 1)							{								alert("Please guess a number between 1 and 5.");								document.getElementById("guess").value = "";							}							else if (rand > guess)							{								alert("Guess higher...");								document.getElementById("guess").value = "";							}							else if (rand < guess)							{								alert("Guess lower...");								document.getElementById("guess").value = "";							}						}						else						{							alert("Please insert a valid number");							document.getElementById("guess").value = "";						}   					}				}			}		</script>	</head>	<body style="background-color:#000000; color:#ffffff;">		<div style="text-align:center;">			<h1>Pick Your Random Number</h1>		</div>		<p />Please insert a number from 1 to 5.		<form method="post">			<input type="text" id="guess" onkeyup="check()" /> 		</form>	</body></html>

Link to comment
Share on other sites

mysql_query returns a MySQL connection resource, that's what you're trying to send to Javascript. Javascript can't use a PHP MySQL connection resource. You need to use one of the mysql functions to get the field value from the result and display that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...