PUG
PUG

Reputation: 4472

how to stop my php page from continuing when field is empty

<?php 
if(isset($_POST['submit'])){ 
echo "wht the heck"; 
$avail = new CheckEmptyFields(); 
$avail->availtime=$_POST['available_time']; 
echo $avail->chkFieldAvailableTime(); 
} 
else { 
print "<font color='red'>*</font>Printing else"; 
} 
?>

in my page but when i press continue the page goes on next page while field is empty.

beacuse i see "printing else" on the page even b4 i press continue so the script ran before while it should run when the user presses continue button.

Upvotes: 0

Views: 419

Answers (3)

wyqydsyq
wyqydsyq

Reputation: 2020

try replacing the isset() with !empty(), it could be happening due to $_POST['submit'] being SET, but having no value. empty() returns true for, as the name describes, any type that is empty, see: http://php.net/manual/en/function.empty.php

So basically isset() will return false only if the variable is not set, but !empty() will return false even if the variable is set but considered empty (set to FALSE, NULL, 0 .etc). Give it a try.

Upvotes: 0

Marc B
Marc B

Reputation: 360702

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if (isset($_POST['submit'])) {
      ... yada yada yada ...
   } else {
      echo '<font yada yada yada';
   }
}

With this, the isset will ONLY be checked if the page was actually requested via a POST hit. If it's loaded via GET or any other method, that chunk of code simply won't execute at all.

Upvotes: 2

Zirak
Zirak

Reputation: 39808

The else statement is executed if $_POST['submit'] is not set. On page load (before user clicks submit), that is the case.

Edit: Now that I think about it, did you put the two in different pages? What's form's action attribute?

Upvotes: 0

Related Questions