[CODE] PHP Upload PHP form

Upload core product.
Post Reply
Mcooper
Posts: 5
Joined: Fri Nov 12, 2010 3:25 pm

[CODE] PHP Upload PHP form

Post by Mcooper »

Thought I would share this here maybe save someone else some time in the future.

This is a form done in PHP I did for our web site.

What is does.

On page load it checks to see if the form has been submitted if it has not it displays the form and jfileupload applet. the user will then fill out the form and then select there files for upload once the upload is complete the form will automatically be submitted using the callback function JSTransferCompleted.

The form will then check to see what CSR the user selected and it will email that CSR the information the user filled out.

This is all done with a single php file and some javascript

This is unstyled code it is just the basic frame work of the form you will have to style it and change it to your needs.

This also has live form Validation using http://www.geektantra.com/2009/09/jquer ... alidation/

You will need to get that Validation script if you want the Validation to work.

The JFileUpload applet is setup to send the file to our FTP. so this is not doing any HTTP stuff with the uploaded file.

You will probably see some of the other callbacks in there I left them in there for maybe future use. I tired to comment all the code but It is not perfect and my code probably is not the best in the world.

Code: Select all

<html>
<head>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
            jQuery(function(){
                jQuery("#contact_email").validate({
                    expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
                    message: "Should be a valid Email Address"
                });                
				jQuery("#contact_name").validate({
                    expression: "if (VAL) return true; else return false;",
                    message: "Please enter the Required field"
                });
                jQuery("#contact_phone").validate({
                    expression: "if (VAL) return true; else return false;",
                    message: "Please enter the Required field"
                });
            });
           
</script>


<script type="text/javascript"> 
<!--
function JSAppletInitialized(version)
{
    
}
function JSTransferStarted(filename, filesize)
{

}
// Set two form values of callback then submit the form.
function JSTransferCompleted(filelist)
{

   document.upload.file_name.value=filelist;
   document.upload.complete.value='complete';
   document.upload.submit();
}
 
function JSTransferCancelled()
{

}
  
//-->
</script>

</head>
<body>

<?php
// Check to see if the form has been submitted or not. If not submitted display else go to email the form
if (!isset($_POST['complete'])) {
?>
    
<form name="upload" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

<p><input type="hidden" name="file_name" id="file_name" value="" /></p>  

<p><label for="contact_name">Contact Name:</label><input type="text" name="contact_name" id="contact_name" value="" /></p> 
 
<p><label for="contact_email">Contact Email:</label><input  type="text" name="contact_email" id="contact_email" value="" /></p> 
 
<p ><label for="contact_phone">Contact Phone:</label><input  type="text" name="contact_phone" id="contact_phone" value="" /></p> 
  
<p ><label for="comments">Comments:</label> 
<textarea name="comments" cols="20" rows="6" id="comments"></textarea></p> 

<!--User select a CSR they want to email--> 
<p ><label for="CSR">Customer Service Representative:</label><select name="csr"> 
<option value="0">(select)</option> 
<option value="1">name1</option> 
<option value="2">name2/option> 
<option value="3">name3</option> 
<option value="4">name4</option> 
<option value="5">name5</option> 
<option value="6">name6</option> 
<option value="7">name7</option>
<option value="8">name8</option> 
</select></p>

<p><script src="applet_ftp.js"></script></p>

<p><label for="complete"></label><input type="hidden" name="complete" id="complete" value="" /></p> 

</form>


<?php
}
else {
	// Get the data from the form and check it for bad code.
	$contactName = check_input($_POST['contact_name']);
	$contactEmail = check_input($_POST['contact_email']);
	$contactPhone = check_input($_POST['contact_phone']);
	$fileName = check_input($_POST['file_name']);
	$comments = check_input($_POST['comments']);
	$csr = check_input($_POST['csr']);
	
	// Check CSR value and then assign value to $email
	if ($csr == 1) { 
    $email = 'name1@somewhere.com'; 
    } 
	elseif ($csr == 2) { 
    $email = 'name2@somewhere.com'; 
    } 
	elseif ($csr == 3) { 
    $email = 'name3@somewhere.com'; 
    } 
    elseif ($csr == 4) { 
    $email = 'name4@somewhere.com'; 
    }
    elseif ($csr == 5) { 
    $email = 'name5@somewhere.com'; 
    }
    elseif ($csr == 6) { 
    $email = 'name6@somewhere.com'; 
    }
    elseif ($csr == 7) { 
    $email = 'name7@somewhere.com'; 
    }
    elseif ($csr == 8) { 
    $email = 'name8@somewhere.com'; 
    }
	else { 
    $email = 'name9@somewhere.com'; 
	} 

// Assign $contactEmail to From: and Reply-to 
$sender = 'From:'. $contactEmail . "\r\n" .
   'Reply-To:'. $contactEmail . "\r\n" .
   'X-Mailer: PHP/' . phpversion();


// the subject of the email
$subject = "Testing Upload Form - Upload from $contactName File: $fileName";	

// Email format
$message = "New Upload on FTP 

File Name: $fileName

Contact Information:

Name: $contactName
E-mail: $contactEmail
Phone: $contactPhone

Comments:
$comments

End of message
";

// Send the email
mail($email, $subject, $message, $sender);
echo 'Email Sent';



}
?>

</body>
</html>


<?php
// Function to check for bad code.
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        die($problem);
    }
    return $data;
}
?>

Post Reply