Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67527

Request assistance as to why my form will not validate properly

$
0
0

I have scoured this site for hints and tips as I re-learn PHP form validation.

The form created (using BS4) and code below are supposed to work together. They have been pieced together using information found here and W3 and from dissecting a few of the free templates out there.

It seemed so simple to work this out. But, I keep receiving incorrect validation from all fields in the form.

name - must be alpha only - validation never displays error when intentionally entering non-alpha chars

email - must comply with the input type 'email' - validation always catches this one and for some reason displays a tooltip... refusing to submit the form. I did not intentionally code for the tooltip and, can't see how to get rid of it OR duplicate it on other fields.

subject uses the input type 'subject' - But, the absence of a subject does not trigger the error display after submitting.

message - just has to be something - But, validation on the pre-submit status already displays the error condition.

When all data is entered, properly formatted, submitting the form erases all the data and, displays errors in every field.

I am not particularly good at hard coding. I own that. But, I think that I follow directions fairly well. And, this just isn't working as expected.

If anyone can point me in the right direction to get validation working on this form, I would appreciate it.

Thank you.

The page is HTML5, PHP 7, BS4.

<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>" class="p-5 bg-white"><div class="mb-3 border-bottom"><h2 class="h4 text-black mb-5">Send us a Message</h2><p>Note that All Fields are Required</p></div><div class="row form-group"><div class="col-md-12 mb-3 mb-md-0"><label class="text-black" for="name"><font color="red" size="+2">* </font>Your Name</label><input type="text" id="name" class="form-control" value="<?php echo $name;?>"><span class="list-group-item-danger"><?php echo $nameErr;?></span></div></div><div class="row form-group"><div class="col-md-12"><label class="text-black" for="email"><font color="red" size="+2">* </font>Your Email Address</label><input type="email" id="email" class="form-control" value="<?php echo $email;?>"><span class="list-group-item-danger"><?php echo $emailErr;?></span></div></div><div class="row form-group"><div class="col-md-12"><label class="text-black" for="subject"><font color="red" size="+2">* </font>Subject</label><input type="subject" id="subject" class="form-control" value="<?php echo $subject;?>"><span class="list-group-item-danger"><?php echo $subjectErr;?></span></div></div><div class="row form-group"><div class="col-md-12"><label class="text-black" for="message"><font color="red" size="+2">* </font>Your Message</label><textarea name="message" id="message" cols="30" rows="7" class="form-control" placeholder="Your message here..." value="<?php echo $message;?>"></textarea><span class="list-group-item-danger"><?php echo $messageErr;?></span></div></div><div class="row form-group"><div align="center" class="col-md-12"><input type="submit" value="Send Message" class="btn btn-primary btn-md text-white"></div></div></form>
<!-- PHP Form validation script --> 

<?
// declare and empty local variables
$nameErr = $messageErr = $emailErr = $subjectErr = "";
$name = $message = $email = $subject =  "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  if (empty($_POST["name"])) {
    $nameErr = "Name is required and can only contain alphabet text";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Roman Alphabet and Spaces Only";
    }
  }


  if (empty($_POST["email"])) {
    $emailErr = "Email address is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Please Correct Invalid Email Address";
    }
  }

   if (empty($_POST["subject"])) {
    $subjectErr = "Subject Cannot be Empty";
  } else {
    $subject = test_input($_POST["subject"]);
    }    
  } 

  if (empty($_POST["message"])) {
    $messageErr = "Message Cannot be Empty";
  } else {
    $message = test_input($_POST["message"]);
  }

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<!-- END PHP Form Validation script -->


Viewing all articles
Browse latest Browse all 67527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>