$php_mysql$ Posted August 3, 2011 Share Posted August 3, 2011 friends to check form error and print out errors in form i use ///////////////////////////////////Post Error Check function postErrorCheck($dataError){ $errormsg = array(); if($dataError['ads_title'] == '') { $errormsg['ads_title'] = 'Advertisement title is required!'; }else if(strlen($dataError['ads_title']) > 100) { $errormsg['ads_title'] = 'Title cannot be more than 100 characters!'; } if($dataError['postersname'] == '') { $errormsg['postersname'] = 'Your name is required!'; }else if(strlen($dataError['postersname']) > 50) { $errormsg['postersname'] = 'Name cannot be more than 50 characters'; } if($dataError['category'] == '0') { $errormsg['category'] = 'Please select a category!'; } return $errormsg; } but i also got a image upload field on my form so to check image upload error i use this function ////////////////////////////////////image error function uploadFile($file, $uploadPath) { print 'file name'.$file['name']; if (is_uploaded_file($file['tmp_name']) && $file['error']==0) { $path = $uploadPath . $file['name']; if (!file_exists($path)) { if (move_uploaded_file($file['tmp_name'], $path)) { echo "The file was uploaded successfully."; return $path; } else { echo "The file was not uploaded successfully."; } } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $file['error'] . ")"; } } now what i want is to put the image upload error function inside the Post Error Check function in a similar way but dunno how to combine it so that i could print the error message for my user for image upload error in similar way using this <?php if($error['ads_title'] !='') { echo '<div class="error_msg">'.$error['ads_title'].'</div>'; } ?> any help would be really greatful Quote Link to comment https://forums.phpfreaks.com/topic/243707-combining-error-message-inside-form-error-function/ Share on other sites More sharing options...
$php_mysql$ Posted August 4, 2011 Author Share Posted August 4, 2011 any help in here please? Quote Link to comment https://forums.phpfreaks.com/topic/243707-combining-error-message-inside-form-error-function/#findComment-1251628 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 <?PHPSensei To The Rescue Option 1: Your Upload function shouldn't be printing the error, it should return FALSE OR TRUE if the image was uploaded in your case. See below. I also see that the error message is printed inside the file upload function, remove that and migrate it over to your check form function, then print the error all together over there So What Can I Do Sensei? Ill use your code as the example function uploadFile($file, $uploadPath) { if (is_uploaded_file($file['tmp_name']) && $file['error']==0) { $path = $uploadPath . $file['name']; if (!file_exists($path)) { if (move_uploaded_file($file['tmp_name'], $path)) { return true; return $path; } else { return false; } } else { Now this is how you would utilize the function in your other code. ///////////////////////////////////Post Error Check function postErrorCheck($dataError){ $errormsg = array(); if($dataError['ads_title'] == '') { $errormsg['ads_title'] = 'Advertisement title is required!'; }else if(strlen($dataError['ads_title']) > 100) { $errormsg['ads_title'] = 'Title cannot be more than 100 characters!'; }else if($dataError['postersname'] == '') { $errormsg['postersname'] = 'Your name is required!'; }else if(strlen($dataError['postersname']) > 50) { $errormsg['postersname'] = 'Name cannot be more than 50 characters'; }elseif($dataError['category'] == '0') { $errormsg['category'] = 'Please select a category!'; }elseif(!uploadFile($file,$uploadPath)){ // error not uploaded }else{ // uplaod success } return $errormsg; } Quote Link to comment https://forums.phpfreaks.com/topic/243707-combining-error-message-inside-form-error-function/#findComment-1251641 Share on other sites More sharing options...
$php_mysql$ Posted August 4, 2011 Author Share Posted August 4, 2011 ay bro could you also show me how to check for upload error supposing image exist or image larger than 10mb with error message like $errormsg['image'] = 'image name already exist !'; etc etc Quote Link to comment https://forums.phpfreaks.com/topic/243707-combining-error-message-inside-form-error-function/#findComment-1251651 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 check my pm. You can store uploadFile($file,$uploadPath) inside a variable, and depending on the error number it returns, you can print THAT error.. $upload = uploadFile($file,$uploadPath); if($upload == 1){ // file too large }elseif($upload == 2){ // too small } Quote Link to comment https://forums.phpfreaks.com/topic/243707-combining-error-message-inside-form-error-function/#findComment-1251655 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Alright I got a pm asking you still don't understand. Whats happening is that uploadFile and postErrorCheck are both printing out their own messages. You need to have only 1 function doing the error handling, while the other one returns true or flase, in this case for the image upload you need to check wether the upload was successful, returning true or false, and handle that data inside postErrorCheck. Now I don't know even know why your not specifying the file's actual name inside the file upload. it should be $file['filename']['tmp_name']; Which confused me on how you are passing these arguments through. Post your entire code, i will re-arrange it for you. Quote Link to comment https://forums.phpfreaks.com/topic/243707-combining-error-message-inside-form-error-function/#findComment-1251666 Share on other sites More sharing options...
$php_mysql$ Posted August 4, 2011 Author Share Posted August 4, 2011 ok here is my complete code ///////////////////////////////////Post Error Check function postErrorCheck($dataError){ $errormsg = array(); if($dataError['ads_title'] == '') { $errormsg['ads_title'] = 'Advertisement title is required!'; }else if(strlen($dataError['ads_title']) > 100) { $errormsg['ads_title'] = 'Title cannot be more than 100 characters!'; } if($dataError['postersname'] == '') { $errormsg['postersname'] = 'Your name is required!'; }else if(strlen($dataError['postersname']) > 50) { $errormsg['postersname'] = 'Name cannot be more than 50 characters'; } if($dataError['category'] == '0') { $errormsg['category'] = 'Please select a category!'; } if($dataError['ads_type'] == '0') { $errormsg['ads_type'] = 'Please select a type!'; } if($dataError['state'] == '0') { $errormsg['state'] = 'Please select a state!'; } if($dataError['ads_location'] == '') { $errormsg['ads_location'] = 'City or Town name is required!'; }else if(strlen($dataError['ads_location']) > 50) { $errormsg['ads_location'] = 'City or Town name cannot be more than 100 characters!'; } if($dataError['email'] == '') { $errormsg['email'] = 'E-mail address is required!'; }else if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $dataError['email'])) { $errormsg['email'] = 'Enter a valid email address!'; } if($dataError['phone'] == '') { $errormsg['phone'] = 'Enter your contact number!'; }else if(!is_numeric($dataError['phone'])) { $errormsg['phone'] = 'Phone number must be numeric!'; }else if(strlen($dataError['phone']) < 10) { $errormsg['phone'] = 'Insert a valid 10 digits number!'; }else if(strlen($dataError['phone']) > 10) { $errormsg['phone'] = 'Insert a valid 10 digits number!'; } if($dataError['description'] == '') { $errormsg['description'] = 'Advertisement description is required!'; } return $errormsg; } ////////////////////////////////////image error function uploadFile($file, $uploadPath) { print 'file name'.$file['name']; if (is_uploaded_file($file['tmp_name']) && $file['error']==0) { $path = $uploadPath . $file['name']; if (!file_exists($path)) { if (move_uploaded_file($file['tmp_name'], $path)) { echo "The file was uploaded successfully."; return $path; } else { echo "The file was not uploaded successfully."; } } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $file['error'] . ")"; } } ///////////////////////////////////Insert Ad function insertAd($postData) { global $uploadPath; $imagepath = uploadFile($_FILES['image'], $uploadPath); // code to resize and save the original image $image = new SimpleImage(); $image->load($imagepath); $image->resize(204,250); $thumb_path = 'image_thumbs/thumb_'.$_FILES['image']['name']; $image->save($thumb_path); //image resize end unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET adtitle = '".$postData['ads_title']."', image = '".$thumb_path."', postersname = '".$postData['postersname']."', category = '".$postData['category']."', adtype = '".$postData['ads_type']."', state = '".$postData['state']."', location = '".$postData['ads_location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."' "; executeSql($sql); } really appreciate ur help Quote Link to comment https://forums.phpfreaks.com/topic/243707-combining-error-message-inside-form-error-function/#findComment-1251678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.