PeanutsMonkey
PeanutsMonkey

Reputation: 7095

Why is that !isset does not seem to work?

I am new to the world of PHP and have put together a form that multiplies an entered value. However when I attempt to validate if a person has not entered any values to return an error message, it does display the message. My code below. Appreciate if you could also suggest improvements.

<?php

$counter = 0;

if(isset($_POST["submit"])) {
    $start = $_POST["start"];
    $end = $_POST["end"];
    $multiply = $_POST["multiplication"];

// if($_POST["start"] == "" && $_POST["end"] == "" && $_POST["multiplication"] == "") {
    // print "Please enter some values";
// }

if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
    print "Please enter some values";

}

// for($start;$start<$end;$start++) {
    // $counter = $counter +1;
    // $multiplication = $counter * $multiply;
    // print "$counter <br />";
    // print "$counter multiplied by $multiply = $multiplication <br />";

// }

}

?>
<html>
<head>
    <title>Sample Multiplication</title>
</head>
<body>
    <form name="multiply" method="post" action="multiplication_sample.php">
        <input type="text" name="start" value="<?php if(isset($_POST["start"])) { print $start; }  ?>">
        <input type="text" name="end" value="<?php if(isset($_POST["end"])) { print $end; } ?>">
        <input type="text" name="multiplication" value="<?php if(isset($_POST["multiplication"])) { print $multiply; } ?>">
        <input type="submit" name="submit" value="submit">
    </form>

<?php

if(isset($_POST["submit"])) {

for($start;$start<$end;$start++) {
    $counter = $counter + 1;
    $multiplication = $counter * $multiply;
    print "$counter multiplied by $multiply = $multiplication <br />";
}

}

?>
</body>
</html>

Upvotes: 1

Views: 3541

Answers (4)

KingCrunch
KingCrunch

Reputation: 131921

I hope, I understand you right. It is

if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
    print "Please enter some values";
}

that works not as expected? It seems, that you assume an empty string means, that nothing is set, what is not true.

$x = "";
isset($x); // true

Use empty() or just $_POST['start'] == '' instead.

Upvotes: 0

Julien Roncaglia
Julien Roncaglia

Reputation: 17837

If the fields aren't defined your code will print your message in the html before the <html> tag appears. Most browsers won't display it or display it in an unexpected place.

You should move the message display somewhere in the html where the user could see it.

And as other pointed out, except on the first call of the page the fields will have an empty value but still exists (and so isset will return TRUE)

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 401022

When the form is submitted, the content of the input fields is sent to the server.

If those input fields are empty, the server gets an empty string for each input -- but it gets something ; so, the $_POST["start"], $_POST["end"], $_POST["multiplication"] items are set -- even if they only contain empty strings.

You could check :

  • If the fields contain an empty string : if ($_POST["start"] === '')
  • Or if if contains only blank spaces : if (trim($_POST["start"]) === '')
  • Or if they are empty : if (empty($_POST["start"]))

Upvotes: 2

Mitch Dempsey
Mitch Dempsey

Reputation: 39899

I think that isset will make sure a variable is not NULL, however, "blank" is not the same as null. If you submit a form with blank values, the variable is still being set, it is just empty.

Upvotes: 7

Related Questions