Jump to content

[SOLVED] Simple XP form fault


scriptham

Recommended Posts

Hi

 

I am struggling to get the following script to work. As you can probably guess it is from a book. I am working towards a modest website and this is my starting point.

 

The basic idea is that the user guesses the num_to_guess entering their guess in the html form and submitting it. The php then checks it against the preset value and displays a higher, direct hit or lower type message.

 

When run the form appears but in use the message does not change, can anyone spot the error please.

 

I am using the Abyss webserver in winXP it is set up to run php and some basic php has already worked so I believe the set up is correct.

 

<?php
$num_to_guess = 42;
if (!isset($_Post[guess]))
{
$message= "Welcome to the guessing machine";
}
else if ($_Post[guess] > $_num_to_guess)
{
$message= "$_Post[guess] is too large. Try a smaller number.";
}
else if ($_Post[guess] < $_num_to_guess)
{
$message= "$_Post[guess] is too small. Try a larger number.";
}
else
{
$message= "Well Done";
}

//unset ($_Post[guess]);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Page title</title>
</head>
<body>



<form action="<?php echo $_SERVER[php_SELF] ?>" method="POST">
<p>Type your guess here: <input type="text" name="guess"/></p>
<p><input type="submit" value="submit your guess"/></p>
</form>


<?php
<h1>
echo $message
</h1>
?>


</body>
</html>

 

I look forward to hearing from you.

Many thanks

ScriptHam

Link to comment
Share on other sites

<?php
$num_to_guess = 42;

if (!isset($_POST['guess']) or empty($_POST['guess'])){
$message= "Welcome to the guessing machine";
}else if ($_POST['guess'] > $num_to_guess){
$message= $_POST['guess'] ." is too large. Try a smaller number.";
}else if ($_POST[guess] < $num_to_guess){
$message= $_POST['guess'] ." is too small. Try a larger number.";
}else{
$message= "Well Done";
}

//unset ($_Post[guess]);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Page title</title>
</head>
<body>



<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>Type your guess here: <input type="text" name="guess"/></p>
<p><input type="submit" value="submit your guess"/></p>
</form>


<?php
echo "<h1>";
echo $message;
echo "</h1>";
?>


</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.