Form Error Condition Never Met

Hi guys,

I’ve got a PHP contact form that I’ve been using for sometime. However I’ve tried to include error handling on a for loop that does not seem to work:

if (!empty($_POST[‘product’])){

			//$product = $_POST['product'];
			
			$P = count($product);
			$products = "";
						
				for($i=0; $i < $P; $i++)
				{
					$products = $products . $product[$i];
					if($i < $P -1)
						$products = $products . (", ");
				}
				
				}else{
			$product = NULL;
			echo '<p class="error">Please select a product.</p>';
		}

How I would expect this to behave is, if the(any) product checkbox is not selected, then the error condition would be met and the message would echo out.

But, regardless the form is always processed, even when product is not selected.

Try var_dump(`$_POST); after the if condition.

Edit:
Enclosing var_dump(…) in <pre> tags makes the output easier to read.

Tapped on a tablet.

The only info I get from the var_dump is:

array(9) {
[“name”]=>
string(0) “”
[“email”]=>
string(0) "
string(0) “”
[“company”]=>
string(0) “”
[“subject”]=>
string(0) “”
[“country”]=>
string(0) “”
[“comments”]=>
string(0) “”
[“mail-list”]=>
string(6) “Opt-in”
[“submit”]=>
string(11) “submit form”
}

“product” does not appear in the array as it’s not been selected, which I suppose, is why there’s no error message.

Use isset instead of empty

Unfortunately that made no difference. The form was still sent, although “product” was empty:

Could I post the whole form process to see if that makes things clearer (it’s a bit of a mess, I’m no PHP expert!)

<?php

// start form processing

if($_POST['submit']){

       // form feedback and errors
    
		   
		   
       $error = false;
	   
	   
	 
	   
	   // check for hidden field content
		
	   if (!empty($_POST['subject'])){
			echo 'critical error'; 
			exit(); // generic fail message and kill form process
	   }
	   
	   
	
	   
	   
	   // check input fields against blacklisted words and spam phrases
	   
	   $badwords = array("adult", "beastial", "bestial", "blowjob", "clit", "cum", "cunilingus", "cunillingus", "cunnilingus", "cunt", "ejaculate", "fag", "felatio", "fellatio", "fuck", "fuk", "fuks", "gangbang", "gangbanged", "gangbangs", "hotsex", "hardcode", "jism", "jiz", "orgasim", "orgasims", "orgasm", "orgasms", "phonesex", "phuk", "phuq", "pussies", "pussy", "spunk", "xxx", "viagra", "phentermine", "tramadol", "adipex", "advai", "alprazolam", "ambien", "ambian", "amoxicillin", "antivert", "blackjack", "backgammon", "texas", "holdem", "poker", "carisoprodol", "ciara", "ciprofloxacin", "debt", "dating", "porn", "link=", "voyeur", "content-type", "bcc:", "cc:", "document.cookie", "onclick", "onload", "javascript", "http", "https", "www", "href", "script");

			foreach ($badwords as $word)
				if (
					strpos(strtolower($_POST['name']), $word) !== false ||
					strpos(strtolower($_POST['comments']), $word) !== false
				)
				
				
				
				{
				echo $error = '<p class="error">Error Invalid content</p>';
					exit(); // generic fail message and kill form process
				}
	   
	   
	 
	   
	   // regex for input field validation
	   
			if (!preg_match('/^[a-zA-Z \' \. \, \" \: \; \? \- \!]*$/', $_POST['name'])){
				$error = '<p class="error">Name error: only letters, punctuation and white space allowed</p>';
			}
			
			if (!preg_match('/^[a-zA-Z0-9 \' \. \, \" \: \; \? \- \!]*$/', $_POST['company'])){
				$error = '<p class="error">Company error: only letters and white space allowed</p>';
			}
			
			if (!preg_match('/^[a-zA-Z0-9 \' \. \, \" \: \; \? \- \!]*$/', $_POST['comments'])){
				$error = '<p class="error">Comments error: only letters and white space allowed</p>';
			}
			
		   if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
			  $error = '<p class="error">Invalid email format</p>';
			}

		   if (!preg_match('/^[0-9 \- \+ \( \) ]*$/', $_POST['phone'])){
			  $error = '<p class="error">Phone error: only numbers and white space allowed</p>';
			}
      
	
	  
 
 
		// capture IP and host detail of user

			$show_ip_hostname = TRUE;
			if ($show_ip_hostname) {
			$show_ip_hostname .= 'IP: ' . $_SERVER['REMOTE_ADDR'] . "\n" . 'HOST: ' . gethostbyaddr($_SERVER['REMOTE_ADDR']) . "\n";
			}

			
			
			
		// switch loop to handle distributor email allocation	

			$country = $_POST['country'];
			
			
				 
				 
				 
				switch($country) {
					 
					default :
						$to = "";
					break;
					
					case "Argentina" :
						$to = "";
					break;
					
					case "Austria" :
						$to = "";
					break;
					
					
					
				 }

				 
				 
		// check submitted content for errors and validation on POST

			$from = "From: \"".$_POST['name']."\" <".$_POST['email'].">";
			
			$subject = "Website: Request for information - [$country]";
			

			if (!empty($_POST['name'])){
				$name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
				}else{
				$name = NULL;
				echo '<p class="error">Missing required field: please enter your name!</p>';
			}

			$company = htmlentities($_POST['company'], ENT_QUOTES, 'UTF-8');

			if (!empty($_POST['email'])){
				$email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
				}else{
				$email = NULL;
				echo '<p class="error">Missing required field: please enter an email address!</p>';
			}

			$phone = htmlentities($_POST['phone'], ENT_QUOTES, 'UTF-8');
			
			$comments = htmlentities($_POST['comments'], ENT_QUOTES, 'UTF-8');
			
			$newsletter = htmlentities($_POST['mail-list'], ENT_QUOTES, 'UTF-8');
			

			
			
			
			
			
		// checkbox validation - has the user selected a product?
		
			//if (!empty($_POST['product'])){
				
				if (isset($_POST['product'])){
				
				$P = count($product);
				$products = "";
							
					for($i=0; $i < $P; $i++)
					{
						$products = $products . $product[$i];
						if($i < $P -1)
							$products = $products . (", ");
					}
					
					}else{
				$product = NULL;
				echo '<p class="error">Please select a product.</p>';
			}
				
			
						
		// checkbox validation - has the user selected a enquiry method?	
		
			if (!empty($_POST['requesting'])){	
		
			$requesting = ($_POST['requesting']);
			$R = count($requesting);
			$reqs = "";
						
				for($i=0; $i < $R; $i++)
				{
					$reqs = $reqs . $requesting[$i];
					if($i < $R -1)
						$reqs = $reqs . (", ");
				}
				} else {
				$requesting = $error;
				echo '<p class="error">Please select a service.</p>';
				}

			

			
			
		// output and format message content to email
		
htmlentities($body = "
This message has been generated by the website in response to a customer enquiry for $country.\n\n
SALES ENQUIRY\n
Name:  $name 
Company: $company 
Email: $email 
Phone: $phone 
Product: $products 
Request: $reqs \n

Comments: $comments \n

Mailing List: $newsletter \n


IP: $show_ip_hostname \n
", ENT_QUOTES, 'UTF-8');
			
			
			
			
			if(!$error){

			  echo "<p class='success' id='contactAction'>Your request has been submitted, a representative will contact you shortly.</p>";
			  
			  mail($to, $subject, $body, $from ); 
		   } else {
			  echo "<p class='error' id='contactAction'>Your message has not been sent.. $error </p>"; 
			 echo "<p><a href='customerRequest2.php'>Try again?</a></p>";
		   }

		   
		   
		
    }
	
	
	
	
// end form processing

?>

can you show the form

I can post the HTML for the form, but not link to it as it’s on a corporate website.

<form method="post" action="customerRequest2.php#contactAction">
									

<div class="row">


	<div class="6u">
	
	<label for="name" class="label">Name</label>
	<input name="name" type="text" id="name" maxlength="40" /> *
	
	<br />
	
	<label for="email"  class="label">Email</label> 
	<input name="email" type="text" id="email" maxlength="50"/> *

	
	
	<br />
	
	</div>
	
	<div class="6u">

	<label for="phone"  class="label">Phone</label>
	<input name="phone" type="text" id="phone" maxlength="22"/>
	
	<br />
	
	<label for="company"  class="label">Company</label>
	<input name="company" type="text" id="company" maxlength="40" />
	
	<br />
	
	</div>
	
</div>


	<input name="subject" type="text" id="subject"  />

	<label for="country"  class="label">Country</label>
	<select name="country" title="">
		  <option selected="selected">(Select your nearest distributor)</option>
		  <option value="Argentina">Argentina</option>
		  <option value="Austria">Austria</option>
		  <option value="Belgium">Belgium</option>
		  <option value="Bulgaria">Bulgaria</option>
		  <option value="China">China</option>
		  <option value="Costa Rica">Costa Rica</option>
		  <option value="Croatia">Croatia</option>
		  <option value="Czech Republic">Czech Republic</option>
		  <option value="Denmark">Denmark</option>
		  <option value="Egypt">Egypt</option>
		  <option value="El Salvador">El Salvador</option>
		  <option value="Estonia">Estonia</option>
		  <option value="Finland">Finland</option>
		  <option value="France">France</option>
		  <option value="Germany">Germany</option>
		  <option value="Greece">Greece</option>
		  <option value="Hungary">Hungary</option>
		  <option value="Ireland">Ireland</option>
		  <option value="Israel">Israel</option>
		  <option value="Italy">Italy</option>
		  <option value="Kingdom of Saudi Arabia">Kingdom of Saudi Arabia</option>
		  <option value="Korea">Korea</option>
		  <option value="Luxembourg">Luxembourg</option>
		  <option value="Malaysia">Malaysia</option>
		  <option value="Mexico">Mexico</option>
		  <option value="Netherlands">Netherlands</option>
		  <option value="New Zealand">New Zealand</option>
		  <option value="Norway">Norway</option>
		  <option value="Poland">Poland</option>
		  <option value="Portugal">Portugal</option>
		  <option value="Russia">Russia</option>
		  <option value="Singapore">Singapore</option>
		  <option value="Slovenia">Slovenia</option>
		  <option value="South Africa">South Africa</option>
		  <option value="Spain">Spain</option>
		  <option value="Sweden">Sweden</option>
		  <option value="Taiwan">Taiwan</option>
		  <option value="Thailand">Thailand</option>
		  <option value="Turkey">Turkey</option>
		  <option value="United Arab Emirates">United Arab Emirates</option>
		  <option value="United Kingdom">United Kingdom</option>
		  <option value="United States">United States</option>
	</select> * 
	
		
		
		
		
		
		<div class="row">
		
			<div class="4u">
		
		<br />
		<br />
	<label for="options" class="options">Select your product(s) of interest:</label>
	
	
	
	<div class="item">
		<input type="checkbox" name="product[]" value="product 1" id="product1" />
		<label for="product1">product 1</label>
	
	</div>
	
	<div class="item">
		<input type="checkbox" name="product[]" value="product 2" id="product2" />
		<label for="product2">product 2</label>
	</div>
	
	

	</div>
	

	
	</div>
	

	
	
	<div class="4u">
	<br />
	<br />
	
	<label for="options" class="options">Select your enquiry type:</label>
	
	<div class="item">
		<input type="checkbox" name="requesting[]" value="Sales call" id="sales"/>
		<label for="sales">Sales Call</label>
	</div>
	
	<div class="item">
		<input type="checkbox" name="requesting[]" value="Demonstration" id="demo"/>
		<label for="demo">Demo</label>
	</div>
	
	<div class="item">
		<input type="checkbox" name="requesting[]" value="Quote" id="quote"/>
		<label for="quote">Quote</label>
	</div>
	
	
	
	</div>
	
	</div>

	
	<br />
	<br />
	
	<label for="name" class="label">Comments</label> <br />
	<textarea value="" id="comments" name="comments" rows="8" cols="100"></textarea>
	
	
	<br />
	
	
	<input type="checkbox" name="mail-list" value="Opt-in" id="mail-list" checked />
	<label for="mail-list">Sign me up to news and product mailing list.</label>
	
	

	
	
	<br />
	
	<input class="send" name="submit" type="submit" id="submit" value="submit form" />

* required field

You are not getting empty error because your input value is not empty by default change this

<input type="checkbox" name="product[]" value="product 1" id="product1" />

to

<input type="checkbox" name="product[]" value="" id="product1" />

On both of the product input. Instead of this you can use placeholder to give hint on input text.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.