1
votes

Based heavily upon this email validation code (which works - I've tested it)

<?php


require_once("include/connectionpdo.php"); 


echo("<h2>parsed1</h2> ");

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    echo "If you have fixed any possible errors, and you believe that there is a problem with our submission system, please ";
    die();
    }

   echo("<h2>parsed2</h2> ");

   // first checks to see if any information is supplied at all in the required fields 
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['addressl1']) ||
        !isset($_POST['addressl2']) ||
        !isset($_POST['town']) ||
        !isset($_POST['county']) ||
        !isset($_POST['type']))
    {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    echo("<h3>parsed3</h3> ");

    $first_name = $_POST['fname']; // required
    $last_name = $_POST['sname']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['phone']; // not required
    $dateofbirth = $_POST['dob']; // required
    $addresslone = $_POST['addressl1']; // required
    $addressltwo = $_POST['addressl2']; // required
    $townnm = $_POST['town']; // required
    $countynm = $_POST['county']; // required     
    $typeapp = $_POST['type']; // required
    $issubscribed = $_POST['subscribed']; // required

    // checks to see if information supplied is correct

    echo("<h4>parsed4</h4> ");

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    $number_exp = '/^[0-9]/';  
  if(!preg_match($email_exp,$email_from)) {
    echo("errortest1 ");
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    echo("errortest2 ");
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    echo("errortest3 ");
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
    $number_exp = '/^[0-9]/';  
  if(!preg_match($string_exp,$last_name)) {
    echo("errortest4 ");
    $error_message .= 'Please just enter numerical values for your phone number.<br />';
  }

  if(strlen($addresslone) < 2) {
    echo("errortest5 ");
    $error_message .= 'The address (line one) you entered do not appear to be valid.<br />';
  }
   if(strlen($addressltwo) < 2) {
    echo("errortest6 ");
    $error_message .= 'The address (line two) you entered do not appear to be valid.<br />';
  } 
  if((!preg_match($string_exp,$townnm)) || (strlen($townnm) < 2)) {
    echo("errortest7 ");
    $error_message .= 'The town you entered does not appear to be valid.<br />';
  } 
  if((!preg_match($string_exp,$countynm)) || (strlen($countynm) < 2)) {
    echo("errortest8 ");
    $error_message .= 'The county you entered does not appear to be valid.<br />';
  } 

  if(strlen($error_message) > 0) {
    died($error_message);
  }


# the data we want to insert
$data = array($first_name, $second_name, $email_from, $telephone, $dateofbirth, $addresslone, $addressltwo, $townnm, $countynm, $typeapp, $issubscribed);
$STH = $DBH->prepare("INSERT INTO members (fname, sname, email, phone, dob, addressl1, addressl2, town, county, type, subscribed) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$STH->execute($data);


?>
<!--<!DOCTYPE html>
<head><title></title></head><body> -->

Thank you for contacting us  We will be in touch with you very soon.

<!-- </body></html> -->

The code prints "parsed 2", but doesn't get much further.

function died runs, but only prints the additional generic message of "'We are sorry, but there appears to be a problem with the form you submitted."

Oddly !isset always seems to return true (regardless of what form information is in the form). I deleted all of the if !isset block, which caused the printing of "parsed 3", "parsed 4", "errortest1" and "The Email Address you entered does not appear to be valid." Again, this was irrespective of whether or not the email address in the form met the stated condition. Removing the if!preg_match statements causes the generation of the error message

"Fatal error: Call to a member function prepare() on a non-object in public_html/application4.php on line 88"

.

There are no syntax errors, or at least if there are any syntax errors they are generating merely logical errors.

2

2 Answers

1
votes

imho you have changed names for first_name and last_name fields, so below code should return false if all fields in the condition are set:

if(!isset($_POST['fname']) ||
        !isset($_POST['sname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['addressl1']) ||
        !isset($_POST['addressl2']) ||
        !isset($_POST['town']) ||
        !isset($_POST['county']) ||
        !isset($_POST['type']))
{
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
1
votes

see the difference or variable name into your code.

after echo("<h2>parsed2</h2> ");

you have used
!isset($_POST['first_name']

and after echo("<h3>parsed3</h3> ");

you have used

$first_name = $_POST['fname']; // required

how 'first_name' becomes 'fname'?

either first_name will be use into code or fname will be use into code.

Same as this, last_name becomes sname. Update the code for this and test it again.