drhe80011 2017-08-17 06:53
浏览 23
已采纳

PHP:Echo没有从表单上处理POST方法

I am a bit new to PHP and I'm having a bit of difficulties here and there. I am developing a form and wish to display an error box if any of the fields are empty when the 'Submit' Button is pressed. I've tried the following code but the echo is still not appearing. Any suggestions ?

Form Code:

<div style="padding-top:40px">
    <div style="text-center; padding-right:25%; padding-left:25%">
        <div class="form-area">  
            <form role="form" method="$_POST" action="searchEmployee.php">
            <br style="clear:both">
                <h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3>
                <div class="form-group">
                    <label>Name:</label>
                    <input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
                </div>
                <div class="form-group">
                     <label>Surname:</label>
                    <input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
                </div>
                <div class="form-group">  
                    <label>ID Card:</label>
                    <input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
                </div>
                <div class="form-group">  
                    <label>Visitor Card Number:</label>
                    <input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
                </div>
                <div class="form-group">  
                    <intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
                </div>
            </form>
        </div>
    </div>
</div>

PHP Code:

<?php
        if (isset($_POST['submit'])) {
            $required = array('name', 'surname', 'ID', 'visitorCard');

            // Loop over field names, make sure each one exists and is not empty
            $error = false;
            foreach($required as $field) {
                if (empty($_POST[$field])) {
                $error = true;
                }
            }

            if ($error) {
                echo "All fields are required.";
            }
        }
        ?>
  • 写回答

7条回答 默认 最新

  • dourun2990 2017-08-17 07:00
    关注

    Here are few mistakes:

    1. You are using method as $_POST in your form attribute, It should be only POST.
    2. Your form will not submit because your button is not a submit type. it should

      <input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor" />
      

      Or

      <button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图