Jump to content

[SOLVED] Suppressing empty input fields in email forms


clbenedict

Recommended Posts

Please help! I am PHP beginner so please excuse obvious mistakes. What I am trying to accomplish is to suppress empty input fields from a HTML form that I am emailling out. Here is what I tried.

 

$message="CUSTOMER CONTACT INFORMATION:";

if($name > 0) {$message="Name: ".$name."";}

if($phone1type == "--select--") {$message="Phone Type: ".$phone1type."";}

if($address > 0) {$message="Address: ".$address."";}

 

However, all I get is an empty form. Nothing gets returned. Please help. Thanks!

Link to comment
Share on other sites

That code seems to be written with the expectation that register_globals is enabled, which hasn't been the case in over 5 years (you must be using an old book or tutorial).

 

If the method in the form is "post" do:

<?php
$message="CUSTOMER CONTACT INFORMATION:";
if($_POST['name'] > 0) {$message="Name: ".$_POST['name']."";}
if($_POST['phone1type'] == "--select--") {$message="Phone Type: ".$_POST['phone1type']."";}
if($_POST['address'] > 0) {$message="Address: ".$_POST['address']."";}
?>

If the method is "get", replace the POST with GET

 

Ken

 

 

Link to comment
Share on other sites

This didn't work. I am still only getting "CUSTOMER CONTACT INFORMATION" in my emails. That's it. Nothing more. I do use a include include("global.inc.php"); at the top. I am going to copy my whole code here and if you can tell me what I am doing wrong, I would love to know.

 

<?php
include("global.inc.php");
$errors=0;
$error="The following errors occured while processing your form input.<ul>";


pt_register('POST','name');
pt_register('POST','phone');
pt_register('POST','phone1type');
pt_register('POST','phone1time');
pt_register('POST','phone2');
pt_register('POST','phone2type');
pt_register('POST','phone2time');
pt_register('POST','address');
pt_register('POST','address2');
pt_register('POST','city');
pt_register('POST','state');
pt_register('POST','zip');
pt_register('POST','email');
pt_register('POST','project');
$Storing=preg_replace("/(\015\012)|(\015)|(\012)/"," <br />", $project);
pt_register('POST','category');

// EXTERIOR ITEMS
// Siding Type:
pt_register('POST','brick');
pt_register('POST','woodsiding');
pt_register('POST','stucco');
pt_register('POST','aluminum');

// Other Exterior Options:
pt_register('POST','trim');
pt_register('POST','gutters');
pt_register('POST','deck');

// Problems:
pt_register('POST','lightpeeling');
pt_register('POST','heavypeeling');
pt_register('POST','rottenwood');

// Home Style:
pt_register('POST','ranch');
pt_register('POST','colonial');
pt_register('POST','3story');

// INTERIOR ITEMS
// Number of Rooms
pt_register('POST','number');

// Check all that apply:
pt_register('POST','wholehouse');
pt_register('POST','bedroom');
pt_register('POST','bathroom');
pt_register('POST','kitchen');
pt_register('POST','livdinroom');
pt_register('POST','wallpaperremoval');
pt_register('POST','plasterrepair');
pt_register('POST','drywallrepair');
pt_register('POST','wallrepair');
pt_register('POST','waterdamage');

// Specifics for Exterior/Interior
// Completion Date:
pt_register('POST','completedate');
// Square feet:
pt_register('POST','squarefeet');
// Check all that apply:
pt_register('POST','selling');
pt_register('POST','purchased');
pt_register('POST','existinghome');
pt_register('POST','rental');
pt_register('POST','commercial');
pt_register('POST','apartment');
pt_register('POST','condo');

// Start Validation Process Here
if(strlen($phone) < 10 ){    // checking the length of the phone number. it must be more than or equal to 10 characters in length
$error.="<li>Please enter your entire phone number, including area code. Thank you.";
$errors=1;   //setting the error to 1
}

if(strlen($phone2) > 0) {
if(strlen($phone2) < 10 ){    // checking the length of the phone number. it must be more than or equal to 10 characters in length
$error.="<li>Please enter your entire secondary phone number, including area code. Thank you.";
$errors=1;   //setting the error to 1
}
else { $errors=0;
}
}

if($name=="" || $city=="" || $state=="" || $zip=="" || $phone=="" || $email=="" ){
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
$errors=1;
}

