dsfg3241 2015-05-09 21:32
浏览 148

!empty()参数格式错误?

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>
  • 写回答

2条回答 默认 最新

  • dongmiao520892 2015-05-09 22:05
    关注

    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; 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效