Chanter Chanter - 2 years ago 88
PHP Question

PHP : Echo not working on POST method from form

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">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
<div class="form-group">
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
<div class="form-group">
<label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
<div class="form-group">
<label>Visitor Card Number:</label>
<input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
<div class="form-group">
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>

PHP Code:

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.";

Answer Source

You need to make 2 change as below

  1. Change button type like this

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

  1. Change Form method

<form role="form" method="POST" action="searchEmployee.php">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download