if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)){
$error.="<li>Please enter a valid email address. Thank you.";
$errors=1;
}

if($errors > 0) {
  $error.="<br><br><input type='button' value='Retry' onClick='history.go(-1)'>";
}

if($errors==1) echo $error;
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));

$message="CUSTOMER CONTACT INFORMATION:";
if($_POST['name'] > 0) {$message="Name: ".$_POST['name']."";}
if($_POST['phone1type'] == "--select--") {$message="Phone Type: ".$_POST['phone1type']."";}
if($_POST['address'] > 0) {$message="Address: ".$_POST['address']."";}

if($_POST[empty($phone2)]) {$message="Secondary Phone: ".$_POST['phone2']."";}
if($_POST[isset($brick)] ) {$message="Siding Types: ".$_POST['brick']."";}

// $message="CUSTOMER CONTACT INFO:
// Name: ".$name."
// Comments:
// ".$project."
// ";
$message = stripslashes($message);
mail("connie_benedict@visualappealdesign.com","Choose a Painter Quote Request",$message,"From: ".$Email."");

$message2="CUSTOMER CONTACT INFORMATION:

Name: ".$name."
Comments:
".$project."
---------------------------------
How did you hear about CCIS? ".$phone1type."
Other: ".$address."
";
$message2 = stripslashes($message);
mail("connie.benedict@gmail.com","Contact Info w Address",$message2,"From: ".$Email."");

header("Refresh: 0;url=thankyou.html");
?><?php 
}
?>

 

(edited by kenrbnsn to add


tags)

Link to comment
Share on other sites

You can see I am trying to send two unique messages to 2 different emails. This is the other issue I am having. It seems that only message is being sent to both emails instead of message being sent to one and message2 being sent to the other.

 

Thanks so much for your help.

Link to comment
Share on other sites

i see an error in this:

if($_POST['name'] > 0) {$message="Name: ".$_POST['name']."";}
if($_POST['phone1type'] == "--select--") {$message="Phone Type: ".$_POST['phone1type']."";}
if($_POST['address'] > 0) {$message="Address: ".$_POST['address']."";}

if($_POST[empty($phone2)]) {$message="Secondary Phone: ".$_POST['phone2']."";}
if($_POST[isset($brick)] ) {$message="Siding Types: ".$_POST['brick']."";}

 

in order to use empty or isset, it's if(isset($_POST['phone2']))

Link to comment
Share on other sites

Here is the pt_register function. Let me know if this helps. Thanks!

 

<?php

function pt_register()
{
  $num_args = func_num_args();
   $vars = array();

   if ($num_args >= 2) {
       $method = strtoupper(func_get_arg(0));

       if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) {
           die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV');
     }

       $varname = "HTTP_{$method}_VARS";
      global ${$varname};

       for ($i = 1; $i < $num_args; $i++) {
           $parameter = func_get_arg($i);

           if (isset(${$varname}[$parameter])) {
               global $$parameter;
               $$parameter = ${$varname}[$parameter];
          }

       }

   } else {
       die('You must specify at least two arguments');
   }

}

?>

Link to comment
Share on other sites

The pt_register() function really shouldn't be used anymore. It looks like it was written for an earlier version of PHP (4.2) before the $_POST, $_GET, $_SESSION, $_SERVER, $_COOKIE superglobal arrays were introduced. You really should reference the values in their original arrays.

 

So now your script turns into:

<?php
include("global.inc.php");
$errors = false;
$error = array();
$error[] = 'The following errors occured while processing your form input.<ul>';

$Storing=(strlen(trim(stripslashes($_POST['project']))) > 0)?preg_replace("/(\015\012)|(\015)|(\012)/"," <br />", $_POST['project']) : '';


