Page 1 of 1

Conditional statement help

Posted: Fri Jul 17, 2009 9:07 am
by Addos
In the form below I only want the conditional statement - $img_upload_error = 'That file is not a .jpg file.' - to run ‘only’ if that field is selected and filled in. As it stands if the form is submitted (with both fields left balnk) both statements are called and I only want the $nomessage_user_f_name = 'First Name Needed!' to be called unless as I say a file is selected for upload. I’m trying to have a facility where the file is an option and don’t want this to get called unless it’s actually used.

Can anyone help me figure out what I’m doping wrong? I can’t think how to ignore this field and was trying to use if ($_FILES['fupload'] != ' ') { etc…
Thanks

Code: Select all

<?php
 
$error_email = '';
$nomessage_user_f_name ='';
$img_upload_error = '';
 
 
if ($_POST && array_key_exists('MM_insert',$_POST)) {
if (isset($_POST['f_name']) && !empty($_POST['f_name'])) {
  $user_f_name=trim($_POST['f_name']);
  } else {
  $nomessage_user_f_name = 'First Name Needed!';
  }
 
// Run conditional on image upload
if ($_FILES['fupload'] != ' ') {
 if ($_FILES['fupload']['type']  != "image/jpeg") {
        $img_upload_error = 'That file is not a .jpg file.';
        } 
     }
 }
 
// Proceed with input to db once these are passed
if(!$nomessage_user_f_name && !$img_upload_error)
 { // do insert etc 
 } ?>
 
 
 <form method="post" name="form1" enctype="multipart/form-data" action="">
 
 <input type="text" name="f_name" value="<?php if (isset($_POST['f_name'])) echo $_POST['f_name'];?>" size="32">
<?php if (isset($nomessage_user_f_name) && !empty($nomessage_user_f_name)) {
         echo $nomessage_user_f_name; } ?>
   
image
 
   <input type="file" name="fupload" value="<?php if (isset($_POST['fupload'])) echo $_POST['fupload'];?>">
   <?php if (isset($img_upload_error)) {
         echo $img_upload_error; } ?> 
    
<input type="submit" value="Insert record">
   <input type="hidden" name="MM_insert" value="form1">
 </form>