Jump to content

Check Form Fields Before Email


thesaleboat

Recommended Posts

What I am trying to do is have this form be verified that all of the fields are filled out before it is emailed.  But when i test it, it just refreshes the page, and deleted all the stuff typed in, instead of popping up an alert. Does php have a separate way to send alerts?

 

<form name="resume" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" >
<div align="center">
<span class="mainHeader2" >Online Resume Submission:</span><br />
<span class="mainTextNoIndent">Please fill out the following form.</span><br/><br/>
<span class="mainTextNoIndent"><strong>Name:</strong></span><br/>
<input type="text" name="requiredName" id="requiredName" value="<?php echo $row['requiredName'];?>" size="20" maxlength="10" /><br/><br/>
<span class="mainTextNoIndent"><strong>Email Address:</strong></span><br/>
<input type="text" name="requiredEmailAddress" id="requiredEmailAddress" value="<?php echo $row['requiredEmailAddress'];?>" size="30" /><br/><br/>
<span class="mainTextNoIndent"><strong>Miscellaneous: </strong></span><br/>
<textarea name="Miscellaneous" id="Miscellaneous" value="<?php echo $row['Miscellaneous'];?>" rows="8" cols="85"></textarea><br/><br/>
<span class="mainTextNoIndent"><strong>* Please ensure your email address is correct before submitting.</strong></span><br/>
<input type="submit" name="submit" value="Submit" /><br/>
</div>
<br/>
</form>
<?php
if (isset($_POST['submit']))
{
// Get Information from form
$Name=$_POST['requiredName'];
$EmailAddress=$_POST['requiredEmailAddress'];
$Miscellaneous=$_POST['Miscellaneous'];

if (empty($Name)) {
$error = 'You did not fill in your email address.';
alert("The first name field is a required field.");
} 
else if (empty($EmailAddress)) {
$error = 'You did not fill in your email address.';
alert("The email address field is a required field.");							
} 
else if (empty($EmailAddress)) {
$error = 'You did not fill in your email address.';
alert("The miscellaneous field is a required field.");
}
if (!$error) {
$EmailAddress = trim($EmailAddress);
$to = "me@behindyou.com";
$subject = "Online Resume Submittion";
$EmailAddress=$_POST['EmailAddress'];
$from = "From: ".$EmailAddress;
$message = "First Name: \n".$Name." ";
$message .= "\n\n\nEmail Address: \n".$EmailAddress." ";
$message .= "\n\n\nMiscellaneous: \n".$Miscellaneous." ";

mail($to, $subject, $message, $from); //Send Email 
echo "Thank you for your application, we will be in contact with you shortly if you meet the job description requirements.";
echo '<meta content="4; URL=index.htm" http-equiv="Refresh" />';//go back to homepage after they read the thank you message
}
}
?>                    

Link to comment
Share on other sites

to the best of my knowledge, alert() is not a PHP function. you may be thinking javascript.

 

 

for errors, i usually echo them to the page in case javascript is turned off, putting the submitted values into the text fields so they don't have to be filled in again.

 

example:

 

$errors = array();
if (trim($_POST['fname']) == "") {
    $errors[] = "First name is required.";
}

if (trim($_POST['lname']) == "") {
    $errors[] = "Last name is required.";
}

if (count($errors) == 0) { 
     // no errors, send emails
} else {
    // there are errors. display them.
    echo "Please correct the following errors:<BR>";
    echo implode('<BR>',$errors);
}

Link to comment
Share on other sites

yes sorry that i havent responded in a while guys, been kinda hectic.  Alert is javascript, but this is the wierd thing, when i tested it on my XAMP local server the alert would pop up and the form could not be submitted until the user entered all Required fields, but when i put it online no alert would pop up and the form could be submitted empty, any thoughts on that.  also blueSkyIS, how do you make the text fields write back to themselves if the 'if' statements arent passed. that is the only reason im trying to have it be action="<?php echo $_SERVER['PHP_SELF'];?>" i had this form working perfectly already. here is my code at this time.

