php - Do you see why my code is not entering in my else condition? -
i have form insert image, title, content , gallery of images news.
for main image of news , gallery images, want validation selected image type. want allow images jpg, gif , png. if user post image of type, want show error message saying 'only jpg, png or gif images accepted.'
but im havin problem, validation seems working, when fill every fields correctly im not entering in else status insert.
im not entering here:
else{ echo 'test'; // test never appear //here have insert not entering here } do see might problem?
so have form:
<form method="post" enctype="multipart/form-data"> <div class="label"> <span class="field">title</span> <input type="text" name="titulo" /> </div> <div> <span>main image:</span> <input type="file" name="img" accept="image/gif, image/jpg, image/jpeg, image/png" /> </div> <div class="galerry"> <div> <span class="field">gallery:</span> <input type="file" name="gb[]" multiple="multiple" accept="image/gif, image/jpg, image/jpeg, image/png" /> </div> </div><!--/gallery--> <input type="submit" value="insert" name="sendform"/> </form> and have php code:
if(isset($_post['sendform'])){ $f['title'] = $_post['title']; $f['content'] =$_post['content']; $img = $_files['img']; $extperm = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'); $gb = $_files['gb']; print_r($img); if(in_array('',$f) || empty($_files['img']['tmp_name'])){ echo 'please fill fields'; } else if(!in_array($img['type'],$extperm)){ echo 'only jpg, png or gif images accepted.'; } else if($_files['gb']['tmp_name'][0]){ print_r($_files['gb']); $count= count($_files['gb']['tmp_name']); for($i=0;$i<$count;$i++){ if(!in_array($gb['type'][$i],$extperm)){ echo 'only jpg, png or gif images accepted.'; } } } else { echo 'test'; // test never appear //here have insert not entering here } }
for kind of thing utilize flag variable, $fine, :
$fine = true; if(in_array('',$f) || empty($_files['img']['tmp_name'])){ echo 'please fill fields'; $fine = false; } if(!in_array($img['type'],$extperm)){ echo 'only jpg, png or gif images accepted.'; $fine = false; } if($_files['gb']['tmp_name'][0]){ print_r($_files['gb']); $count= count($_files['gb']['tmp_name']); for($i=0;$i<$count;$i++){ if(!in_array($gb['type'][$i],$extperm)){ echo 'only jpg, png or gif images accepted.'; $fine = false; } } } if($fine){//if there no problem echo 'test'; // test should appear //here have insert not entering here } notice there no else statements, every possible case tested. means may have multiple errors echoed.
you commit errors $errors array, test if(count($errors) == 0) example
php
No comments:
Post a Comment