oo7ml
01-24-2007, 06:00 AM
I have a form that loads another php script (upload.processor.php) once the form is submitted. The "upload.processor.php" checks the forms validation and upload validation. However i want to separate the form validation from the upload validation. If there is something wrong with the form (such as a blank field) i want this to be picked up and the error displayed above the form..... Then if the forms validation is passed, then it loads "upload.processor.php" to check the upload validation.
Does anyone know how i could separate my code to function as described above? (and if possible, why the email validation is not really working)
Here is the code i have for the join.form.php (i'l post the upload.processor.php on a reply to this thread as i am over the limit)
join.form.php
<?php
<?php
if($_POST)
{
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("whoishot") or die(mysql_error());
//////////////////////START OF FORM VALIDATION//////////////////////////////////////////////////////////////////////////
// checks whether fields are blank
if (!$_POST['firstname'] || !$_POST['lastname'] || !$_POST['username'] || !$_POST['password'] || !$_POST['email'] ) {
UploadForm('Error: You did not complete all of the required fields!');
}
// checks whether username is already in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM accounts WHERE username = '$usercheck'") or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
(mysql_num_rows($check) !== 0) or UploadForm('Sorry, the username '.$_POST['username'].' is already in use.');
// set variables
// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the directory that will recieve the uploaded files
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
// make a note of the location of the success page
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';
// name of the fieldname used for the file in the HTML form
$fieldname = 'file';
// Now let's deal with the upload
// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded',
2 => 'html form max file size exceeded',
3 => 'file upload was only partial',
4 => 'no file was attached');
// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0)
or UploadForm('Error: '.$errors[$_FILES[$fieldname]['error']], $uploadForm);
// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
or UploadForm('Error: that file is not an HTTP upload');
// validation... since this is an image upload script we
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'])
or UploadForm('Error: only image uploads are allowed');
// make a unique filename for the uploaded file and check it is
// not taken... if it is keep trying until we find a vacant one
$now = time(); //current time stamp
$path_parts = pathinfo($_FILES[$fieldname]['name']);
while( file_exists($uploadFilename = $uploadsDirectory . $now++ . "." . $path_parts["extension"]) );
// now let's move the file to its final and allocate it with the new filename
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or UploadForm('Server error: receiving directory insuffiecient permission');
// send form to the database
//retrieve form data in a variable
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$file = $_POST['file'];
// Insert a row of information into the table
mysql_query("INSERT INTO accounts (firstname, lastname, username, password, email, file) VALUES( '$firstname' , '$lastname' , '$username', '$email', '$uploadFilename') ");
// If you got this far, everything has worked and the file has been successfully saved.
// We are now going to redirect the client to the success page.
header('Location: ' . $uploadSuccess);
}
else
{
UploadForm();
}
function sticky($in)
{
if(isset($_POST[$in]))
{
return (get_magic_quotes_gpc()?stripslashes($_POST[$in]):$_POST[$in]);
}
return '';
}
function UploadForm($message = '', $max_file_size = 500000)
{
?><!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>
<html>
<head>
<title>template</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<link rel=stylesheet href=stylesheet.css type=text/css>
<style type="text/css">
#error_message{color:red;font-weight:bold;}
</style>
</head>
<body>
<table width=759 border=0 cellspacing=0 cellpadding=0>
<tr>
<td width="759"><img src="Images/headers/join.jpg" width="189" height="90"></td>
</tr>
<tr>
<td><form id="Upload" action="" enctype="multipart/form-data" method="post">
<table width="77%" border="0" cellspacing="1" cellpadding="2">
<tr>
<td> </td>
<td><span id="error_message"><?php echo $message ?></span></td>
</tr>
<tr bgcolor="#efefef">
<td width="28%" class="form">First Name:</td>
<td width="72%"><input type="text" name="firstname" value="<?php echo sticky('firstname') ?>" size="35"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Last Name:</td>
<td><input type="text" name="lastname" value="<?php echo sticky('lastname') ?>" size="35"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Username:</td>
<td><input type="text" name="username" value="<?php echo sticky('username') ?>" size="30"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Password:</td>
<td><input type="password" name="password" value="<?php echo sticky('password') ?>" size="30"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Email:</td>
<td><input type="text" name="email" value="<?php echo sticky('email') ?>" size="35"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Submit Your Photo:</td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>" /> <input id="file" type="file" name="file" size="40"/>
</td>
</tr>
<tr>
<td height="82"><div align="center"><img src="Images/warning.jpg" width="80" height="67"></div></td>
<td colspan="2" class="form_right">
<?php echo file_get_contents('upload_warning.txt') ?>
</td>
</tr>
<tr>
<td> </td>
<td height="35" colspan="2" class="text">By joining you accept the
whoishot.ie <a class="linksHeader" href="termsofservice.html" target="_blank">Terms
of Service</a></td>
</tr>
<tr>
<td height="35"> </td>
<td> <input name="submit" type="submit" id="submit" value="Create my Account">
<input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>
<br></td>
</tr>
</table>
</body>
</html><?php
die();
}
?>
Does anyone know how i could separate my code to function as described above? (and if possible, why the email validation is not really working)
Here is the code i have for the join.form.php (i'l post the upload.processor.php on a reply to this thread as i am over the limit)
join.form.php
<?php
<?php
if($_POST)
{
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("whoishot") or die(mysql_error());
//////////////////////START OF FORM VALIDATION//////////////////////////////////////////////////////////////////////////
// checks whether fields are blank
if (!$_POST['firstname'] || !$_POST['lastname'] || !$_POST['username'] || !$_POST['password'] || !$_POST['email'] ) {
UploadForm('Error: You did not complete all of the required fields!');
}
// checks whether username is already in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM accounts WHERE username = '$usercheck'") or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
(mysql_num_rows($check) !== 0) or UploadForm('Sorry, the username '.$_POST['username'].' is already in use.');
// set variables
// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the directory that will recieve the uploaded files
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
// make a note of the location of the success page
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';
// name of the fieldname used for the file in the HTML form
$fieldname = 'file';
// Now let's deal with the upload
// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded',
2 => 'html form max file size exceeded',
3 => 'file upload was only partial',
4 => 'no file was attached');
// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0)
or UploadForm('Error: '.$errors[$_FILES[$fieldname]['error']], $uploadForm);
// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
or UploadForm('Error: that file is not an HTTP upload');
// validation... since this is an image upload script we
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'])
or UploadForm('Error: only image uploads are allowed');
// make a unique filename for the uploaded file and check it is
// not taken... if it is keep trying until we find a vacant one
$now = time(); //current time stamp
$path_parts = pathinfo($_FILES[$fieldname]['name']);
while( file_exists($uploadFilename = $uploadsDirectory . $now++ . "." . $path_parts["extension"]) );
// now let's move the file to its final and allocate it with the new filename
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or UploadForm('Server error: receiving directory insuffiecient permission');
// send form to the database
//retrieve form data in a variable
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$file = $_POST['file'];
// Insert a row of information into the table
mysql_query("INSERT INTO accounts (firstname, lastname, username, password, email, file) VALUES( '$firstname' , '$lastname' , '$username', '$email', '$uploadFilename') ");
// If you got this far, everything has worked and the file has been successfully saved.
// We are now going to redirect the client to the success page.
header('Location: ' . $uploadSuccess);
}
else
{
UploadForm();
}
function sticky($in)
{
if(isset($_POST[$in]))
{
return (get_magic_quotes_gpc()?stripslashes($_POST[$in]):$_POST[$in]);
}
return '';
}
function UploadForm($message = '', $max_file_size = 500000)
{
?><!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>
<html>
<head>
<title>template</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<link rel=stylesheet href=stylesheet.css type=text/css>
<style type="text/css">
#error_message{color:red;font-weight:bold;}
</style>
</head>
<body>
<table width=759 border=0 cellspacing=0 cellpadding=0>
<tr>
<td width="759"><img src="Images/headers/join.jpg" width="189" height="90"></td>
</tr>
<tr>
<td><form id="Upload" action="" enctype="multipart/form-data" method="post">
<table width="77%" border="0" cellspacing="1" cellpadding="2">
<tr>
<td> </td>
<td><span id="error_message"><?php echo $message ?></span></td>
</tr>
<tr bgcolor="#efefef">
<td width="28%" class="form">First Name:</td>
<td width="72%"><input type="text" name="firstname" value="<?php echo sticky('firstname') ?>" size="35"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Last Name:</td>
<td><input type="text" name="lastname" value="<?php echo sticky('lastname') ?>" size="35"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Username:</td>
<td><input type="text" name="username" value="<?php echo sticky('username') ?>" size="30"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Password:</td>
<td><input type="password" name="password" value="<?php echo sticky('password') ?>" size="30"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Email:</td>
<td><input type="text" name="email" value="<?php echo sticky('email') ?>" size="35"></td>
</tr>
<tr bgcolor="#efefef">
<td class="form">Submit Your Photo:</td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>" /> <input id="file" type="file" name="file" size="40"/>
</td>
</tr>
<tr>
<td height="82"><div align="center"><img src="Images/warning.jpg" width="80" height="67"></div></td>
<td colspan="2" class="form_right">
<?php echo file_get_contents('upload_warning.txt') ?>
</td>
</tr>
<tr>
<td> </td>
<td height="35" colspan="2" class="text">By joining you accept the
whoishot.ie <a class="linksHeader" href="termsofservice.html" target="_blank">Terms
of Service</a></td>
</tr>
<tr>
<td height="35"> </td>
<td> <input name="submit" type="submit" id="submit" value="Create my Account">
<input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>
<br></td>
</tr>
</table>
</body>
</html><?php
die();
}
?>