<form name="resume" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" >
  <div align="center">
    <br/><br/>
    <span class="mainHeader2" >Online Resume Submission:</span><br />
    <span class="mainTextNoIndent">Please fill out the following form.</span>
    <br/><br/>
     <span class="mainTextNoIndent"><strong>First Name:                                    M.I.:           Last Name: </strong></span><span class="mainTextNoIndent">                             </span><br/>
     <input type="text" name="requiredFirstName" id="requiredFirstName"  value="<?php echo isset($_POST['requiredFirstName']) ? $_POST['requiredFirstName'] : '';?>" size="20" />   
     <input type="text" name="requiredMiddleInitial" id="requiredMiddleInitial" value="<?php echo isset($_POST['requiredMiddleInitial']) ? $_POST['requiredMiddleInitial'] : '';?>" size="1" maxlength="1" />   
     <input type="text" name="requiredLastName" id="requiredLastName" value="<?php echo isset($_POST['requiredLastName']) ? $_POST['requiredLastName'] : '';?>" size="20" />
     <br/><br/>
     <span class="mainTextNoIndent"><strong>Telephone Number:</strong><br/>
     *Numbers Only</span><br/>
     <input type="text" name="requiredTelephoneNumber" id="requiredTelephoneNumber" value="<?php echo isset($_POST['requiredTelephoneNumber']) ? $_POST['requiredTelephoneNumber'] : '';?>" size="20" maxlength="10" />
     <br/><br/>
     <span class="mainTextNoIndent"><strong>Email Address:</strong></span><br/>
     <input type="text" name="requiredEmailAddress" id="requiredEmailAddress" value="<?php echo isset($_POST['requiredEmailAddress']) ? $_POST['requiredEmailAddress'] : '';?>" size="30" />
     <br/><br/>
      <span class="mainTextNoIndent"><strong>Home Address: </strong></span><br/>
     <textarea name="requiredHomeAddress" id="requiredHomeAddress" value="<?php echo isset($_POST['requiredHomeAddress']) ? $_POST['requiredHomeAddress'] : '';?>" rows="3" cols="40"></textarea>
     <br/><br/>
     <span class="mainTextNoIndent"><strong>Security Clearance:</strong><br/>
     List current security clearance.</span><br/>
     <select name="requiredSecurityClearance" id="requiredSecurityClearance" value="<?php echo isset($_POST['requiredSecurityClearance']) ? $_POST['requiredSecurityClearance'] : '';?>" size="1">
        <option>None</option> 
        <option>Confidential</option> 
        <option>Secret</option> 
        <option>Top Secret</option> 
        <option>SCI</option> 
      </select>
      <br/><br/>
      <span class="mainTextNoIndent"><strong>Education: </strong><br/>
      List any degrees earned. Provide degree, subject, degree level, school and year graduated.</span><br/><br/>
     <textarea name="requiredEducation" id="requiredEducation" value="value="<?php echo isset($_POST['requiredEducation']) ? $_POST['requiredEducation'] : '';?>"" rows="4" cols="85"></textarea>
     <br/><br/>
      <span class="mainTextNoIndent"><strong>Professional Experience: </strong><br/>
      List current and previous work experience. The most current position should be listed first in this format. Include dates of employment,<br/> title, and company. Each position should have a narrative paragraph describing your accomplishments in the position.</span><br/><br/>
     <textarea name="requiredProfessionalExperience" id="requiredProfessionalExperience" value="value="<?php echo isset($_POST['requiredProfessionalExperience']) ? $_POST['requiredProfessionalExperience'] : '';?>"" rows="8" cols="85"></textarea>
     <br/><br/>
     <span class="mainTextNoIndent"><strong>Professional Licenses and Certifications:  </strong><br/>
      List any professional licenses and certifications that the candidate has obtained, date obtained and if applicable, expiration date.</span><br/><br/>
     <textarea name="requiredProfessionalLicenses" id="requiredProfessionalLicenses" value="value="<?php echo isset($_POST['requiredProfessionalLicenses']) ? $_POST['requiredProfessionalLicenses'] : '';?>"" rows="8" cols="85"></textarea>
     <br/><br/>
     <span class="mainTextNoIndent"><strong>Computer Proficiency: </strong><br/>
     List computer experience including operating  systems, hardware, software, applications,  databases, etc.</span><br/><br/>
     <textarea name="requiredComputerProficiency" id="requiredComputerProficiency" value="value="<?php echo isset($_POST['requiredComputerProficiency']) ? $_POST['requiredComputerProficiency'] : '';?>"" rows="8" cols="85"></textarea>
     <br/><br/>
     <span class="mainTextNoIndent"><strong>Miscellaneous: </strong><br/>
     Professional Associations and Memberships, Foreign Languages, Patents, Publications, and Awards</span>
     <br/><br/>
     <textarea name="Miscellaneous" id="Miscellaneous" value="value="<?php echo isset($_POST['Miscellaneous']) ? $_POST['Miscellaneous'] : '';?>"" rows="8" cols="85"></textarea>
     <br/><br/>
     <span class="mainTextNoIndent"><strong>* Please ensure your email address is correct before submitting.</strong></span><br/>
     <input type="submit" name="submit" value="Submit" /><br/>
    </div>
  <br/>
