Page 1 of 1

form validation error in php

Posted: Sat Jan 28, 2012 11:49 am
by danielbala
Hi..

Im new to php..
This is my form validate code..
im getting errors...i did my best to do this code

Can anyone help me and correct my code


<?php
session_start();
// submit button clicked
if(isset($_POST['submit'])){
$errors=array();
// Continue when clicked
if(isset($_POST['Role'], $_POST['Name'], $_POST['datepicker'], $_POST['gender'], $_POST['activity'], $_POST['Address'], $_POST['Photo'], $_POST['Biodata'], $_POST['certificates'], $_POST['Salary'])){
// Fields are all set else error : No errors conintue
// Set variables an sanitise
$Emp_Save= mysql_real_escape_string($_POST['Emp_Save']); // Sanitise
$Emp_Cancel=$_POST['Emp_Cancel']; // Sanitise
$Role=$_POST['Role']; // Sanitise
$Name=$_POST['Name']; // Sanitise
$datepicker=$_POST['datepicker']; // Sanitise
$gender=$_POST['gender']; // Sanitise
$activity=$_POST['activity']; // Sanitise
$Address=$_POST['Address']; // Sanitise
$Photo=$_POST['Photo']; // Sanitise
$Biodata=$_POST['Biodata']; // Sanitise
$certificates=$_POST['certificates']; // Sanitise
$Salary=$_POST['Salary']; // Sanitise

//-- Continue with error checks ;;

if($_Role == ""){
$errors[]='Please Select The Employee Role!';
}

if(strlen($Name >30){
$errors[]='Name Is Too Long!';
}
else if(empty($Name))
{
$errors[]='Please Fill The Field!';
}


// Vslidate Datepicker
if(strlen($datepicker) != 10){
$errors[]='Date should only be 10 characters and number .';
}
// explode datepaicker by what ever youre using to separate them
$datepicker_ex = explode('/', $datepicker);
// Check that theyre numbers
if(!is_numeric($datepicker_ex[0]) || !is_numeric($datepicker_ex[1]) || !is_numeric($datepicker_ex[2])){
$errors[]='Date should only be number .';
} else {
// they are number format them back and cast to int , just incase
$datepicker = (int)$datepicker_ex[0].'/'.(int)$datepicker_ex[1].'/'.(int)$datepicker_ex[2];
}
// END - date picker valiadation

else if($gender == ""){
$errors[]='Please Select The Gender!';
}

else if($Activity == ""){
$errors[]='Please Select the Status!';
}

else if($Address == ""){
$errors[]='Please Enter The Address!';


if(array_key_exists('Photo', $_FILES)) {
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg');
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
// Validate the uploaded file
if($_FILES['Photo']['size'] === 0
|| empty($_FILES['Photo']['tmp_name'])) {
echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['Photo']['size'] > 50000) {
echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['Photo']['error'] !== UPLOAD_ERR_OK) {
// There was a PHP error
echo("<p>There was an error uploading.</p>\r\n");
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
}




if(array_key_exists('Biodata', $_FILES)) {
$allowed_filetypes = array('.pdf');
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
// Validate the uploaded file
if($_FILES['Biodata']['size'] === 0
|| empty($_FILES['Biodata']['tmp_name'])) {
echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['Biodata']['size'] > 50000) {
echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['Biodata']['error'] !== UPLOAD_ERR_OK) {
// There was a PHP error
echo("<p>There was an error uploading.</p>\r\n");
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
}


if(array_key_exists('certificates', $_FILES)) {
$allowed_filetypes = array('.pdf');
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
// Validate the uploaded file
if($_FILES['certificates']['size'] === 0
|| empty($_FILES['certificates']['tmp_name'])) {
echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['certificates']['size'] > 50000) {
echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['certificates']['error'] !== UPLOAD_ERR_OK) {
// There was a PHP error
echo("<p>There was an error uploading.</p>\r\n");
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
}


elseif($_EmpSalary == ""){
$errors[]='Please Enter The Salary!';
}

//----- End error checks do insert or display errors of any are found --
if(empty($errors)){
// do insert
$query = mysql_query("INSERT INTO `formdetails` (role,name,,date_of_birth,gender,activity,address,photo,biodata,certificates,salary) VALUES ('$Role', '$Name','$datepicker',, '$gender','$activity','$Address','$Photo','$Biodata','$certificates','$_EmpSalary')");

} else {
foreach($errors as $error){
// display errors
echo $error;
}
}

//---------------------------------------------------
} else {
$errors[]= 'All fields required.';
}
}
// END --
?>

Re: form validation error in php

Posted: Sat Jan 28, 2012 12:36 pm
by Celauran
What errors are you getting? Also, please use syntax tags; your code is an illegible mess.

Re: form validation error in php

Posted: Sat Jan 28, 2012 1:21 pm
by danielbala
I CORRETCED THE CODE..

but still not getting the output..

<?php
session_start();
// submit button clicked
if(isset($_POST['submit'])){
$errors=array();
// Continue when clicked
if(isset($_POST['Role'], $_POST['Name'], $_POST['datepicker'], $_POST['gender'], $_POST['activity'], $_POST['Address'], $_POST['Photo'], $_POST['Biodata'], $_POST['certificates'], $_POST['Salary'])){
// Fields are all set else error : No errors conintue
// Set variables an sanitise
$Emp_Save= mysql_real_escape_string($_POST['Emp_Save']); // Sanitise
$Emp_Cancel=$_POST['Emp_Cancel']; // Sanitise
$Role=$_POST['Role']; // Sanitise
$Name=$_POST['Name']; // Sanitise
$datepicker=$_POST['datepicker']; // Sanitise
$gender=$_POST['gender']; // Sanitise
$activity=$_POST['activity']; // Sanitise
$Address=$_POST['Address']; // Sanitise
$Photo=$_POST['Photo']; // Sanitise
$Biodata=$_POST['Biodata']; // Sanitise
$certificates=$_POST['certificates']; // Sanitise
$Salary=$_POST['Salary']; // Sanitise

//-- Continue with error checks ;;

if($_Role == ""){
$errors[]='Please Select The Employee Role!';
}

else if(strlen($Name >30)){
$errors[]='Name Is Too Long!';
}
else if(empty($Name))
{
$errors[]='Please Fill The Field!';
}


// Vslidate Datepicker
else if(strlen($datepicker) != 10){
$errors[]='Date should only be 10 characters and number .';
}
// explode datepaicker by what ever youre using to separate them
$datepicker_ex = explode('/', $datepicker);
// Check that theyre numbers
if(!is_numeric($datepicker_ex[0]) || !is_numeric($datepicker_ex[1]) || !is_numeric($datepicker_ex[2])){
$errors[]='Date should only be number .';
} else {
// they are number format them back and cast to int , just incase
$datepicker = (int)$datepicker_ex[0].'/'.(int)$datepicker_ex[1].'/'.(int)$datepicker_ex[2];
}
// END - date picker valiadation

if(empty($gender)){
$errors[]='Please Select The Gender!';
}

if(empty($Activity)){
$errors[]='Please Select the Status!';
}

if(empty($Address)){
$errors[]='Please Enter The Address!';


if(array_key_exists('Photo', $_FILES)) {
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg');
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
// Validate the uploaded file
if($_FILES['Photo']['size'] === 0
|| empty($_FILES['Photo']['tmp_name'])) {
echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['Photo']['size'] > 50000) {
echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['Photo']['error'] !== UPLOAD_ERR_OK) {
// There was a PHP error
echo("<p>There was an error uploading.</p>\r\n");
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
}




if(array_key_exists('Biodata', $_FILES)) {
$allowed_filetypes = array('.pdf');
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
// Validate the uploaded file
if($_FILES['Biodata']['size'] === 0
|| empty($_FILES['Biodata']['tmp_name'])) {
echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['Biodata']['size'] > 50000) {
echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['Biodata']['error'] !== UPLOAD_ERR_OK) {
// There was a PHP error
echo("<p>There was an error uploading.</p>\r\n");
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
}


if(array_key_exists('certificates', $_FILES)) {
$allowed_filetypes = array('.pdf');
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
// Validate the uploaded file
if($_FILES['certificates']['size'] === 0
|| empty($_FILES['certificates']['tmp_name'])) {
echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['certificates']['size'] > 50000) {
echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['certificates']['error'] !== UPLOAD_ERR_OK) {
// There was a PHP error
echo("<p>There was an error uploading.</p>\r\n");
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
}


if(empty($_EmpSalary)){
$errors[]='Please Enter The Salary!';
}

//----- End error checks do insert or display errors of any are found --
if(empty($errors)){
// do insert
$query = mysql_query("INSERT INTO `formdetails` (role,name,,date_of_birth,gender,activity,address,photo,biodata,certificates,salary) VALUES ('$Role', '$Name','$datepicker',, '$gender','$activity','$Address','$Photo','$Biodata','$certificates','$_EmpSalary')");

} else {
foreach($errors as $error){
// display errors
echo $error;
}
}

//---------------------------------------------------
} else {
$errors[]= 'All fields required.';
}
}
// END --
?>

Re: form validation error in php

Posted: Sat Jan 28, 2012 1:24 pm
by danielbala
please help me to get the correct code validation...

Re: form validation error in php

Posted: Sat Jan 28, 2012 1:51 pm
by Celauran
You have mismatched braces.

Re: form validation error in php

Posted: Sat Jan 28, 2012 2:15 pm
by danielbala
yes....can u please check the file upload code.........???

I just did the form validation..like..

1.check all the fields whether they r empty??

2.validate datepicker

3.validate biodata,certificates fields whether they r empty,uploading file size,file format like it shud upload only pdf format..

4.validate photo whether they r empty,uploading file size,file format like it shud upload only jpeg,jpg,bmp,gif,png format..