// Start Validation Process Here
foreach ($_POST as $fld => $val) {
$val = trim(stripslashes($val));
switch ($fld) {
	case 'phone':
		if(strlen($val) < 10 ){    // checking the length of the phone number. it must be more than or equal to 10 characters in length
			$error[]="<li>Please enter your entire phone number, including area code. Thank you.</li>";
			$errors = true;   //setting the error to 1
		}
		break;
	case 'phone2':
		if(strlen($val) > 0) {
			if(strlen(trim(stripslashes($_POST['phone2']))) < 10 ){    // checking the length of the phone number. it must be more than or equal to 10 characters in length
				$error[]="<li>Please enter your entire secondary phone number, including area code. Thank you.</li>";
				$errors = true;   //setting the error to 1
			}
			else
				$errors = false;
		}
		break;
	case 'name':
	case 'city':
	case 'state':
	case 'zip':
		if (strlen($val) == 0) {
			$error[] ="<li>You did not enter one or more of the required fields. Please go back and try again.</li>";
			$errors = true;
		}
		break;
	case 'email':
		if (strlen($val) == 0) {
			$error[] ="<li>You did not enter one or more of the required fields. Please go back and try again.</li>";
			$errors = true;
		}
		if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$val)){
			$error[] = "<li>Please enter a valid email address. Thank you.</li>";
			$errors = true;
		}
		break;
}
}

if($errors) {
$error[] ="</ul><br><br><input type='button' value='Retry' onClick='history.go(-1)'>";
echo implode("\n",$error);
}
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));

$message = array();
$message2 = array();
$message[] = "CUSTOMER CONTACT INFORMATION:";
$message2[] = "CUSTOMER CONTACT INFORMATION:";
foreach ($_POST as $fld => $val) {
	$val = trim(stripslashes($val));
	switch ($fld) {
		case 'name':
			$message[] = 'Name: ' . $val;
			$message2[] = 'Name: ' . $val;
			break;
		case 'project':
			$message2[] = 'Comments: ' . $val;
			break;
		case 'phone1type':
			$message[] = 'Phone Type: ' . $val;
			$message2[] = 'How did you hear about CCIS? ' . $val;
			break;
		case 'address':
			$message[] = 'Address: ' . $val;
			$message2[] = 'Other: ' . $val;
			break;
		case 'phone2':
			$message[] = 'Secondary Phone: ' . $val;
			break;
		case 'brick':
			$message[] = 'Siding Types: ' . $val;
	}
}
mail("yourfirst@email.here","Choose a Painter Quote Request",implode("\n",$message),"From: ".$_POST['email']);
mail("yoursecond@email.here","Contact Info w Address",implode("\n",$message2),"From: ".$_POST['email']);
}
header("Refresh: 0;url=thankyou.html");
?>

 

Ken

 

 

 

Link to comment
Share on other sites

Wow. Thank you for rewriting this. It definitely is a little above my head but will attempt to understand it as I play with it. So, It does work. You solved my mystery of the two messages. They work great.  There are three more questions I have:

 

1) When I run it to check the validation of the form I always get this message

Warning: Cannot modify header information - headers already sent by (output started at /home/vadtest7/public_html/contactchooseapainter.php:54) in /home/vadtest7/public_html/contactchooseapainter.php on line 91

 

Line 91 = header("Refresh: 0;url=thankyou.html");

Line 54 =    echo implode("\n",$error);

 

I tried to comment out line 54 but the that breaks all validation.

 

2) The error messages now double or triple meaning this output:

    * You did not enter one or more of the required fields. Please go back and try again.

    * Please enter your entire phone number, including area code. Thank you.

    * You did not enter one or more of the required fields. Please go back and try again.

    * You did not enter one or more of the required fields. Please go back and try again.

    * You did not enter one or more of the required fields. Please go back and try again.

    * Please enter a valid email address. Thank you.

    * Please enter a valid email address. Thank you.

 

You can see I get four "You did not enter..." and two "Please enter a valid email...". The reason why, I believe, is caused by each element that is being checked. So we have one error message for four items: name, city, state, email. The message is repeated four times.

 

For the email it should be one or the other. If nothing is there then it should say enter your email. And if something is there is should say valid email. I can obviously change the messages and actualy already did.

 

I also just put an error for each item so now it is descriptive on exactly what they didn't enter. But I still get to email messages. I tried to put in a if else statement and that just broke the code.

 

3) I still don't know how to suppress the empty fields. for the email messages. I tried something like this but it broke the code.

 

if(!empty('phone1type')){

            $message[] = 'Phone Type: ' . $val;

$message2[] =)'How did you hear about CCIS? ' . $val;}

            break;

 

Thank you for helping me solve these PHP mysteries. I appreciate your time and effort!

Link to comment
Share on other sites

