user4592091
user4592091

Reputation:

!empty() Parameter Misformatted?

I am building a form. I have set it so that when the submit button has been pressed, an HTML page with the data shows up. If submit has not been clicked, the form is displayed.

My issue is that when I add the !empty() parameters to if((isset($_POST['submit']), my PHP page shows no content whatsoever. I added the !empty() parameters to check if the submit button has been clicked AND all form fields are not empty, and form output is false. I'm thinking the formatting of the !empty() validation is incorrect. The form runs fine with out && (!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['user_street']) && !empty($_POST['user_city']) && !empty($_POST['user_state']) && !empty($_POST['zip_code'])))

I've double checked and do not see any errors. Any insights to what might be the issue would be appreciated.

<?php

if((isset($_POST['submit']) && (!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['user_street']) && !empty($_POST['user_city']) && !empty($_POST['user_state']) && !empty($_POST['zip_code'])))  {
    $first = $_POST['first_name'];
    $last = $_POST['last_name'];
    $street = $_POST['user_street'];
    $city = $_POST['user_city'];
    $state = $_POST['user_state'];
    $zip = $_POST['zip_code'];
    $output_form = false; 

    }

    else {
        $output_form = true;
    }
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>

    <body>

    <?php
    if($output_form) {
    ?>

    <h1>PHP Sticky Form</h1>

    <form action="<?= $_SERVER[ 'PHP_SELF' ]; ?>" method="post" name="userForm">
        <p>First Name: <input type="text" id="first_name" name="first_name" value="<?=$first ?>" /></p>
        <p>Last Name: <input type="text" id="last_name" name="last_name" value="<?=$last ?>" /></p> 
        <p>Street Address: <input type="text" id="user_street" name="user_street" value="<?=$street ?>" /></p> 
        <p>City: <input type="text" id="user_city" name="user_city" value="<?=$city ?>" /></p> 
        <p>State: <input type="text" id="user_state" name="user_state" value="<?=$state ?>" /></p> 
        <p>Zip Code: <input type="text" id="zip_code" name="zip_code" value="<?=$zip ?>" /></p> 
        <p><input type="submit" name="submit" /></p>

    </form>

    <?php
     } else {
    ?>
        <h1>Your Address Information</h1>
        <p>NAME: <?= $first . ' ' . $last ?></p>
        <p>STREET:  <?= $street ?></p>
        <p>CITY, STATE, ZIP: <?= $city . ' ' . $state . ", " . $zip ?></p>

    <?php
     }
    ?>

    </body>

</html>

Upvotes: 1

Views: 74

Answers (2)

Ruben
Ruben

Reputation: 343

You are probably missing an extra ) after empty($_POST['zip_code']))).

Upvotes: 0

mdodong
mdodong

Reputation: 316

Your main problem there is that you are using && for checking values in $_POST,

if(isset($_POST['submit'] && (!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['user_street']) && !empty($_POST['user_city']) && ...

This means that if one value from the form (ex. $_POST['first_name'] is empty) then it will not show your output data.

You can do something like:

if(isset($_POST['submit'])){
  //if $_POST['first_name'] is not empty, get value, else get error
  $first = !empty($_POST['first_name']) ? $_POST['first_name'] : 'empty firstname';
  //check other values as well
  ...
  $output_form = false; 
}else{
  $output_form = true; 
}

Upvotes: 0

Related Questions