Jump to content

Form Validation


Go to solution Solved by Barand,

Recommended Posts

Hi. I'm new to PHP and have a problem with some form validation.

The code below is my contact form. It was working until I added a dropdown list and checkbox. For all the other fields I created an input variable using the 'name' attribute to reference the data entered into the input fields and the validation appeared to work and an email was sent to the appropriate email address.

For the checkbox field I have also created a 'name' attribute but don't know whether this is right way to confirm whether the box has been checked?

For the field with a dropdown selection I have used the option 'selected', but again, don't know whether this is right or not.

Now when I enter valid data into all the fields, select a value from the dropdown and check the checkbox, I get an error message stating that I need to fill in all the form fields. I am pretty sure it is to do with the way I have tried to pick up the data from dropdown list and the checkbox, but can't figure out what it is. 

Any help would be much appreciated.

Thanks in advance.

 

<?php echo file_get_contents('header.php'); ?>

<?php
    // Message Vars
    $msg = '';
    $msgClass = '';

  // Check to see if the form has been submitted
  if(filter_has_var(INPUT_POST, 'submit')) {
    //Input Data Variables
    $firstname = htmlspecialchars($_POST['firstname']);
    $lastname = htmlspecialchars($_POST['lastname']);
    $phonenumber = htmlspecialchars($_POST['phonenumber']);
    $email = htmlspecialchars($_POST['email']);
    $selected = htmlspecialchars($_POST['selected']);
    $message = htmlspecialchars($_POST['message']);
    $checkbox = htmlspecialchars($_POST['checkbox']);


    //Check required fields
    if(!empty($firstname) && !empty($lastname) && !empty($phonenumber) && !empty($email) && !empty($selected) && !empty($message) && !empty($checkbox)) {
      // If passed, check email address
      if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
        //If failed, Email address is not valid
        $msg = 'Please use a valid email address';
        $msgClass = 'alert-danger';
      } else {
        //Recipient email address and information to be sent to them
        $toEmail = 'hello@example.co.uk';
        $subject = 'Contact Request from  Website';
        $body = '<h2>Contact Request</h2>
                <h4>Name</h4><p>'.$firstname. ''.$lastname.'</p>
                <h4>Phone Number</h4><p>'.$phonenumber.'</p>
                <h4>Email Address</h4><p>'.$email.'</p>
                <h4>Subject</h4><p>'.$selected.'</p>
        ';

        // Email Headers
        $headers = "MIME-VERSION: 1.0" ."\r\n";
        $headers .="Content-Type:text/html;charset=UTF-8" ."\r\n";

        // Additional Headers
        $headers .= "From: " .$firstname. " ".$lastname." <".$email.">" ."\r\n"; 

        if(mail($toEmail, $subject, $body, $headers)) {
          // Message Sent
          $msg = 'Your message has been sent';
          $msgClass = 'alert-success';
        } else {
          // Message failed
          $msg = 'Your message has NOT been sent';
          $msgClass = 'alert-danger';
        }
      }  
    } else {
      //Failed
      $msg = 'Please fill in all fields';
      $msgClass = 'alert-danger';
    }
  }


