PHP文件中的$ _POST数组显示空结果

时间:2019-02-01 18:29:06

标签: php forms post action

我正在使用通过引导程序4创建的简单表单。我正在使用php文件进行服务器验证,但它没有从表单接收任何帖子条目。

这是我的表单,其中具有客户端验证,可以通过从输入字段中删除“必需”来暂时禁用该验证,以测试服务器端验证。但是,服务器端验证无法正常工作,因为它使用了$ _POST,该$ _POST不会显示或保留from的任何传入值。

这是我的表格:

<form role="form" action="form_handling.php" method="post" id="elegantForm" class="needs-validation" novalidate>  

<!--  Name Group    -->   
         <div class="form-group">
    <label for="name">Full Name:</label>
    <input type="text" class="form-control" id="fullname" aria-describedby="nameHelp" placeholder="Enter full name" required>
        <div class="invalid-feedback">
          Please enter full name.
        </div>
  </div>

<!--  Email Group     -->    
        <div class="form-group">
             <label for="email">Email address:</label>
             <input type="email" class="form-control" id="emailaddress" aria-describedby="emailHelp" placeholder="Enter email" required>
        <div class="invalid-feedback">
          Please enter valid email address.
        </div>
        </div>

<!--  Telephone Group   -->   
         <div class="form-group">
              <label for="phone">Telephone:</label>
              <input type="tel" class="form-control" id="phoneNumber" aria-describedby="phonelHelp" placeholder="xxx-xxx-xxxx" required> 
        <div class="invalid-feedback">
          Please enter telephone number.
        </div>
         </div>

<!--  Telephone Group   -->   
         <div class="form-group">
              <label for="tel">Message:</label>
              <textarea type="textarea" class="form-control" id="messageBox" aria-describedby="messagelHelp" placeholder="Enter message here" maxlength="6000" rows="6" required></textarea>
             <div class="invalid-feedback">
          Please enter a message.
        </div>
          </div>

<!--      Submit Button   -->
       <button type="submit" class="btn btn-primary">Submit</button>
    </form>

----------------------------------------

And here is the PHP file:

<?php

$errors         = array();      // array to store validation errors
$data           = array();      // array to store incoming "post" data 

if (empty($_POST['name']))
        $errors['name'] = 'Name is required.';

    if (empty($_POST['email']))
        $errors['email'] = 'Email is required.';

    if (empty($_POST['phone']))
        $errors['phone'] = 'Phone is required.';

    if (empty($_POST['message']))
        $errors['message'] = 'Message is required.';

// return a response ===========================================================

    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    } else {


        // show a message of success and provide a true success variable
        $data['success'] = true;
        $data['message'] = 'Success!';
    }


print_r($_POST);
var_dump($_POST);

?>

在解释这里发生的事情时,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

对于元素ID,POST方法不会传递。您需要设置表单元素的名称

<input type="tel" class="form-control" name="phoneNumber" id="phoneNumber" aria-describedby="phonelHelp" placeholder="xxx-xxx-xxxx" required>

答案 1 :(得分:1)

您忘记为输入内容添加“名称”属性。