</form>
<?php
    if (isset($_POST['submit']))
    {
        // Get Information from form
        $FirstName=$_POST['requiredFirstName'];
        $MiddleInitial=$_POST['requiredMiddleInitial'];
        $LastName=$_POST['requiredLastName'];
        $TelephoneNumber=$_POST['requiredTelephoneNumber'];
        $EmailAddress=$_POST['requiredEmailAddress'];
        $HomeAddress=$_POST['requiredHomeAddress'];
        $SecurityClearance=$_POST['requiredSecurityClearance'];
        $Education=$_POST['requiredEducation'];
        $ProfessionalExperience=$_POST['requiredProfessionalExperience'];
        $ProfessionalLicenses=$_POST['requiredProfessionalLicenses'];
        $ComputerProficiency=$_POST['requiredComputerProficiency'];
        $Miscellaneous=$_POST['Miscellaneous'];
        
        if (empty($FirstName)) {
        //$error = 'You did not fill in your first name.';
            alert("The "+$FirstName+" field is a required field.");
        } 
        else if (empty($MiddleInitial)) {
            $error = 'You did not fill in your middle initial.';
        } 
        else if (empty($LastName)) {
            $error = 'You did not fill in your last name.';
        } 
        else if (empty($TelephoneNumber)) {
            $error = 'You did not fill in your telephone number.';
        } 
        else if (empty($EmailAddress)) {
            $error = 'You did not fill in your email address.';
        } 
        else if (empty($HomeAddress)) {
            $error = 'You did not fill in your home address.';
        } 
        else if (empty($Education)) {
            $error = 'You did not fill in the education.';
        } 
        else if (empty($ProfessionalExperience)) {
            $error = 'You did not list your professional experience.';
        } 
        else if (empty($ProfessionalLicenses)) {
            $error = 'You did not list your professional licenses and certifications.';
        } 
        echo $error;
        if (!$error) {
        //verify EmailAddress is actually valid
        $EmailAddress = trim($EmailAddress);
        $_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+";
        $_host = "([-0-9A-Z]+\.)+";
        $_tlds = "([0-9A-Z]){2,4}$/i";
        
        $to = "jsale@silverback7.com";
        $subject = "Online Resume Submittion";//.", From: ".$FirstName." ".$LastName;
        $EmailAddress=$_POST['EmailAddress'];//reset EmailAddress
        $from = "From: ".$EmailAddress;

        $message = "First Name: \n".$FirstName." ";
        $message .= "\n\n\nMiddle Initial: \n".$MiddleInitial." ";
        $message .= "\n\n\nLast Name: \n".$LastName." ";
        $message .= "\n\n\nTelephone Number: \n".$TelephoneNumber." ";
        $message .= "\n\n\nEmail Address: \n".$EmailAddress." ";
        $message .= "\n\n\nHome Address: \n".$HomeAddress." ";
        $message .= "\n\n\nSecurity Clearance: \n".$SecurityClearance." ";
        $message .= "\n\n\nEducation: \n".$Education." ";
        $message .= "\n\n\nProfessional Experience: \n".$ProfessionalExperience." ";
        $message .= "\n\n\nProfessional Licenses: \n".$ProfessionalLicenses." ";
        $message .= "\n\n\nComputer Proficiency: \n".$ComputerProficiency." ";
        $message .= "\n\n\nMiscellaneous: \n".$Miscellaneous." ";
        
        mail($to, $subject, $message, $from); //Send Email 
        echo "Thank you for your application, we will be in contact with you shortly if you meet the job description requirements.";
        echo '<meta content="4; URL=index.htm" http-equiv="Refresh" />';//go back to homepage after they read the thank you message
        }
    }
?>                    

thank you thorpe also but i have a question what does the ? in between the to posts do?

Link to comment
Share on other sites

Alright, i took the two suggestions and put them in place, but now when the form is not filled out or the email address is not in proper format it will still be submitted and emailed, please help!

