I have a php form, but it submits even if the required field is not filled out. Can anyone figure out why? I am not a php developer and am not sure why it isn't working. Please tell me where to add any code snippets you may provide as this is new to me. In order to prove I have tried, and know what I am talking about to some extent, I am only showing the code here which should be relevant to the issue.
<?php
require 'xxxxxxx.php';
$fnameErr = "";
if(isset($_POST['add'])) {
if (empty($_POST["fname"])) {
$fnameErr = "Name is required";
exit();
}else {
$fname = check_input($_POST['fname']);
}
$lname = check_input($_POST['lname']);
$phone = check_input($_POST['phone']);
$email = check_input($_POST['email']);
$sql = " INSERT INTO QuotesInfo ( fname, lname, phone, email, doservice, etype, rtype,
ptime, paddress, rtime, daddress, vtype, pcount, addetails, heardwhere )
VALUES ('".$_POST["fname"]."','".$_POST["lname"]."','".$_POST["phone"]."','
".$_POST["email"]."','".$_POST["doservice"]."','".$_POST["etype"]."','".$_POST["rtype"]."','".$_POST["ptime"]."','
".$_POST["paddress"]."','".$_POST["rtime"]."','".$_POST["daddress"]."','".$_POST["vtype"]."','".$_POST["pcount"]."','
".$_POST["addetails"]."','".$_POST["heardwhere"]."') ";
$msg= $fname . " " . $lname . "
" . $phone . "
" . $email . "
" . $doservice . "
" . $etype . "
" . $rtype . "
" . $ptime . "
" . $paddress . "
" . $rtime . "
" . $daddress . "
" . $vtype . "
" . $pcount . "
" . $addetails . "
" . implode(", ", $heardwhere);
$headers = "From: $from";
<form id="form1" name="form1" method = "post" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="fname">First Name</label><input type="text" name="fname" id="fname" />
<span class="error">* <?php echo $fnameErr;?></span>
<br class="clear" />
You have a bunch of syntax errors in your code. Here is a cleaned up version, please verify before you use it:
<?php
require 'xxxxxxx.php';
$fnameErr = "";
if(isset($_POST['add'])) {
if (empty($_POST["fname"])) {
$fnameErr = "Name is required";
exit();
} else {
$fname = check_input($_POST['fname']);
}
$lname = check_input($_POST['lname']);
$phone = check_input($_POST['phone']);
$email = check_input($_POST['email']);
$sql = "INSERT INTO QuotesInfo
( fname, lname, phone, email, doservice, etype, rtype, ptime, paddress, rtime, daddress, vtype, pcount, addetails, heardwhere )
VALUES
('".$_POST["fname"]."',
'".$_POST["lname"]."',
'".$_POST["phone"]."','
'".$_POST["email"]."',
'".$_POST["doservice"]."',
'".$_POST["etype"]."',
'".$_POST["rtype"]."',
'".$_POST["ptime"]."',
'".$_POST["paddress"]."',
'".$_POST["rtime"]."',
'".$_POST["daddress"]."',
'".$_POST["vtype"]."',
'".$_POST["pcount"]."',
'".$_POST["addetails"]."',
'".$_POST["heardwhere"]."')";
$msg= $fname . " " . $lname . "
" . $phone . "
" . $email . "
" . $doservice . "
" . $etype . "
" . $rtype . "
" . $ptime . "
" . $paddress . "
" . $rtime . "
" . $daddress . "
" . $vtype . "
" . $pcount . "
" . $addetails . "
" . implode(", ", $heardwhere);
$headers = "From: $from";
?>
<form id="form1" name="form1" method = "post" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="fname">First Name</label>
<input type="text" name="fname" id="fname" required />
<span class="error">* <?php echo $fnameErr;?></span>
<br class="clear" />
</form>
<?php } ?>
Regarding the required
validation, you need to add a required
attribute to the form element that is required.