Search code examples
phphtmlif-statementisset

im trying to add an if else statement for when the form is posted and wether or not the user include files or not?


how would I do an if / else statement for such? the user has the option of adding images or not. if the add images, i want the if to run and if they dont I want the else to run because depending on whether or not the add an image they will have two different end.

if (isset($_POST['formsubmitted'])) { 

if (isset($_POST['name'])){  }else{  }

if (isset($_POST['state'])){  }else{  }

if (isset($_FILES['images']['name'])) { echo 'images'; exit;} else {echo 'no images'; exit;}


} #end main form submitted

if (isset($_FILES['images']['name'])) dosnt work because as of now even when no images are submitted it still says images submitted.

the html file fields are:

<form>
<input type='file' name='images[]' id=''>
<input type='file' name='images[]' id=''>
<input type='file' name='images[]' id=''>
<input type = "submit">    
</form>

Solution

  • You should try this

    Submit.php

    <pre>
    <?php 
    // those index are empty the array filter remove this 
    
    echo "without filter"."<br>";
    print_r($_FILES['images']['name']);
    
    echo "filter"."<br>";
    $usersFileUpload = array_filter($_FILES['images']['name']);
    print_r($usersFileUpload);
    
    $usersFileUploadCount = count($usersFileUpload);
    
    for($i=0;$i<=$usersFileUploadCount;$i++){
        echo $usersFileUpload[$i]."<br>";
    
        // insert Table     
    }
    ?>
    

    HtmlForm.php

    <form action="submit.php" method="post" enctype="multipart/form-data" name="images">
    <input type='file' name='images[]' id=''><br />
    <input type='file' name='images[]' id=''><br />
    <input type='file' name='images[]' id=''>
    <input name="" type="submit" />
    </form>
    

    See the image for verification 1

    enter image description here

    See the image for verification 2

    enter image description here