thesaleboat Posted August 21, 2008 Share Posted August 21, 2008 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 = "[email protected]"; $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 } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/ Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 Where is $row defined? Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-622146 Share on other sites More sharing options...
thesaleboat Posted August 21, 2008 Author Share Posted August 21, 2008 ??? hmm very good point is there something else i should use for the value? or how would i define $row? Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-622199 Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-622202 Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 In your form, everywhere where you have... <?php echo $row['requiredName'];?> for example needs to be changed to.... <?php echo isset($_POST['requiredName']) ? $_POST['requiredName'] : '';?> Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-622203 Share on other sites More sharing options...
thesaleboat Posted August 26, 2008 Author Share Posted August 26, 2008 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 = "[email protected]"; $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? Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626031 Share on other sites More sharing options...
thesaleboat Posted August 26, 2008 Author Share Posted August 26, 2008 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 = "[email protected]"; $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> Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626497 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626503 Share on other sites More sharing options...
thesaleboat Posted August 27, 2008 Author Share Posted August 27, 2008 i took out the javascript, but you think i could do this instead of having it echo? because the echo never happens and it just redirects to index.htm Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626505 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 <?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 = "[email protected]"; $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>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626506 Share on other sites More sharing options...
thesaleboat Posted August 27, 2008 Author Share Posted August 27, 2008 HEY! Thanks man... I can finally go home now , is there any way to have the page scroll back to the bottom if there is an error? so that the user can see the problem? Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626516 Share on other sites More sharing options...
thesaleboat Posted August 27, 2008 Author Share Posted August 27, 2008 Thanks again Andy-H, im gonna head home now but ill check back tommorrow. Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626522 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 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>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626527 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 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 lol Quote Link to comment https://forums.phpfreaks.com/topic/120730-check-form-fields-before-email/#findComment-626528 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.