Hello I seem to have a small problem, I have just setup a Apahe server on my localhost so I can run PhP Files.
and I made some simple codes. but they don’t seem to work proberly, those used to work on my univeristy where I statred making them as they are my Practical Assignment. Here are the wronmg outputs
when I try to send the var from home.php to guess.php I get this output
Hello " .$name . "!" . " Try and guess the number dude!!!..."); ?> "); echo("A Number from 1-100 has been set. up for the challange?"); ?>
and ofc is wrong.
also I need some help to know how to check if the number given in guess.php is the same as the $x variable
home.php
[php]
Home Page
<?php
session_start();
session_register("login");
?>
Enter Registration
[/php]
guess.php
[php]
Guess The Number Dude
<?php
session_start();
$name = $_REQUEST['nm'];
echo("
Hello " .$name . "!" . " Try and guess the number dude!!!...");
?>
<?php
$x = 10;
echo (" ");
echo("A Number from 1-100 has been set. up for the challange?");
?>
Enter Your Number
[/php]
and correct.php
[php]
Correct
<?php
if ($x == 10) ;
echo("correct");
?>
[/php]
first page seems good
second and 3rd pages have some problems.
second page
try
[php]echo “
Hello, $_POST[name], Try and guess the number dude!!!..
”;
unset($_POST[‘nm’];[/php]
get rid of that request crap and since you’re reusing nm, it needs to be reset or zero’d out.
$x will never equal 10 because variables don’t typically transfer between pages. if you want to it on the 3rd page, it has to be done through a hidden input on the form
3rd page
now, to get the value of x so that $x will equal 10, do
$x = $_POST[‘x’]; ($x now equals 10)
change the php to
[php]<?php
$y = $_POST[‘x’]; // from the hidden input
$x = $_POST[‘nm’]; //from the text input
if ($x == 10) { // correct number
echo “X is Correct”;
} else { // incorrect number
echo “Wrong Number bud”;
}
if ($y == 10) { // correct number
echo “Y is Correct”;
} else { // incorrect number
echo “Wrong Number bud”;
}
?>[/php]
well that helped a lot with fixing the pages ty =)
but what I actually wanna do is to check on the second page if the number is correct. if not I want to display error msg and increment a value $count for example. and go to the 3rd page if he finds the correct number iis that possible ?
ok, then try something like this
[php]<?php
session_start();
if(!isset($ct)) {
// makes sure that $ct doesn’t get overwritten on the next submit
$ct = 0;
}
if(isset($_POST[‘submit’])) {
if($_POST[‘nm’] = 10) {
header(“Location: correct.php”);
exit;
} else {
$ct++;
if($ct != 5) {
$msg = “Nope, try again, you have “.5 - $ct.” tries left”;
} else {
$ct = 5;
$msg = “You are out of tries”;
}
}
}
?>
Guess The Number Dude
Hello $_POST[nm]! Try and guess the number dude!!!...
A Number from 1-100 has been set. U for the challange?
Enter Your Number
[/php]
correct.php
[code]
Correct
Your answer is correct!
[/code]
I didn’t know what you wanted to do with the counter, so i used it to give the person 5 tries, you can do anything with you with it.
Cool that helped me a lot again you are great ty
but I just have one minor problem that I can’t figure out. I am new to php so I can’t really tell what is the problem here
So well the following code should be correct I have edited a bit what you gave me to make it work how I need to but when I click on submit the second time it forgets the $name and it outputs one error msg. how can I fix that ? also for the $ct counter you have added I just want it to send it to the the correct.php form and say "Good you found the number with only : $ct tries "
[php]
Guess The Number Dude
<?php
session_start();
if(!isset($ct)) {
// makes sure that $ct doesn’t get overwritten on the next submit
$ct = 0;
}
if(isset($_POST[‘submit’])) {
if($_POST[‘num’] == 10) {
header(“Location: correct.php”);
exit;
} else {
$ct++;
if($_POST[‘num’] >= 50) {
echo(“You Have to try Smaller Numbers Bud…”);
} else {
$_POST[‘num’] <10 ;
echo(“Maybe you can Find it or not bur you seem to be close try smaller numbers”);
}
}
}
?>
<?php
$name = $_REQUEST['nm'];
echo("
Hello " .$name . "!" . " Try and guess the number dude!!!...");
?>
Enter Your Number