Jump to content

Recommended Posts

I have been working on a file submission form for a conference. The form has seven required fields - two of which are option boxes. The user is also required to select a file of a particular type (pdf/doc/docx/rtf).

 

Currently, when a user neglects to enter the required data, they are alerted with a related error message that provides them with further instructions. This works, so long as the user selects the wrong file type. eg. If I entered my first name, second name, title and selected a jpg, an error informing me that I have not included an email address will appear.

 

The problem, is that if I select a file in the correct format (pdf/doc/docx/rtf), the file will upload and a confirmation email is sent to the user, regardless of whether they have entered data into all required fields or not. Users are; however, required to put in their first and last names (which is why I find it particularly odd ie. working for some required fields, but not others...).

 

Any help would be much appreciated - the quicker the better too  :-\ .

 

 

PHP (I have ommitted the xHTML, but then the PHP makes the form self-explanatory):

<?php

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$title = $_POST['title'];
$email = $_POST['email'];
$organisation = $_POST['organisation'];
$position = $_POST['position'];
$session_type = $_POST['session_type'];

// max size in bytes
define('MAX_FILE_SIZE', 2097152); 
if (array_key_exists('btn', $_POST)) {
	// define new constant which contains the path to the upload folder
	define('upload_path','uploads/');
	// find the extension
	$flext = pathinfo($_FILES['frmfile']['name']);
	// store extension
	$ext = strtolower($flext['extension']); 
	// get name from field and replace existing filename
	$file = str_replace(' ', '_', $session_type . '_' . $first_name . '_' . $last_name . '.' . $ext); 
	// change to lowercase
	$file = strtolower($file); 
	// calculate in kilobytes
	$maxfs = number_format(MAX_FILE_SIZE/1024, 1).'KB';
	$fsize = false;
	// check the file size
	if ($_FILES['frmfile']['size'] > 0 && $_FILES['frmfile']['size'] <= MAX_FILE_SIZE) {
		$fsize = true;
	} 
	// allow MIME file types
	$filetype = array('application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword','application/pdf','application/rtf'); 
	// set false before verification
	$ftype = false;
	// check allowed
	foreach($filetype as $type) {
		if ($type == $_FILES['frmfile']['type']) {
			$ftype = true;
			break;
		}
	} 
	// on type verification, move tmp to folder, else report error
	if ($ftype && $fsize && $session_type && $first_name && $last_name != '') {
		switch($_FILES['frmfile']['error']) {
			case 0:

			$EmailFrom = "email@email.com";
			$EmailTo = $email;

			$EmailTo  = $email . ', ';
			$EmailTo .= 'email@email.com'; 



			$Subject = "Conference Name Submission Confirmation";
			$headers = "From: email@email.com\r\n";
			$headers .= "Reply-To: ". $email . "\r\n";
			$headers .= "Return-Path: ". $email . "\r\n";
			$headers .= "MIME-Version: 1.0\r\n";
			$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

			$Body = '<html><body style="' . 'font-family: calibri, arial, sans-serif;"' . '>';
			$Body .= "	
				<p>Dear <b>" . $first_name . ' ' . $last_name ."</b>,</p>
				<p>This email is confirmation that <a href=" . "http://www.url.com/" . upload_path . $file . " title=" . "Uploaded file" . ">your " . $session_type . " submission</a> for the <a href=" . "http://www.url.com/" . ">Conference Name</a> was successful.<br />
				If you would like to make changes to your submission, please re-enter the same details originally provided, as outlined below, and upload a new file &#40;this will overwrite the existing file&#41;.</p>
				<p>We will keep the following details on file and keep you informed throughout the various stages of the review process.<br />
					<b>Name: </b>" . $title . ' ' . $first_name . ' ' . $last_name ."<br>
					<b>Email: </b><a href=" . "mailto:" . $email .">" . $email . "</a><br>
					<b>Organisation: </b>" . $organisation ."<br>
					<b>Position: </b>" . $position ."<br>
					<b>Sesstion Type: </b>" . $session_type ."<br>
					<b>File: </b><a href=" . "http://www.url.com/" . upload_path . $file . " title=" . "Uploaded file" . ">" . "http://www.url.com/" . upload_path . $file . "</a>
				</p>
				<p>If you have any further queries or would like to make changes to your details, please reply to this email or call &#43;00 0 0000 0000.<p>
				<p>Regards,</p>";
			$Body .='
			<p style="color: #666;"><i><b>John Doe</b></i><br />
			Position Name<br />
			Address Line 1<br />
			Address Line 2<br />
			Address Line 3<br />
			Address Line 4<br /><br />
			<b>Phone:</b> +00-0-0000 0000<br />
			<b>web:</b> <a href="http://www.url.com</a><br />
			<b>web:</b> <a href="http://www.url.com">www.url.com</a><br /><br />
			<b>Conference Name:</b> <a href="http://www.url.com">www.url.com</a>
			</p>
			';
			$Body .= "</body></html>";
			$success = mail($EmailTo,$Subject,$Body,$headers);				

			// move file to the upload folder
			$upload = move_uploaded_file($_FILES['frmfile']['tmp_name'],upload_path.$file);
			if ($upload) {
				$msg = '<p class="success">The file was successfully uploaded to the server. You will recieve a confirmation email soon. Feel free to view your submission online at any time via <a href="' . upload_path . $file . '" title="Uploaded file">this link</a>.</p>';
			} else {
				$msg = 'Error, please try again.';
			}
			break;
			case 3:
			$msg = 'Error, please try again.';
			break;
			default:
			$msg = 'Error, please contact <a href="mailto:email@email.com">John Doe</a>.';
		}
	} elseif ($_FILES['frmfile']['error'] == 4) {
		$msg = 'Please select file.';
	} elseif ($first_name == '') {
		$msg = 'Please provide your first name.';
	} elseif ($last_name == '') {
		$msg = 'Please provide your last name.';
	} elseif ($title == 'Select') {
		$msg = 'Please select a title.';
	} elseif ($email == '') {
		$msg = 'Please provide your email address.';
	} elseif ($organisation == '') {
		$msg = 'Please list your organisation.';
	} elseif ($position == '') {
		$msg = 'Please list your position.';
	} elseif ($session_type == 'Select') {
		$msg = 'Please select a session type.';
	} else {
		$msg = $_FILES['frmfile']['name'].' cannot be uploaded.';
		if(!$ftype) {
			$msg .= 'Allowed file types include: Microsoft Word &#40;.doc, .docx&#41;, Adobe Acrobat &#40;.pdf&#41; and Rich Text &#40;.rtf&#41;.';
		}
		if(!$fsize) {
			$msg .= 'Maximum file size is '.$maxfs.'.';
		}
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/206958-required-fields-ignored/
Share on other sites

Given that the logic that is validating the contents of the form fields is at the end of your code and it is only being executed if the upload ['error'] element is a 4, what do you expect?  You send the email after only checking if the form data exists and the upload worked, not after you have validated all of the data. Computers only do exactly what their code tells them to do.

 

You would need to put all the validation logic first, then only process the information if there are no validation errors. I recommend using an array to hold the validation errors and to set an element for each validation error, then you can simply test if the array is empty or not to determine if there are any validation errors. If the array is empty, no errors, process the information.

Thank you. I have since reviewed my code, which perhaps I should have done more closely before posting on here.

 

Changed this line:

if ($ftype && $fsize && $session_type && $first_name && $last_name != '') {

 

To:

if ($ftype && $fsize && $first_name && $last_name && $email && $organisation && $position != '' && $title && $session_type !='Select') {

 

As you can see, I excluded some variables... It now works as required.

 

Thank you for your assistance.

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.