Jump to content

Random Numbers


user4fun
 Share

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

Edited by user4fun
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???

Edited by user4fun
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.

Edited by Ingolme
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...