Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1389 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Javascript :

Number control

Thread title: Number control
Closed Thread    
    Thread tools Search this thread Display Modes  
02-25-2009, 07:36 PM
#1
Digi85 is offline Digi85
Status: I'm new around here
Join date: Feb 2009
Location:
Expertise:
Software:
 
Posts: 1
iTrader: 0 / 0%
 

Digi85 is on a distinguished road

  Old  Number control

Hey guys!

I'm working on a PHP script to generate a number between 1-100 and let people guess whether it's right or wrong. Now I would like to add a javascript to check if the number is between 1-100 or not. I'm kinda lost Could someone please give me a hint please? Would be really grateful!

Thanks in advance!

Love,
Diana


Below is my PHP code:


<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gissa numer</title>


</head>
<body>

<?php
$number=$_POST['number'];
if (!isset($number)) {
$number = rand(1, 100);
echo "Gissa ett tal mellan 1 och 100. ";
} else {

$guess = $_POST['guess'];
$n = $_POST['antal'];
if ($guess<1 || $guess>100) {
echo "Talet ska vara ellan 1-100! Gör om gör rätt!";
} else {


if ($guess>$number) {
$n++;
echo "Nope, guess lower<br />";
echo "You have made " . $n . " guess(es)";
} elseif ($guess<$number) {
$n++;
echo "Nope, guess higher<br />";
echo "You have made " . $n . " guess(es)";
} elseif ($guess==$number) {
$n++;
echo "Congratulations! You made " . $n . " guess(es)";
}
}
}
?>

<form action="<?=$_SERVER['PHP_SELF']; ?>" method="post">
<input name="guess" type="text"> <!--Vårat textfält-->

<?php echo "<input type='hidden' name='number' value='$number' /> "?>
<?php echo "<input type='hidden' name='antal' value='$n' /> "?>

<input name="knapp" type="submit" value="Guess">

</form>

</body>
</html>

03-06-2009, 12:10 AM
#2
Wildhoney is offline Wildhoney
Wildhoney's Avatar
Status: Request a custom title
Join date: Feb 2006
Location: Nottingham
Expertise:
Software:
 
Posts: 1,648
iTrader: 18 / 95%
 

Wildhoney is on a distinguished road

Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney

  Old

The following code should suffice for you. Set the ID properties on 2 objects:
  • Submit button to ID "pSubmit";
  • Input textbox to ID "pValue".

Then simply call TalkPHP_Init(); from the body onload.

Code:
function TalkPHP_Init()
{
	var pSubmit = document.getElementById('pSubmit');
	pSubmit.onclick = function() { return TalkPHP_Validate(); }
}

function TalkPHP_Validate()
{
	var iNumber = +document.getElementById('pValue').value;
	
	if (!iNumber || isNaN(iNumber))
	{
		alert('You have not entered a number.');
		return false;
	}
	
	if (iNumber < 1 || iNumber > 100)
	{
		alert('Your number must be between 1 and 100.');
		return false;
	}
	
	return true;
}

03-13-2009, 06:25 AM
#3
rkquest is offline rkquest
rkquest's Avatar
Status: I'm new around here
Join date: Nov 2008
Location:
Expertise:
Software:
 
Posts: 18
iTrader: 0 / 0%
 

rkquest is on a distinguished road

  Old

This will give you an idea..

Code:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gissa numer</title>

</head>
<body>

<form action="" method="post">
<input name="guess" type="text">
<input name="knapp" type="submit" value="Guess">

</form>

<script type="text/javascript">
	document.forms[0].knapp.onclick = function() {
		var theNumber = document.forms[0].guess.value;
		if (theNumber>=0 && theNumber<=100) {
			return true;
		} else {
			alert("You don't know how to follow instructions, moron!");
			return false;
		}
	}
</script>

</body>
</html>

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed