forms – $_POST Array in PHP file is Showing Empty Results-ThrowExceptions

Exception or error:

I am using a simple form created using bootstrap 4. I am using a php file to do a server validation but it is not receiving any post entries from the form.

Here is my form which has a client-side validation that can be temporarily disabled to by removing “required” form the input field to test the server-side validation. The server-side validation is not working however because it uses $_POST which is not showing or retaining any incoming values the from.

Here is my form:

<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);

?>
How to solve:

For element ID’s are not passed by the POST method. You need to set the name of your form elements

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

Answer´╝Ü

You forgot to put the “name” property for your inputs.

Leave a Reply

Your email address will not be published. Required fields are marked *