Hi all,
I’m a little bit confused as to why my form doesn’t behave as expected. The $errmsg never seems to display when a particular field has not been provided with a value, so I never get for example ‘You did not include a title’.
This form is very much incomplete still, but I just can’t see why the basic validation logic I’m going for doesn’t work.
Erm…why? Any thoughts very welcome. I may be missing something really stupid here.
<?php
/**
*
*/
//Include configuration file first
require_once dirname(__FILE__). '/includes/config.inc.php';
//Include the language file set by config.inc.php
require_once $config['app_dir']. '/lang/' . $config['language'] . '.php';
//Include the functions file
require_once $config['app_dir']. '/includes/functions_inc.php';
/**
* Establish a connection to the database.
* We can use the config values defined in config.php as arguments for the class
*/
require ('classes/dbutility.class.php');
//Instantiate the dbutility class. Connects to db instantly using config credentials
$db = new dbUtil($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);
/**
* Page processing
*/
$title = $lang['upload_title'];
$heading = $lang['upload_heading'];
$description = $lang['upload_description'];
$pagelink = '"index.php">Thumbs Gallery';
include_once $config['app_dir']. '/includes/xhtml_headnav.html';
//Setup defaults for form processing
$clean = array();
$errors = 0;
//VALIDATE FORM ENTRIES
if (isset($_POST['submit'])) {
$errmsg = '';
if (isset($_POST['title'])) {
//validate title
$is_title_valid = validateTitle($_POST['title']);
if ($is_title_valid === TRUE){
$clean['title'] = htmlentities($_POST['title']);
} else {
$errors++;
$errmsg .= 'Title must be no longer than 100 characters';
}
} else {
$errors++;
$errmsg .= 'You did not include a title';
}
if (isset($_POST['description'])) {
//validate description
$is_description_valid = validateDescription($_POST['description']);
if ($is_description_valid === TRUE){
$clean['description'] = htmlentities($_POST['description']);
} else {
$errors++;
$errmsg .= 'Description must be between 10 and 255 characters';
}
} else {
$errors++;
$errmsg .= 'You did not provide a description';
}
if (isset($_POST['userfile'])) {
//validate image upload
/*
* Check the file is a jpeg image that has definately been provided by the form, see hoe-ans_uploading_files
*/
} else {
$errors++;
$errmsg .= 'You have not included a file to upload';
}
}
//Ready to render page, so we initilise the output variable
$output = '';
//If form is submitted with no errors ACTION DATA
if (isset($_POST['submit']) && $errors == 0) {
//place tile, description and path into the table, then upload image
$output .= '<p>Your image has been uploaded.</p>';
//after data has been processed, close db connection END OF PROCESSING
$db->close();
} else {
//(Re)display the upload form
$output .='
<form enctype="multipart/form-data" action="' .$_SERVER['PHP_SELF']. '" method="post">
<fieldset>
<div>
<label for="ttl">Title</label>
<input type="text" name="title" id="ttl" size="40" value="' .(isset($clean['title']) ? htmlentities($clean['title']) : ''). '"/>
</div>
<div>
<label for="desc">Description</label>
<textarea name="description" id="desc" rows="5" cols="30">' .(isset($clean['description']) ? htmlentities($clean['description']) : ''). '</textarea>
</div>
<div>
<label>Upload Image Location</label>
<input name="userfile" type="file"/>
</div>
<div>
<input type="submit" value="Submit Image" name="submit"/>
</div>
</fieldset>
</form>'."\
";
//Any errors should be displayed to the user also
if ($errors > 0) {
//If errors found, display these to user
$output .= '<p>Errors in your submission: ' .$errmsg. '</p>'. "\
";
}
} //close the final else statement
//Echo the generated xhtml
echo $output;
//Include the html footer to end the page
include_once $config['app_dir']. '/includes/xhtml_footer.html';
?>