What I do for form validation is I call the same page for display,
validation and database entries.
In the action part of the html form I use:
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'];?>" method="post"
enctype="multipart/form-data" name="add_fleet" id="add_fleet">
This way when the submit button is clicked, the page calls itself.
To make this work you will need to set a 'hidden field':
<input name="mode" type="hidden" id="mode" value="add">
Then the whole process is as simple as checking to see if:
$_POST['mode'] == 'add'
if it does branch off to validate the fields and if the fields fail set
an $error variable. Then before you do your database insert, check to see:
isset($error)
If it is then do not do an insert but continue to display the form with
the fields filled out with what they already entered. If $error is not
set, then continue with the INSERT and if all goes well just redirect
the page to whatever you want such as a confirmation page.
I hope this is not too confusing. Here is some code that works.
################################################## ##############
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<table width="500" border="1" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td align="center">
<?php
##### Check if form was Submitted #####
if($_POST['mode'] == 'add'){
### Retrieve the variables ###
if($_POST['field1'] == ""){
$error = "* Please fill in Field #1<br>";
}else{
$field1 = $_POST['field1'];
}
if($_POST['field2'] == ""){
$error .= "* Please fill in Field #2<br>";
}else{
$field2 = $_POST['field2'];
}
if($_FILES['file']['name'] == ""){
$error .= "* Please select a File to upload<br>";
}else{
$file_name = $_FILES['file']['name'];
/* Process file*/
}
### If fields are valid then attempt an insert ###
### If not set $error and bypass the insert ###
if(!isset($error)){
$insert = "INSERT INTO $table(
Field1,
Field2,
FileName)VALUES(
'$field1',
'$field2',
'$file_name')";
if(!$result = mysql_query($insert)){
$error .= "Error in inserting<br>";
}else{ ### If insert goes well, redirect to another page ###
?>
<script language="JavaScript">
<!--
self.location="<?php echo "confirmation.php";?>"
//-->
</script>
<?
}
}//if(!isset($error)){
### If there was an error display it ###
if(isset($error)){
echo "<table width=\"300\" border=\"0\" cellspacing=\"0\"
cellpadding=\"0\">";
echo "<tr>";
echo "<td bgcolor=\"#FF0000\">".$error."</td>";
echo "</tr>";
echo "</table>";
}
}//if($_POST['mode'] == 'add'){
?>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'];?>"
method="post" enctype="multipart/form-data" name="add_fields"
id="add_fields">
<table width="500" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="right">Field #1: </td>
<td><input name="field1" type="text" id="field1" value="<?php
echo $field1;?>"></td>
</tr>
<tr>
<td align="right">Field #2: </td>
<td><input name="field2" type="text" id="field2" value="<?php
echo $field2;?>"></td>
</tr>
<tr>
<td align="right">File Upload: </td>
<td><input type="file" name="file">
<br><?php echo $file_name;?></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="Submit"
value="Submit">
<input type="reset" name="Submit2" value="Cancel"
onClick="self.location='redirect.php'">
<input name="mode" type="hidden" id="mode" value="add"> </td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
################################################## #################
Marius III wrote:
Thanks for all the help.
I figure that the Browse button is placed there with the INPUT TYPE="FILE"
Tag.
But if the user submit that form and not all form data is filled in then I
display a page which tells him to "Fill in all your fields" with a BACK
link. Now how can I save the data when the user click the Back link. Cause
normally if he clicks my back link the form is reset and all previous
entered data is lossed.
Thank you!