clemkoa clemkoa - 8 months ago 55
PHP Question

PHP isset not working in POST method

I tried to make a form with a post method, and i want to check if the fields are filled with isset(). But even if the fields are empty, the isset returns true.

Here is a simplified code: (name of the page: test.php, the post method is directed to itself)

if (isset($_POST['test'])) {
echo 'field set';


<form method="post" action="test.php">
<input type="text" name="test"/>
<input type="submit" value="Submit"/>

When you submit, it always echoes 'field set', even if you didn't enter anything in the "test" input.

Thanks for your answers !


So check whether $_POST['test'] is empty or not and try like this

if (isset($_POST['test']) && trim($_POST['test']) != '') {
    echo 'field set';

You can also use empty,but Note that "0" is also "empty" and along with it its better to trim the inputs to remove spaces. Another way is to check the length of input using strlen

if (isset($_POST['test']) && trim(strlen($_POST['test'])) > 0) {
    echo 'field set';