Jump to content

Need Help with PHP Form Input


jonesmeister

Recommended Posts

Hi, everyone. I need help with a PHP project on which I'm currently working.

 

I need to create a form which does the following:

1) When you insert a negative number, it echoes: "Please insert a positive number."

2) When you insert a number greater than 1000, it echoes: "Please insert a number less than 1000."

3) When you insert anything else that's not a number (ie: a letter), it echoes: "Please insert a valid number."

4) If it doesn't do any of the above, it'll take the number that you entered and loop "Hello World" as many times as that number.

 

The code that I have so far looks something like below. (I had to type it from the top of my head.)

 


<?php

 

<form action="hwpositive.php" method="post">

Enter number: <input type="text" name="number" />

</form>

 

$input = $_POST["number"];

 

if ($input<0)

{ echo "Please insert a positive number."; }

else if ($input>1000)

{ echo "Please insert a number less than 1000."; }

else if ($input!=is_numeric)

{ echo "Please insert a valid number."; }

else {

for {$i;  $i<=$_POST["number"]; $i++;}

}

 

?>


 

I can get this code to do the first three tasks listed above, but not the last. I'm a high school Programming 12 student, and this is my first year of learning PHP coding. Please help me out. Would it be better to use the case-switch option? I'm not too familiar on how to use it.

 

Thank you in advance! I appreciate your help greatly.

Link to comment
https://forums.phpfreaks.com/topic/220197-need-help-with-php-form-input/
Share on other sites

a switch statement would be cleaner.

<?php

<form action="hwpositive.php" method="post">
Enter number: <input type="text" name="number" />
</form>

$input = $_POST["number"];

if ($input<0)
{ echo "Please insert a positive number."; }
else if ($input>1000)
{ echo "Please insert a number less than 1000."; }
//else if ($input!=is_numeric) //need to put the variable is the function as follows, the ! reverses the function, so !is_numeric is if its false, and is_numeric is if its true.
else if (!is_numeric($input))
{ echo "Please insert a valid number."; }
else {
//for loop expressions must be enclosed in ( expr1; expr2; expr3 ), then the code to execute on each iteration in curly brackets
for ($i;  $i<=$_POST["number"]; $i++
{
echo "Hello World";
}
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.