Jump to content

Preventing blank form results


JCS1988

Recommended Posts

Hello, I am fairly new to PHP and only know a bit. I was able to come up with a form that users can fill out for a design request for a clients web site. The form is handled by another processor page and the information is then sent to the e-mail address for the company, right now It's sent to my e-mail for testing purposes. The form works great when they enter the information and submit, but if a users happens to visit the processer page it sends the form out blank. I understand that it couldn't post any data from the previous page because it was never given any information, but how can I make it so that the processor page only sends out the email when there is actual information to send. I know there is a way to do all the processing within one page but I am not sure how. If someone could please help me out, and maybe modify the scripts I have attached that would be awesome! Thanks in advance!

 

I have this code inside the header:

 

		 <!-- validate -->
	<SCRIPT type=text/javascript>
	<!--
		function validateField(fieldId, fieldBoxId, fieldType, required)
		{
			fieldBox = document.getElementById(fieldBoxId);
			fieldObj = document.getElementById(fieldId);

			if(fieldType == 'text'  ||  fieldType == 'textarea'  ||  fieldType == 'password'  ||  fieldType == 'file'  ||  fieldType == 'phone'  || fieldType == 'website')
			{	
				if(required == 1 && fieldObj.value == '')
				{
					fieldObj.setAttribute("class","mainFormError");
					fieldObj.setAttribute("className","mainFormError");
					fieldObj.focus();
					return false;					
				}

			}


			else if(fieldType == 'menu'  || fieldType == 'country'  || fieldType == 'state')
			{	
				if(required == 1 && fieldObj.selectedIndex == 0)
				{				
					fieldObj.setAttribute("class","mainFormError");
					fieldObj.setAttribute("className","mainFormError");
					fieldObj.focus();
					return false;					
				}

			}


			else if(fieldType == 'email')
			{	
				if((required == 1 && fieldObj.value=='')  ||  (fieldObj.value!=''  && !validate_email(fieldObj.value)))
				{				
					fieldObj.setAttribute("class","mainFormError");
					fieldObj.setAttribute("className","mainFormError");
					fieldObj.focus();
					return false;					
				}

			}



		}

		function validate_email(emailStr)
		{		
			apos=emailStr.indexOf("@");
			dotpos=emailStr.lastIndexOf(".");

			if (apos<1||dotpos-apos<2) 
			{
				return false;
			}
			else
			{
				return true;
			}
		}


		function validateDate(fieldId, fieldBoxId, fieldType, required,  minDateStr, maxDateStr)
		{
			retValue = true;

			fieldBox = document.getElementById(fieldBoxId);
			fieldObj = document.getElementById(fieldId);	
			dateStr = fieldObj.value;


			if(required == 0  && dateStr == '')
			{
				return true;
			}


			if(dateStr.charAt(2) != '/'  || dateStr.charAt(5) != '/' || dateStr.length != 10)
			{
				retValue = false;
			}	

			else	// format's okay; check max, min
			{
				currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
				//alert(currDays);

				if(maxDateStr != '')
				{
					maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
					//alert(maxDays);
					if(currDays > maxDays)
						retValue = false;
				}

				if(minDateStr != '')
				{
					minDays = parseInt(minDateStr.substr(0,2),10) + parseInt(minDateStr.substr(3,2),10)*30  + parseInt(minDateStr.substr(6,4),10)*365;
					//alert(minDays);
					if(currDays < minDays)
						retValue = false;
				}
			}

			if(retValue == false)
			{
				fieldObj.setAttribute("class","mainFormError");
				fieldObj.setAttribute("className","mainFormError");
				fieldObj.focus();
				return false;
			}
		}
	//-->
	</SCRIPT>
	<!-- end validate -->

 

 

This is the form itself:

 