?>



      <div class="container clearfix content-container">
        <h1 class="section-title">Contact Us</h1>
        <p>If you have any questions about the services we provide or would like to chat about a new website project, please get in touch using any of the options below. We would love to hear from you!</p>

        <!--Contact Page Row-->
        <div class="row">
          <div class="col-lg-6 contact-details">
            <div class="row">
              <div class="col-lg-12">
                <img src="assets/img/contact-us.jpg" class="img-responsive d-block contact-image" alt="Contact Us Image">
              </div>
            </div>
            <div class="row">
              <div class="col-lg-12 contact-name">
                <span class="d-inline-block contact-inline-block"><i class="fas fa-user fa-2x">
                </i></span>
                <span class="d-inline-block"><p class="contact-details">sn0wman23</p></span>   
              </div>
            </div>
            <div class="row">
              <div class="col-lg-12 contact-phone">
                <span class="d-inline-block contact-inline-block"><a href="tel:+447740484798"><i class="fas fa-mobile-alt fa-2x"></i></a></span>
                <span class="d-inline-block"><a href="tel:+441234567890"><p class="contact-details">01234 567890</a></p></span>  
              </div>
            </div>
            <div class="row">
              <div class="col-lg-12 contact-email">
                <span class="d-inline-block contact-inline-block"><a href="mailto:hello@example.co.uk"><i class="fas fa-envelope-square fa-2x"></i></a></span>
                <span class="d-inline-block"><a href="mailto:hello@example.co.uk"><p class="contact-details">hello@example.co.uk</a></p></span>  
              </div>
            </div>
            <div class="row">
              <div class="col-lg-12 contact-fb">
                <span class="d-inline-block contact-inline-block"><a href="https://www.facebook.com/" target="_blank"><i class="fab fa-facebook-square fa-2x"></a></i></span>
                <span class="d-inline-block"><a href="https://www.facebook.com/example/" target="_blank"><p class="contact-details">@example</a></p></span> 
              </div>
            </div>
          </div>

          <!--Contact Form-->
          <div class="col-lg-6 contact-form d-block">
            <?php if($msg != ''): ?>
              <div class="alert <?php echo $msgClass; ?>"><?php echo $msg; ?></div>
            <?php endif;?>
            <form method="post" action="contact.php" role="form">
              <div class=" row form-group">
                <div class="col-lg-6">
                  <label for="firstnameid">First name:</label>
                  <input type="text" name="firstname" class="form-control mb-3" id="firstnameid">
                </div>
                <div class="col-lg-6">
                  <label for="lastnameid">Last name:</label>
                  <input type="text" name="lastname" class="form-control mb-3" id="lastnameid">
                </div>
              </div>
              <div class="row form-group">
                <div class="col-lg-6">
                  <label for="phonenumber">Phone number:</label>
                  <input type="tel" name="phonenumber" class="form-control mb-3" id="phonenumberid">
                </div>
                <div class="col-lg-6">
                  <label for="emailid">Email address:</label>
                  <input type="email" name="email" class="form-control mb-3" id="emailid">
                </div>
              </div>
              <div class="row form-group">
                <div class="col-lg-12">
                  <label for="subjectid">How can I help?:</label>
                  <select class="form-control mb-3" id="subjectid">
                    <option selected>Select one from this list</option>
                    <option value="1">Value 1</option>
                    <option value="2">Value 2</option>
                    <option value="3">Value 3</option>
                  </select>
                </div>
              </div>
              <div class="row form-group">
                <div class="col-lg-12">
                  <label for="messageid">Tell me a little bit more:</label>
                  <textarea name="message" class="form-control mb-3" id="messageid" rows="6"></textarea>
                </div>
              </div>
              <div class="row form-group">
                <div class="col-lg-12 form-check">
                  <input type="checkbox" name="checkbox" class="form-check-input" id="formcheckid">
                  <label for="formcheckid" class=form-check-label mb-3>By checking this tickbox you have confirmed that we can collect the information in this form for the purposes outlined in our <a href="privacy-policy.html">privacy policy.</a></label>
                </div>
              </div>
              <div class="row form-group">
                <div class="col-lg-12">
                  <button type="submit" name="submit" class="btn btn-primary mt-4">Send Message
                  </button>
                </div>
              </div>
            </form>
          </div>
          <!--Contact Form End-->
        </div>
        <!--Contact Page Row End-->
      </div>
      <!--Container end-->

<?php echo file_get_contents('footer.php'); ?>

 

Link to post
Share on other sites
  • Solution

Your select element does not have a name.

Your checkbox doesn't have a value. ( I suggest "1")

Only checked checkboxes are posted.  You can use

$checkbox = $_POST['checkbox'] ?? 0;

Then $checkbox will contain 1 if checked and 0 if not.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.