Jump to content

Placing limits on variable input, and displacing results once variable is entere


newtophp2010

Recommended Posts

Hello all. I am very new to PHP, and I am not sure where to look or what I'm looking for in my current assignment.

 

My task is to take in two numbers between 0-100. Once I take in that number, it should state beside it "The __ was accepted."

The program should not accept any numbers greater than 100 or any characters.

 

Once I do this, I must take a second number and do a similar thing.

 

Finally, I must have a statement show up at the bottom stating which number is greater.

 

Essentially, I need help in determining what I should use to place parameters, and how I can keep the program from echo ing any statement until input has been taken and tested for parameters.

 

Any help you can provide will be greatly appreciated!

Link to comment
Share on other sites

here's a dodgy way to get the job done, I'll leave it up to you to either learn from it, or modify it. Take note of the links I've added below. I've left out a few key things on purpose, as it's an assignment meaning you need to learn.

<?php
if(isset($_POST['num1']) && isset($_POST['num2'])) { // check if Form values are entered (form submitted)
  $num1 = strip_tags($_POST['num1']); // assign number 1 to a variable
  $num2 = strip_tags($_POST['num2']); // assign number 2 to a variable
  
if($num1 >= 0 && $num1 <= 100) { // check number 1 is 0-100
   echo "$num1 has been accepted.<br />";
   $r = 1;
} else {
   echo "$num1 has not been accepted.<br />";
}
if($num2 >= 0 && $num2 <= 100) { // check number 2 is 0-100
   echo "$num2 has been accepted.<br />";
   $t = 1;
} else {
   echo "$num2 has not been accepted.<br />";
}

if(isset($t) && isset($r)) {
   if($num1 > $num2) {
     echo 'number 1 is greater than number 2';
   }
   elseif($num1 < $num2) {
     echo 'number 2 is greater than number 1';
   }
}  

} else { // if form has not been submitted or missing input, display the form...
   echo <<<FORM
<form action='' method='post'>
<input type='text' name='num1' />
<input type='text' name='num2' />
<input type='submit' />
</form>
FORM;
}
?>

 

Manual:

- strip_tags()

- isset()

- Heredoc

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.