Script Not Catching Empty Name Field

Hello all,

I have a question about the home page e-mail submission box for my website. When you interact and get to the second form, the script does not output an error message if the name field is left empty.

The script:


<?php
	$instance = new CheckForm;
	$instance -> checkSubmission();
	
	class CheckForm
	{
		public function checkSubmission()
		{	
			$response = array("validation" => " ", "message" => " ", "database" => " ");	
			if ($_POST['country'] != "Select Country")
			{
				if (!is_null($_POST['confirmEmail']) && !is_null($_POST['name']))
				{
					$origEmail = $_POST['origEmail'];
					$confirmEmail = $_POST['confirmEmail'];
					if ($origEmail == $confirmEmail)
					{
						$name = htmlspecialchars($_POST['name']);
						$ageRange = $_POST['age'];
						$country = $_POST['country'];
						
						require_once("categoryfinder.php");
						$categoryFinder = new CategoryFinder;
						$category = $categoryFinder -> getCategory();
						$response = array("validation" => "pass", "message" => "Thanks for joining the e-mail list, <b>" . $name . "</b>, under the e-mail address, <b>" . $confirmEmail . "</b>.", "database" => "pass");
						try{
							require_once('databasewriter.php');
							$dbWriter = new DatabaseWriter;
							$dbWriter -> writeUserToDatabase($confirmEmail, $name, $ageRange, $country, $category);
						} catch (PDOException $e) {
							$response = array("validation" => "pass", "message" => "Thanks for joining the e-mail list, <b>" . $name . "</b>, under the e-mail address, <b>" . $confirmEmail . "</b>.", "database" => "fail");
						}

					} else {
						$response = array("validation" => "fail", "message" => "E-mail addresses don't match.", "database" => "fail");
					}
				} else {
					if (is_null($_POST['confirmemail'])){
						$response = array("validation" => "fail", "message" => "Confirmation e-mail not entered.");
					} elseif (is_null($_POST['name'])) {	
						$response = array("validation" => "fail", "message" => "Please enter a name.");
					}
				}
			} else {
				$response = array("validation" => "fail", "message" => "Please select a country.");
			}
			echo json_encode($response);
		}
	}
?>

What seems to be the issue? :cap:

if (!is_null($_POST['confirmEmail']) && !is_null($_POST['name']))

This says If ‘confirmEmail’ and (&&) ‘name’ are both null.

What you want is

if (!is_null($_POST['confirmEmail']) || !is_null($_POST['name']))

If either ‘confirmEmail’ OR (||) ‘name’ are null…

Yeah, you’re right! :cool:

But it’s still not posting that particular error to the #errormessage <p> element, whereas if I don’t select a country it says “Please select a country” like it should.

I think it may be due to the nested if statement near the end of the script.

Perhaps you could “fail early” on each of your tests, rather than invoke a shed-load of indentation …


if ($_POST['country'] === "Select Country") { 
    $response = array("validation" => "fail", "message" => "Please select a country.");
}

All right, I tried mixing this PHP script up, but I’m still not seeing an error message if the name is left empty! :smashy:


<?php
	$instance = new CheckForm;
	$instance -> checkSubmission();
	
	class CheckForm
	{
		public function checkSubmission()
		{	
			$origEmail = $_POST['origEmail'];
			$confirmEmail = htmlspecialchars($_POST['confirmEmail']);
			$name = htmlspecialchars($_POST['name']);
			$ageRange = $_POST['age'];
			$country = $_POST['country'];
			$status = 0;
		
			$response = array("validation" => " ", "message" => " ", "database" => " ");
		
			if (is_null($confirmEmail) && is_null($name) && $country === "Select Country") {
				$response = array("validation" => "fail", "message" => "That's not a valid submission.");
			} elseif ($country === "Select Country") {
			    $response = array("validation" => "fail", "message" => "Please select a country.");
			} elseif (is_null($name)) {
				$response = array("validation" => "fail", "message" => "Please enter a name.");
			} elseif (is_null($confirmEmail)) {
				$response = array("validation" => "fail", "message" => "No confirmation e-mail was entered.");
			} elseif ($origEmail != $confirmEmail) {
				$response = array("validation" => "fail", "message" => "E-mail addresses don't match.");
			} else
				$status = 1;
			
			if ($status === 1) {
						require_once("categoryfinder.php");
						$categoryFinder = new CategoryFinder;
						$category = $categoryFinder -> getCategory();
						$response = array("validation" => "pass", "message" => "Thanks for joining the e-mail list, <b>" . $name . "</b>, under the e-mail address, <b>" . $confirmEmail . "</b>.", "database" => "pass");
						try{
							require_once('databasewriter.php');
							$dbWriter = new DatabaseWriter;
							$dbWriter -> writeUserToDatabase($confirmEmail, $name, $ageRange, $country, $category);
						} catch (PDOException $e) {
							$response = array("validation" => "pass", "message" => "Thanks for joining the e-mail list, <b>" . $name . "</b>, under the e-mail address, <b>" . $confirmEmail . "</b>.", "database" => "fail");
						}
			}

			echo json_encode($response);
		}
	}
?>

Thanks!


if(empty($name)){


}

Thanks, Cups!