<form method=post enctype=multipart/form-data action=processor.php onSubmit="return validatePage1();">
  <table width="605" border="0" align="left" cellpadding="0" cellspacing="4">
    <tr>
      <td width="61"> </td>
      <td width="255" align="right">*First Name:</td>
      <td width="277"><input class=mainForm type=text name=field_1 id=field_1 size='20' value=''></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">*Last Name:</td>
      <td><input class=mainForm type=text name=field_2 id=field_2 size='20' value=''></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">Street Address:</td>
      <td><input class=mainForm type=text name=field_3 id=field_3 size='20' value=''></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">City:</td>
      <td><input class=mainForm type=text name=field_4 id=field_4 size='20' value=''></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">State:</td>
      <td><input name=field_5 type=text class=mainForm id=field_5 value='' size='1' maxlength="2"></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">*Zip Code:</td>
      <td><input name=field_6 type=text class=mainForm id=field_6 value='' size='3' maxlength="5"></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">*Phone:</td>
      <td><input class=mainForm type=text name=field_7 id=field_7 size='15' value=''></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">Email:</td>
      <td><input class=mainForm type=text name=field_8 id=field_8 size='30' value=''></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">*I am interested in:</td>
      <td><select class=mainForm name=field_9 id=field_9>
      <option value="Choose One" selected>Choose One</option>
        <option value="Windows">Windows</option>
        <option value="Sunroom">Sunroom</option>
        <option value="Bath System">Bath System</option><br>
     <option value="Other">Other</option>
      </select>      </td>
    </tr>
    <tr>
      <td> </td>
      <td align="right" valign="top">If other, explain here:</td>
      <td><textarea class=mainForm  name=field_10 id=field_10 rows=5 cols=30></textarea></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">*How did you hear of us?:</td>
      <td><select class=mainForm name=field_11 id=field_11>
              <option value="Choose One" selected>Choose One</option>
        <option value="TV Commercial">TV Commercial</option>
        <option value="Newspaper">Newspaper</option>
        <option value="Postcard">Postcard</option>
        <option value="Magazine">Magazine</option>
        <option value="Home Show or Event">Home Show or Event</option>
        <option value="Doorhanger">Doorhanger</option>
        <option value="Yellow Pages (book)">Yellow Pages (book)</option>
        <option value="Yellow Pages (online)">Yellow Pages (online)</option>
        <option value="Radio">Radio</option>
        <option value="Referral">Referral</option>
        <option value="Word of Mouth">Word of Mouth</option>
        <option value="Internet Search Engine">Internet Search Engine</option>
        <option value="Other">Other</option>
      </select>      </td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">If other, type here:</td>
      <td><input class=mainForm type=text name=field_12 id=field_12 size='20' value=''></td>
    </tr>
    <tr>
      <td> </td>
      <td align="right">*Best day/time to meet with you:</td>
      <td><select class=mainForm name=field_13 id=field_13>
       <option value="Day" selected>Day</option>
        <option value="Mon">Mon</option>
        <option value="Tues">Tues</option>
        <option value="Wed">Wed</option>
        <option value="Thurs">Thurs</option>
        <option value="Fri">Fri</option>
        <option value="Sat">Sat</option>
        <option value="Sun">Sun</option>
      </select>      
        <select class=mainForm name=field_14 id=field_14>
        <option value="Time" selected>Time</option>
          <option value="Morning">Morning</option>
          <option value="Afternoon">Afternoon</option>
          <option value="Evening">Evening</option>
        </select>        </td>
    </tr>
    <tr>
      <td height="67"> </td>
      <td colspan="2" align="center" valign="middle"><br>
        <input id="saveForm" class="mainForm" type="submit" value="Submit" />
         
        <label>
        <input type="reset" name="reset" id="reset" value="Reset">
        </label></td>
      </tr>
  </table>
  <!-- end of this page -->

	<!-- page validation -->
	<SCRIPT type=text/javascript>
	<!--
		function validatePage1()
		{
			retVal = true;
			if (validateField('field_1','fieldBox_1','text',1) == false)
retVal=false;
if (validateField('field_2','fieldBox_2','text',1) == false)
retVal=false;
if (validateField('field_3','fieldBox_3','text',0) == false)
retVal=false;
if (validateField('field_4','fieldBox_4','text',0) == false)
retVal=false;
if (validateField('field_5','fieldBox_5','text',0) == false)
retVal=false;
if (validateField('field_6','fieldBox_6','text',1) == false)
retVal=false;
if (validateField('field_7','fieldBox_7','text',1) == false)
retVal=false;
if (validateField('field_8','fieldBox_8','text',0) == false)
retVal=false;
if (validateField('field_9','fieldBox_9','menu',1) == false)
retVal=false;
if (validateField('field_10','fieldBox_10','textarea',0) == false)
retVal=false;
if (validateField('field_11','fieldBox_11','menu',0) == false)
retVal=false;
if (validateField('field_12','fieldBox_12','text',0) == false)
retVal=false;
if (validateField('field_13','fieldBox_13','menu',1) == false)
retVal=false;
if (validateField('field_14','fieldBox_14','menu',1) == false)
retVal=false;

			if(retVal == false)
			{
				alert('Please correct the errors.  Fields marked with an asterisk (*) are required');
				return false;
			}
			return retVal;
		}
	//-->
	</SCRIPT>

	<!-- end page validaton -->
</form>

 

This is the code on the processor page the info is sent to:

 

<?php

$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

mail("[email protected]","Design Consultation Request","Design Consultation Request:

First Name: " . $_POST['field_1'] . " 
Last Name: " . $_POST['field_2'] . " 
Street: " . $_POST['field_3'] . " 
City: " . $_POST['field_4'] . " 
State: " . $_POST['field_5'] . " 
Zip Code: " . $_POST['field_6'] . " 
Phone: " . $_POST['field_7'] . " 
Email: " . $_POST['field_8'] . " 
I am intrested in: " . $_POST['field_9'] . " 
If other, explain here: " . $_POST['field_10'] . " 
How did you hear of us?: " . $_POST['field_11'] . " 
If other, type here: " . $_POST['field_12'] . " 
Best day to meet with you: " . $_POST['field_13'] . " 
Best time to meet with you: " . $_POST['field_14'] . " 

","From: [email protected]");

?>

 

 

Thanks again!!

Link to comment
https://forums.phpfreaks.com/topic/69084-preventing-blank-form-results/
Share on other sites

Add this to your processor page:

 

if (sizeof($_POST)==0)
{
header("Location:Form.php");
}
else
{
//put the rest of your processor code here
}

 

This will redirect you to your form if no post was sent to the page, but if a post was sent it will process.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.