Hello. I really need some help with my form processor. The script below processes the submission of the user. The user may submit 2-10 or more tutorials at one time using a single form. The form is already working well, but the processor needs some more features on it.
I need an error checking feature, but I'm unsure on how to do it. I want to check 1st if everything on the form is filled up before any submission will be processed. My current code has an error checking, but if a user forget to fill the 2nd tutorial, the 1st tutorial will be added and the 2nd tutorial error will not be added and an error message will be printed.
The tendency of this is that the user would press the "back" button and correct the errors, and "click submit again" which will re-enter the 1st tutorial again to the database, making a duplicate entry.
// submission form
case 'tutorials';
$pagename="Submit Tutorial";
include("top.php");
if(!$_POST["num"])
{
?>
How many tutorials would you like to submit?<br />
<form method="post" action="submit.php?mode=tutorials">
<input type="text" name="num" value="1" size="15" maxlength="3" /> <input type="submit" value="Go!" />
</form>
<?php
}
else
{
// display form
?>
Fields marked with * are optional.<br />
<form method="post" action="submit.php?mode=gotutorials" enctype="multipart/form-data">
<?php
for($i=0;$_POST["num"]>$i;$i++)
{
?>
<h2>Tutorial <?php echo $i+1; ?></h2>
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<tr class="mainbox1">
<td valign="top"><strong>Tutorial Title:</strong><br /><small>(Format: Tutorial Title - Short description here) Max.: 75 chars.</small></td><td><input type="text" name="tut_title<?=$i;?>" maxlength="75" size="40" /></td>
</tr>
<tr class="mainbox2">
<td valign="top"><strong>Tutorial URL:</strong><br /><small>(Complete URL to the tutorial page. Please include "http://")</small></td><td><input type="text" name="tut_url<?=$i;?>" value="http://" size="40" /></td>
</tr>
<tr class="mainbox1">
<td valign="top"><strong>Source:</strong><br /><small>(Ex: mydomain.com, subdomain.domain.com, domain.com/subdir. Do not include "http://")</small></td><td><input type="text" name="web_url<?=$i;?>" maxlength="250" size="40" /></td>
</tr>
<tr class="mainbox2">
<td valign="top"><strong>Category:</strong></td>
<td valign="top">
<select name="cat_id<?=$i;?>">
<option value="">Please Select Below</option>
<?php
$qCat="select * from tut_categ order by cat_name asc ";
$rsCat=mysql_query($qCat) or die("MySQL Error: $qCat ." . mysql_error());
while($rowCat=mysql_fetch_array($rsCat))
{
extract($rowCat);
echo "<option value=\"$cat_id\">$cat_name</option>";
}
?>
</select></td>
</tr>
<tr class="mainbox1">
<td valign="top"><strong>Tutorial Icon: *</strong><br /><small>(Only 40x40 gif/jpg icons are allowed and it should not be greater than 7kb. If you don't have an icon, leave this blank and check 'No Icon'.)</small></td>
<td>
<input type="file" name="image<?=$i;?>" size="20" />
<input type="checkbox" name="noicon<?=$i;?>" value="no" />No Icon</td>
</tr>
</table><br /><br />
<?php
} //end of for
?>
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<tr class="mainbox2">
<td valign="top"> </td><td valign="top" align="right"><input type="submit" value="Submit Tutorial(s)" /></td>
</tr>
</table><br />
</form>
<?php
} // end of else
break;
// processing part
case 'gotutorials';
$pagename="Submit Tutorial";
include("top.php");
$d="select * from phpbb_users where username='$member' ";
$rd=mysql_query($d) or die("MySQL Error: $d . " .mysql_error());
$rowd=mysql_fetch_array($rd);
$user_id=$rowd["user_id"];
$c = 0;
while($_POST["tut_title".$c])
{
$tut_title=trim($_POST["tut_title".$c]);
$tut_url=trim($_POST["tut_url".$c]);
$web_url=trim($_POST["web_url".$c]);
$cat_id=$_POST["cat_id".$c];
$noicon=$_POST["noicon".$c];
$image=$_POST["image".$c];
$x=$c+1;
// check for empty fields
if(empty($tut_title) || empty($tut_url) || $tut_url=="http://" || empty($web_url) || empty($cat_id))
{
echo "<b>Incomplete Infos for Tutorial #$x</b><br />Please go back in fill in all the required fields!<br /><br />";
echo "$bottom";
include("footer.php");
die;
}
// start file upload code
if(!empty($noicon))
{
$q="insert into tutorials (id,tut_title,tut_url,web_url,cat_id,icon,added,user_id) VALUES('','$tut_title','$tut_url','$web_url','$cat_id','none',now(),'$user_id') ";
$rs=mysql_query($q) or die("MySQL Error: $q ." . mysql_error());
if($rs) // if successfully added
{
echo "<b>Tutorial #$x Added</b><br />Thank you! The tutorial has been successfully added on our tutorial indexing database.<br /><br />";
}
else
{
echo "<b>Error</b><br />An error occured, please try again. If the error persists, please contact the admin.";
echo "$bottom";
include("footer.php");
die;
}
}
// start of file upload
if(empty($noicon))
{
$max_image_size = '8192'; // this has to be in bytes
$max_image_width = '40'; // in pixels
$max_image_height = '40';
$image_accepted = array("image/jpeg","image/gif","image/pjpeg");
// Validation for Image
// 1) validate for byte size
if ($_FILES['image'.$c]['size'] > $max_image_size)
{
die ("<b>Icon Error for Tutorial #$x</b><br />Image uploaded is over the maximum byte size allowed.<br /><br />");
}
// 2) validate for dimension size
$dim = getimagesize($_FILES['image'.$c]['tmp_name']);
if ($dim[0] != $max_image_width || $dim[1] != $max_image_height)
{
die ("<b>Icon Error for Tutorial #$c</b><br />Image uploaded is not of the required dimensions.<br /><br />");
}
// 3) Validate for image type
if (!in_array($_FILES['image'.$c]['type'], $image_accepted))
{
die ("<b>Icon Error for Tutorial #$c</b><br />Image uploaded is not one of the accepted type.<br /><br />");
}
$icon = $_FILES['image'.$c]['name'];
// file renaming
$unique_id = md5(uniqid(time()));
$icon = $unique_id.'_'.$icon;
if (move_uploaded_file($_FILES['image'.$c]['tmp_name'], '/home/aoi/public_html/uploaded_icons/'.$icon))
{
$q="insert into tutorials (id,tut_title,tut_url,web_url,cat_id,icon,added,user_id) VALUES('','$tut_title','$tut_url','$web_url','$cat_id','uploaded_icons/$icon',now(),'$user_id') ";
$rs=mysql_query($q) or die("MySQL Error: $q ." . mysql_error());
if($rs) // if successfully added
{
echo "<b>Tutorial $c Added</b><br />Thank you! The tutorial has been successfully added on our tutorial indexing database.<br /><br />";
}
else
{
echo "<b>Error $c</b><br />An error occured, please try again. If the error persists, please contact the admin.<br /><br />";
echo "$bottom";
include("footer.php");
die;
}
}
}
$c++;
}// end while
break;
Somebody suggested me to use javascript to check first if the form has been filled up completely. But on the other hand, what if the image has an error? Either it's too big or has an invalid file format.
So, I want that when the use submits the form, it will check first all the errors and if an error is found, the script won't input anything to the mysql database unless tha user has submitted an errorless submission.
I really need to make this thing work. I've been trying to figure it out for weeks and I also tried to ask help, but I got no solution until now. So, please help me... :queasy: