Help with form looping and error checking...
Posted: Wed Nov 09, 2005 12:36 am
feyd | Please use
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.
Please help ,me. Thanks!
feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
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.Code: Select all
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;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.
Please help ,me. Thanks!
feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]