<?php
if (isset($_POST['submit']))
{
// Get Information from form
$FirstName=$_POST['requiredFirstName'];
$MiddleInitial=$_POST['requiredMiddleInitial'];
$LastName=$_POST['requiredLastName'];
$TelephoneNumber=$_POST['requiredTelephoneNumber'];
$EmailAddress=$_POST['requiredEmailAddress'];
$HomeAddress=$_POST['requiredHomeAddress'];
$SecurityClearance=$_POST['requiredSecurityClearance'];
$Education=$_POST['requiredEducation'];
$ProfessionalExperience=$_POST['requiredProfessionalExperience'];
$ProfessionalLicenses=$_POST['requiredProfessionalLicenses'];
$ComputerProficiency=$_POST['requiredComputerProficiency'];
$Miscellaneous=$_POST['Miscellaneous'];

$errors = array();

$EmailAddress = trim($EmailAddress);
$_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+";
$_host = "([-0-9A-Z]+\.)+";
$_tlds = "([0-9A-Z]){2,4}$/i";

if (trim($_POST['requiredFirstName']) == "") {
	$errors[] = "First name is required.";
}
if (trim($_POST['requiredMiddleInitial']) == "") {
	$errors[] = 'You did not fill in your middle initial.';
} 
if (trim($_POST['requiredLastName']) == "") {
	$errors[] = 'You did not fill in your last name.';
} 
if (trim($_POST['requiredTelephoneNumber']) == "") {
	$errors[] = 'You did not fill in your telephone number.';
}
if(preg_match($_name."@".$_host.$_tlds,$email)){
	$errors[] = 'Your email address is not in the correct format.';
}
if (trim($_POST['requiredEmailAddress']) == ""){
	$errors[] = 'You did not fill in your email address.';
} 
if (trim($_POST['requiredHomeAddress']) == "") {
	$errors[] = 'You did not fill in your home address.';
} 
if (trim($_POST['requiredEducation']) == "") {
	$errors[] = 'You did not fill in the education field.';
} 
if (trim($_POST['requiredProfessionalExperience']) == "") {
	$errors[] = 'You did not list your professional experience.';
} 
if (trim($_POST['requiredProfessionalLicenses']) == "") {
	$errors[] = 'You did not list your professional licenses and certifications.';
} 
if (trim($_POST['requiredComputerProficiency']) == "") {
	$errors[] = 'You did not state your computer proficiency.';
} 

if (count($errors) == 0) { 
	// no errors, send emails
	//verify EmailAddress is actually valid
	$to = "no1@sumplace.com";
	$subject = "Online Resume Submittion";
	$EmailAddress=$_POST['EmailAddress'];//reset EmailAddress
	$from = "From: ".$EmailAddress;

	$message = "First Name: \n".$FirstName." ";
	$message .= "\n\n\nMiddle Initial: \n".$MiddleInitial." ";
	$message .= "\n\n\nLast Name: \n".$LastName." ";
	$message .= "\n\n\nTelephone Number: \n".$TelephoneNumber." ";
	$message .= "\n\n\nEmail Address: \n".$EmailAddress." ";
	$message .= "\n\n\nHome Address: \n".$HomeAddress." ";
	$message .= "\n\n\nSecurity Clearance: \n".$SecurityClearance." ";
	$message .= "\n\n\nEducation: \n".$Education." ";
	$message .= "\n\n\nProfessional Experience: \n".$ProfessionalExperience." ";
	$message .= "\n\n\nProfessional Licenses: \n".$ProfessionalLicenses." ";
	$message .= "\n\n\nComputer Proficiency: \n".$ComputerProficiency." ";
	$message .= "\n\n\nMiscellaneous: \n".$Miscellaneous." ";

	mail($to, $subject, $message, $from); //Send Email 
	echo "Thank you for your application, we will be in contact with you shortly if you meet the job description requirements.";
	echo '<meta content="4; URL=index.htm" http-equiv="Refresh" />'; //go back to homepage after they read the thank you message
}
 else {
	// there are errors. display them.
	echo "Please correct the following errors:<BR>";
	echo implode('<BR>',$errors);
}
}
?>                    

and here is a sample from the form

<textarea name="requiredEducation" id="requiredEducation" value="value="<?php echo isset($_POST['requiredEducation']) ? $_POST['requiredEducation'] : '';?>"" rows="4" cols="85"></textarea>

Link to comment
Share on other sites

add this to your script (above where you call the alert() functions):

 

function alert($msg){

$msg = htmlentities($msg, ENT_QUOTES);

if (!empty($msg)){

echo '<script type="text/javascript">
alert("Error: ' . $msg . '")
</script>';

}}

 

EDIT: NVM that was for your original script lol

Link to comment
Share on other sites

