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
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";
}
}

?>

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.