Jump to content

Random Numbers


user4fun

Recommended Posts

Genrate a random number and test it in the same page. I could use some help.Thank youguess_number.phpI need the Generate Random_Function to be called as soon as the page is loaded.

Function Generate_Random (){$random_number = rand(50,5000); //need to make $random_number global//more stuff}Function Validate($passed_val){	if ($passed_Val== $random_number)		{		// more stuff		echo "You got it right";		}				else		{		//more stuff				echo "You got it WRONG";		}}?><form method="POST" action="<?=$_SERVER['PHP_SELF']?>">  I want to guess  <input type="text" name="guessed_number">  <br>  <input type="submit" value="Submit" name="try_this" onclick="Validate()">  I need to pass the guessed_number entry  <input type="submit" value="Generate a new number" name="get_new" onClick="Generate_Random()"></form>

I know this is not complete, but I will take any help that I can get.Thank you

Link to comment
Share on other sites

You can't call a PHP function from HTML or Javascript.You're going to have to make the equivalent of those functions in Javascript.

Link to comment
Share on other sites

PHP is C-based, keywords are lowercase, and remember naming conventions!. You can access the posted data using the $_POST superglobal array. Why do you need functions anyway?

Link to comment
Share on other sites

The onclick event of HTML elements only calls Javascript functions.If you want to do it as PHP, you'll have to send form data to a PHP page to be processed.PHP runs only before the page loads. Once the page has loaded PHP doesn't do anything else.

Link to comment
Share on other sites

How about this.Deciding what the user want to do and sending the write "User Action" to the validate number.php page.

<script type="text/javascript">var http = false;if(navigator.appName == "Microsoft Internet Explorer"){http = new ActiveXObject("Microsoft.XMLHTTP");} else {  http = new XMLHttpRequest();}function UserAction(Btn_Name){if(Btn_Name == "Submit")	{	var objValue = document.myform.guessed_number.value;	var params = "?action=number_submitted&user_number=" + objValue;	}if(Btn_Name == "Need_New")   {   var params = "?action=need_new_number";   }http.open("GET",validate_number.php" + params",true);http.onreadystatechange=function()   {   if(http.readyState==4)   {	document.GetElementById.My_Answer.value=http.responseText;   }}xmlhttp.send(null);}</script></head><body><form method="POST"  name = "myform" action="<?=$_SERVER['PHP_SELF']?>"><div id="My_Answer"><br>  I want to guess<input type="text" name="guessed_number">  <br>  <input type="submit" value="Submit" name="try_this" onclick="UserAction(Submit)">  <input type="submit" value="Generate a new number" name="get_new" onClick="UserAction(Need_New)"></form>

validate.number.php

$user_action = $_GET['action'];if ($user_action == "number_submitted"){$user_number = $_GET['user_number'];echo "You have submitted a number " .$user_number;}if ($user_action == "need_new_number"){echo "You have requested a new number";}

When I submit my form, I dont get a result. It seems like the form jsut refreshes itself???

Link to comment
Share on other sites

When using AJAX, you're not supposed to use a <form> tag or submit buttons. Just use normal buttons.However, if you wanted to avoid Javascript, AJAX is Javascript, and if Javascript is disabled it's not going to work.

Link to comment
Share on other sites

That's exactly what the submit button is supposed to do. In the previous post, I told you not to use a form tag or a submit button. Use a normal button.You also have a few mistakes in your Javascript.

I want to guess<input type="text" id="guessed_number"><br><button type="button" onclick="UserAction('Submit')">Submit</button><button type="button" onclick="UserActon('Need_New')">Generate a new number</button>

To keep up with the times, instead of using document.myform.guessed_number, which is an old fashioned method that's probably going to lose support eventually, use document.getElementById() so that you won't need a useless form to access the element.

var objValue = document.getElementById("guessed_number").value;

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...