Reputation:
i have a multiplication table which works perfectly, save for one tiny mishap. i need the message "invalid entry!" to ONLY appear when i submit an empty query via my input field. the problem lies in the message being there, staring me in the face upon first opening the table form in my localhost.
any ideas? thanks in advance.
the code:
<html>
<form method="post" action="php32-table.php">
<input type="text" name="num">
<input type="submit" value="get table">
</form>
</html>
<?php
$num = isset($_POST["num"]) ? $_POST["num"] : "";
if($num) {
for($i = 1; $i <= 10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
} else {
echo "invalid entry!";
}
?>
Upvotes: 0
Views: 58
Reputation: 9581
Num will always be set since it is a textfield. What you need to do is check if the field is empty. The code below should work.
<?php
if (isset($_POST["submit"])){
if(!empty(trim($_POST['num'])) {
$num = $_POST['num'];
for($i = 1; $i <= 10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
} else {
echo "invalid entry!";
}
}
?>
Upvotes: 1
Reputation:
You have to check the $_POST['submit'] is set or not
<html>
<form method="post" action="php32-table.php">
<input type="text" name="num">
<input type="submit" name='submit' value="get table">
</form>
</html>
<?php
if (isset($_POST["submit"])){
$num = isset($_POST["num"]) ? $_POST["num"] : "";
if($num) {
for($i = 1; $i <= 10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
} else {
echo "invalid entry!";
}
}
?>
Upvotes: 0
Reputation: 285
change your input type to number.
<input type="number" name="num">
Here the PHP code
if(isset($_POST["num"])){
$num = $_POST["num"];
if($num)
{
for($i = 1; $i <= 10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "invalid entry!";
}
}
?>
Upvotes: 0
Reputation: 2645
I removed the link to action for testing purposes, I also prefer personally to run the PHP for the form within the same file, so my example below is exactly how to do that. You can always refer to your original and place the php within the php32-table.php
file.
You can run this from the submission side by adding in name="submit"
like below:
<form method="post" action="">
<input type="text" name="num">
<input type="submit" value="get table" name="submit">
</form>
You can then check if the submit button has been pressed by using isset
and if it has been clicked then run the code between { } like below:
<?php
if (isset($_POST['submit']))
{
$num = isset($_POST["num"]) ? $_POST["num"] : "";
if ($num) {
for ($i = 1; $i <= 10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
} else {
echo "invalid entry!";
}
}
?>
to be honest, to keep it cleaner i'd remove: $num = isset($_POST["num"]) ? $_POST["num"] : "";
and simply change that line to:
if (!empty($_POST['num']))
{
// run code
} else {
// output error
}
Upvotes: 0
Reputation: 33186
You could check if the $_POST["num"]
variable is set. This way you can know if the form has been posted.
<?php
if (isset($_POST["num"]))
{
$num = isset($_POST["num"]) ? $_POST["num"] : "";
if ($num) {
for ($i = 1; $i <= 10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
} else {
echo "invalid entry!";
}
}
?>
Upvotes: 0