1

So I have asked a similar question but in the comments, I was told a possible answer however it didn't work so I'm posting another one. The situation is slightly different so I didn't know whether to edit and update the other one or make a new one.

Anyway, I have a form and a PHP script that is supposed to validate the form data when the user submits and for now(testing purposes) if it does not pass the validation an array with strings should print.

Berfore I go on here is the html:

<form method="post" action="mail3.php" class="col-12" id="form-book">
            <div class="row">
              <div class=" form-group col-12">
                <p>Fill out the form below to tell me about your problem. <strong>Any problems to do with technology, all the solutions.</strong> Just tell me what you can, however the more info you give the better.</p>
              </div>
              <div class="form-group col-6">
                <label></label>
                <input id="name" name="name" placeholder="Name" type="text" required="required" class="form-control here bottom"> <span class="error">
              </div>
              <div class="form-group col-6">
                <label></label>
                <input id="phone" name="phone" placeholder="Phone#" type="text" required="required" class="form-control here bottom"> <span class="error">
              </div>
              <div class="form-group col-12">
                <label></label>
                <input id="email" name="email" placeholder="Email (Optional)" type="text" class="form-control here bottom"> <span class="error">
              </div>
              <div class="form-group col-6">
                <input data-provide="datepicker" id="date" name="date" placeholder="Pick a date" required="required" class="form-control here bottom">
              </div>
              <div class="form-group col-6">
                <input type="text" id="time" name="time" placeholder="Choose the best time" required="required" class="form-control here bottom timepicker">
              </div>
              <div class="form-group col-12">
                <label></label>
                <textarea id="message" name="message" cols="40" rows="5" class="form-control" required="required" aria-describedby="messageHelpBlock"></textarea>
                <span id="messageHelpBlock" class="form-text text-muted">Tell me a little about whats going on or what you need help with.</span>
              </div>
              <div class="form-group col-12">
                <button name="submit" type="submit" class="btn btn-primary">Send your message</button>
              </div>
            </div>
            <!--row-->
          </form>

PHP:

                <?php
    date_default_timezone_set("Pacific/Honolulu"); //set the DateTimeZone

    function test_input($data) { //test input func that checks data
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }

    if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $date = $_POST['date'];
    $time = $_POST['time'];
    $message = $_POST['message'];
    $subject = "Appointment Booked";
    $email = filter_var($email, FILTER_SANITIZE_EMAIL); //clean email
    $mailTo = "itguy@johnpuaoi.com";
    $dateTested = test_input($date);
    $timeTested = test_input($time);

    $errors = []; //set errors arrray

    //funcs will check each input and if an error is detected it will add a string to errors array
    //if no errors are found the input is then passed into xTested variable
    function validateName($input) {
      if (empty($input)) { //check if empty
        return $errors[] = "Your name is required."; //if empty add string to errors array
      } elseif (is_int($input)) { //check if input is an interger
        return $errors[] = "That is not a valid name."; //if is interger add string to errors array
      } else {
        return $nameTested = test_input($input); //return tested input variable
      }
    }
    function validatePhone($input) {
      if (empty($input)) { //check if empty
        return $errors[] = "Your phone number is required."; //add string to array if empty
      } elseif (!preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $input)) { //check if input does not match
        return $errors[] = "That is not a valid name."; //add string to array if no match
      } else {
        return $phoneTested = test_input($input); //return tested input variable
      }
    }
    function validateEmail($input) {
      if (empty($input)) { //check if empty
        return $errors[] = "Your email is required.";//add string to array if empty
      } elseif (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        return $errors[] = "That is not a valid email.";
      } else {
        return $emailTested = test_input($input); //return tested input variable
      }
    }
    function validateMessage($input) {
      if (empty($input)) { //check if empty
        return $errors[] = "Your message can't be empty.";//add string to array if empty
      } else {
        return $messageTested = test_input($input); //return tested input variable
      }
    }
    validateName($name); //call validate funcs
    validatePhone($phone);
    validateEmail($email);
    validateMessage($message);

    $headers = "From: ".$emailTested;
    $txt = "An appointment was booked by ".$nameTested.".\n\n".$dateTested." @".$timeTested.".\n\n".$messageTested;

     if ($errors == null) { //if errors array is empty
       mail($mailTo, $subject, $txt, $headers); //mail data
       header("Location: index.php?mailsend"); //redirect to home
     } else {
       print $errors; //print errors to test if validate funcs work
     }
    }
 ?>

Now I have created validate functions for a set data type - name, email, phone and message. It checks the input and if an error occurs then a string is added to the errors array. If no error is found then the input is passed to a variable to tells php it has been tested for errors.

IMPORTANT In the last question I asked, in the comments someone pointed out that I did not have anything stopping the mail func even if the data did not pass validation. So to fix that I included an if statement at the bottom that checks if the errors array is empty. If it is then the mail func is called, if it isn't then for testing purposes the errors array is printed.

I test the script by purposely entering invalid data but the form submits anyway. What is allowing the script to still mail the form data?

Now if you could not tell already, I am new to php so forgive me if I its something simple. I apologize in advance also if I should have just edited the first question.

  • You aren't assigning the return value of the validation functions to anything. – Patrick Q Nov 13 '18 at 20:05
  • The scope of the $errors array means it isn't visible to your if statement. – Dave Nov 13 '18 at 20:07
  • Isn't that what the "return $nameTested = test_input($input);" does? or should it be "$nameTested = test_input($input); then return $nameTested? – johncodes808 Nov 13 '18 at 20:09
  • @Dave how then do I make $errors avaliable to it – johncodes808 Nov 13 '18 at 20:17
  • You can make your $errors array a global variable. Within each of your functions refer to your $errors array as $GLOBALS['errors'] – MichaelvE Nov 13 '18 at 20:20
  • @MichaelvE You may wish to read [this](https://stackoverflow.com/questions/5166087/php-global-in-functions) (both the question and accepted answer) before recommending the use of global variables. johncodes, it would be good for you to read that as well. – Patrick Q Nov 13 '18 at 20:29

0 Answers0