<?php
if (isset($_POST['submit']))
{
// Get Information from form
$FirstName=$_POST['requiredFirstName'];
$MiddleInitial=$_POST['requiredMiddleInitial'];
$LastName=$_POST['requiredLastName'];
$TelephoneNumber=$_POST['requiredTelephoneNumber'];
$EmailAddress=$_POST['requiredEmailAddress'];
$HomeAddress=$_POST['requiredHomeAddress'];
$SecurityClearance=$_POST['requiredSecurityClearance'];
$Education=$_POST['requiredEducation'];
$ProfessionalExperience=$_POST['requiredProfessionalExperience'];
$ProfessionalLicenses=$_POST['requiredProfessionalLicenses'];
$ComputerProficiency=$_POST['requiredComputerProficiency'];
$Miscellaneous=$_POST['Miscellaneous'];

$errors = array();

$EmailAddress = trim($EmailAddress);
$_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+";
$_host = "([-0-9A-Z]+\.)+";
$_tlds = "([0-9A-Z]){2,4}$/i";

if (trim($_POST['requiredFirstName']) == "") {
	$errors[] = "First name is required.";
}
if (trim($_POST['requiredMiddleInitial']) == "") {
	$errors[] = 'You did not fill in your middle initial.';
} 
if (trim($_POST['requiredLastName']) == "") {
	$errors[] = 'You did not fill in your last name.';
} 
if (trim($_POST['requiredTelephoneNumber']) == "") {
	$errors[] = 'You did not fill in your telephone number.';
}
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['requiredEmailAddress'])){
$errors[] = "The email you have entered is not a valid email format.";
}
if (trim($_POST['requiredEmailAddress']) == ""){
	$errors[] = 'You did not fill in your email address.';
} 
if (trim($_POST['requiredHomeAddress']) == "") {
	$errors[] = 'You did not fill in your home address.';
} 
if (trim($_POST['requiredEducation']) == "") {
	$errors[] = 'You did not fill in the education field.';
} 
if (trim($_POST['requiredProfessionalExperience']) == "") {
	$errors[] = 'You did not list your professional experience.';
} 
if (trim($_POST['requiredProfessionalLicenses']) == "") {
	$errors[] = 'You did not list your professional licenses and certifications.';
} 
if (trim($_POST['requiredComputerProficiency']) == "") {
	$errors[] = 'You did not state your computer proficiency.';
} 

if (count($errors) == 0) { 
	// no errors, send emails
	//verify EmailAddress is actually valid
	$to = "no1@sumplace.com";
	$subject = "Online Resume Submittion";
	$EmailAddress=$_POST['EmailAddress'];//reset EmailAddress
	$from = "From: ".$EmailAddress;

	$message = "First Name: \n".$FirstName." ";
	$message .= "\n\n\nMiddle Initial: \n".$MiddleInitial." ";
	$message .= "\n\n\nLast Name: \n".$LastName." ";
	$message .= "\n\n\nTelephone Number: \n".$TelephoneNumber." ";
	$message .= "\n\n\nEmail Address: \n".$EmailAddress." ";
	$message .= "\n\n\nHome Address: \n".$HomeAddress." ";
	$message .= "\n\n\nSecurity Clearance: \n".$SecurityClearance." ";
	$message .= "\n\n\nEducation: \n".$Education." ";
	$message .= "\n\n\nProfessional Experience: \n".$ProfessionalExperience." ";
	$message .= "\n\n\nProfessional Licenses: \n".$ProfessionalLicenses." ";
	$message .= "\n\n\nComputer Proficiency: \n".$ComputerProficiency." ";
	$message .= "\n\n\nMiscellaneous: \n".$Miscellaneous." ";

	mail($to, $subject, $message, $from); //Send Email 
	echo "Thank you for your application, we will be in contact with you shortly if you meet the job description requirements.";
	echo '<meta content="4; URL=index.htm" http-equiv="Refresh" />'; //go back to homepage after they read the thank you message
}
 else {

echo '<center>The following errors have occurred:<br /><br />';

for ($i=0; $i < count($errors); $i++){

	echo '<font color="red">' . $errors[$i] . '</font><br />';

}

echo '</center>';

}
}
?>

Link to comment
Share on other sites

You can redirect it to page.php#errors and add

 

else {

 

echo '<div align="center" name="errors">The following errors have occurred:<br /><br />';

 

for ($i=0; $i < count($errors); $i++){

 

echo '<font color="red">' . $errors[$i] . '</font><br />';

 

}

 

echo '</div>';

 

}

}

?>

Link to comment
Share on other sites

You can redirect it to page.php#errors and add

else {

echo '<div align="center" name="errors">The following errors have occurred:<br /><br />';

for ($i=0; $i < count($errors); $i++){

	echo '<font color="red">' . $errors[$i] . '</font><br />';

}

echo '</div>';

}
}
?>

 

Line breaks :P lol

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.