Here is the form: http://www.vadtestsite.com/homepage.html

 

Here is the code: I did change it up and did get the email error message to be one of the other.

 

<?php
include("global.inc.php");
$errors = false;
$error = array();
$error[] = 'The following errors occured while processing your form input.<ul>';

$Storing=(strlen(trim(stripslashes($_POST['project']))) > 0)?preg_replace("/(\015\012)|(\015)|(\012)/"," <br />", $_POST['project']) : '';


// Start Validation Process Here
foreach ($_POST as $fld => $val) {
   $val = trim(stripslashes($val));
   switch ($fld) {
      case 'phone':
         if(strlen($val) < 10 ){    // checking the length of the phone number. it must be more than or equal to 10 characters in length
            $error[]="<li>Please enter your phone number, including area code.</li>";
            $errors = true;   //setting the error to 1
         }
         break;
      case 'phone2':
         if(strlen($val) > 0) {
            if(strlen(trim(stripslashes($_POST['phone2']))) < 10 ){    // checking the length of the phone number. it must be more than or equal to 10 characters in length
               $error[]="<li>Please enter your entire secondary phone number, including area code. Thank you.</li>";
               $errors = true;   //setting the error to 1
            }
            else
               $errors = false;
         }
         break;
      case 'name':
     if (strlen($val) == 0) {
            $error[] ="<li>Please enter you full name.</li>";
            $errors = true;
         }
         break;
      case 'city':
     if (strlen($val) == 0) {
            $error[] ="<li>Please enter your city.</li>";
            $errors = true;
         }
         break;
      case 'state':
         if (strlen($val) == 0) {
            $error[] ="<li>Please enter your state.</li>";
            $errors = true;
         }
         break;
      case 'zip':
         if (strlen($val) == 0) {
            $error[] ="<li>Please enter your zip code.</li>";
            $errors = true;
         }
         break;

      case 'email':
         if(strlen($val) == 0) {
                $error[] ="<li>Please enter your email address.</li>";
            	$errors = true;
            }
            else {
       		if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$val)){
          		$error[] = "<li>Please enter a valid email address. Thank you.</li>";
           		$errors = true;
			}
         }
   }
}

if($errors) {
   $error[] ="</ul><br><br><input type='button' value='Retry' onClick='history.go(-1)'>";
   echo implode("\n",$error);
}
else{
   $where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
   
   $message = array();
   $message2 = array();
   $message[] = "CUSTOMER CONTACT INFORMATION:";
   $message2[] = "CUSTOMER CONTACT INFORMATION:";
   foreach ($_POST as $fld => $val) {
      $val = trim(stripslashes($val));
      switch ($fld) {
         case 'name':


            $message[] = 'Name: ' . $val;
            $message2[] = 'Name: ' . $val;
            break;
         case 'project':
            $message2[] = 'Comments: ' . $val;
            break;
         case 'phone1type':
	  if(empty('phone1type')){
            $message[] = 'Phone Type: ' . $val;
		$message2[] ='How did you hear about CCIS? ' . $val;
		 }
            break;
         case 'address':
            $message[] = 'Address: ' . $val;
            $message2[] = 'Other: ' . $val;
            break;
         case 'phone2':
            $message[] = 'Secondary Phone: ' . $val;
            break;
         case 'brick':
            $message[] = 'Siding Types: ' . $val;
      }
   }
   mail("connie_benedict@visualappealdesign.com","Choose a Painter Quote Request",implode("\n",$message),"From: ".$_POST['email']);
   mail("connie.benedict@gmail.com","Contact Info w Address",implode("\n",$message2),"From: ".$_POST['email']);
}
header("Refresh: 0;url=thankyou.html");
?>

 

You will notice that I am trying to suppress the phone1type but to no avail.

 

Currently getting this error because of trying to suppress.

 

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '$' in /home/vadtest7/public_html/contactchooseapainter.php on line 93

Link to comment
Share on other sites

I figured out how to suppress the blank inputs. YEAH!!!!!! Just one last thing. This message. how does the echo implode impede the header? I don't understand this issue.

 

Warning: Cannot modify header information - headers already sent by (output started at /home/vadtest7/public_html/contactchooseapainter.php:54) in /home/vadtest7/public_html/contactchooseapainter.php on line 91

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.