Jump to content

Not working!


ZeroShade

Recommended Posts

I'm having a problem with my page... basically the targetDiv is suppose to say hi when I pass the guess value from the text field. Anybody see any problems?

<!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");			}						function getData(data, targetDiv)			{				if (XMLHttpRequestObject)				{					var obj = document.getElementById(targetDiv);					XMLHttpRequestObject.open("GET", data);										XMLHttpRequestObject.onreadystatechange = function()					{						if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)						{							// Ready state is complete and status is 200.						}					}										obj.innerHTML = "<?php if ($select){ echo 'hi'; } mysql_close($dbh); ?>";				}			}		</script>	</head>	<body>		<?php 			$username = "username";			$password = "password";			$hostname = "localhost";						$dbh = mysql_connect($hostname, $username, $password)				or die("Unable to connect to MySQL");						mysql_select_db ("RandomNumber", $dbh);						srand((double)microtime()*1000000);			$rand = rand(0, 5);						mysql_query("INSERT INTO number (Number) VALUES ($rand)");			$select = mysql_query('SELECT COUNT( * ) AS `Rows` , `Number` FROM `number`;						//mysql_query("DELETE FROM `number` WHERE `number`.`Number` = $rand LIMIT 1");			//mysql_close($dbh);		?>		<h1>Pick Your Random Number</h1>		<p />Please insert a number from 0 to 5.		<form>			<input type="text" value="guess" id="guess" onchange="getData('guess', 'targetDiv')" /> 		</form>		<div id="targetDiv">			<p />		</div>	</body></html>

Link to comment
Share on other sites

You can't put PHP code in Javascript like that. The PHP gets executed first. That means that the very first line of code on that page that gets executed is this one:<?php if ($select){ echo 'hi'; } mysql_close($dbh); ?>You're probably getting an error that $dbh isn't a valid connection resource, since you call mysql_connect after that.

Link to comment
Share on other sites

What do you suggest I should do to create the random number in php... and then using ajax figure out if the user puts in the guessed number in the textbox? I know I could do it in javascript... but that would defeat the purpose of what I'm trying to do.

Link to comment
Share on other sites

Write the encoded number into a Javascript variable using PHP. Get Javascript to encode the person's answer the same way. If the encoded numbers match, then they entered the right number. Algorithms that would do that would be something like SHA1 or MD5, I've found a Javascript implementation of SHA1 before.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...