I have a script below that currently works. Very simply, you can submit information to a MySQL DB via this form, as well as upload a file. The name of the file is stored in the DB, along with the other information input. Addtionally, the file is uploaded to the specified directory.
To check for form validation, I have an error array set up. If you leave the title field blank, you are returned to the form and asked to fill in the title... but the audience field 'keeps' the information you submitted previously. The same validation happens for the audience field.
My predicament: what if a user has elected to upload a file, but leaves one of the required fields blank? What happens to the uploaded file? In other words, I want to keep the uploaded file in temp until the form is validated and all the information is submitted. I can pass the audience and title information to the next form via POST, but how do I pass the uploaded file information?
In short: how can I 'keep' the uploaded file while the form is still validating? I would need to show the user that the file is still there. I would also need to give them the option of changing the file they uploaded.
Any thoughts?
<?
$host = "xxxxxx";
$user = "xxxxxx";
$pass = "xxxxxx";
$db = "xxxxxx";
$PHP_SELF = $_SERVER['PHP_SELF'];
$action = @$_GET['action'];
if ($action) { $action = $action; }
else {
header("Location: $PHP_SELF?action=new");
exit;
}
/************ NEW LESSON *************/
if ($action=="new")
{
?>
<form enctype="multipart/form-data" action="<? echo $PHP_SELF ?>?action=add" method="post" name="newlesson">
Title:<br>
<input name="title" type="text" size="40" maxlength="75"><br><br>
Audience:<br>
<input name="audience" type="text" size="40" maxlength="50">
<br><br>Image:<br><input name="userfile" type="file"><br><br>
<input type="submit" name="Submit" value="Submit"></form>
<?
include("footer.php");
exit;
}
//**********************************//
function handleupload() {
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$file = $_FILES['userfile']['name'];
$realname = str_replace(" ", "_", $file); //replace spaces with underscore
copy($_FILES['userfile']['tmp_name'], "../images/lessons/".$realname);
unset($_FILES['userfile']['tmp_name']);
return $realname;
} else {
unset($_FILES['userfile']['tmp_name']);
return false;
}
}
/************ ADD LESSON *************/
if ($action=="add")
{
// set up error list array
$errorList = array();
$count = 0;
// validate text input fields
if ((!$_POST['title'] || !$_POST['audience'])) { $missingfields = "Error: The fields marked in red below cannot be left blank."; }
if (!$_POST['title']) { $errorList[$count] = "Title"; $titleerror = "<img src=\"invalidfield.gif\"> "; $count++; }
if (!$_POST['audience']) { $errorList[$count] = "Audience"; $audienceerror = "<img src=\"invalidfield.gif\"> "; $count++; }
// check for errors
if (sizeof($errorList) == 0)
{
MYSQL_CONNECT($host,$user,$pass) OR DIE("Unable to connect to database");
@mysql_select_db("$db") or die("Unable to select database");
$title = strip_tags(@$_POST['title']);
$audience = strip_tags(@$_POST['audience']);
$image = handleupload();
$query = "INSERT INTO lessons(title,audience,image) VALUES('$title','$audience','$image')";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
MYSQL_CLOSE();
header("Location: $PHP_SELF?action=new");
}
else
{
// errors occurred
echo "$missingfields";
$title = @$_POST['title'];
$audience = @$_POST['audience']; ?>
<form enctype="multipart/form-data" action="<? echo $PHP_SELF ?>?action=add" method="post" name="newlesson">
<? IF ($titleerror) { echo $titleerror; } ?>Title:<br>
<input name="title" type="text" size="40" maxlength="75" value="<? echo @$title; ?>"><br><br>
<? IF ($audienceerror) { echo $audienceerror; } ?>Audience:<br>
<input name="audience" type="text" size="40" maxlength="50" value="<? echo @$audience; ?>">
<br><br>Image:<br><input name="userfile" type="file"><br><br>
<input type="submit" name="Submit" value="Submit"></form>
<?
}
exit;
}
?>