0

I'm working on a Vehicle damage form where users can click picture of their vehicle.

enter image description here

How it works?

  1. They choose a description of the photo they want to click.
  2. Then they click on Add button
  3. I append input type file as a child in the div (which is inside the form element).

Issues?

These appended input type files are not passing data to next page.

What I tried?

I added input type file field in my html code and it worked perfectly. I was able to get data on next page via PHP. But when I'm adding the same file element via javascript, it's not passing data. My $_FILES array shows empty.

HTML Code

<div class="meet-mainbody">

<form action="tst.php" method="POST" enctype="multipart/form-data">

<div class="row">
<div class="col-sm-6">
    <h3>Panel Damage</h3>
</div>
<div class="col-sm-3"><input type="radio" name="panel_damage" class="meet-checkbox" value="No" checked  onclick="shwPanelPhoto('panDamag', 'N')"/> NO</div>
<div class="col-sm-3"><input type="radio" name="panel_damage" class="meet-checkbox" value="Yes" onclick="shwPanelPhoto('panDamag', 'Y')"/> YES</div>
</div>
<br>
<div class="row hid_field" id="panDamag">
<div class="col-sm-6">
    <select name="panvalues" class="meet-select" id="panDamType">
        <option value="">Choose a Photo Description</option>
        <?php
            $getPanD = "SELECT S.OptionName, S.Active FROM table1 Q
                    LEFT JOIN table2 L on Q.QuestionID = L.QuestnID
                    LEFT join table3 S on L.OptionID = S.SelectID
                    WHERE Q.QuestionID = '15' and S.SelectType = 'AddedBox' and S.Active = 'true'";
            $getPanDPass = sqlsrv_query($conn, $getPanD, $params, $options);
            if($getPanDPass){
                while($PDrow = sqlsrv_fetch_array($getPanDPass, SQLSRV_FETCH_ASSOC)){
                    echo'<option value="'.$PDrow['OptionName'].'">'.$PDrow['OptionName'].'</option>';
                }
            }
            echo '</select>';
        ?>
    </select>
</div>
<div class="col-sm-6"><button type="button" class="panel-button" onclick="AddChooseFile('shwChoos')">ADD</button></div>
</div>
<br>

<div id="shwChoos"></div>
<br><br>
<!--- end code here---->
<center><input id="sub_btn" type="submit" class="meet-button meet-disabled " name="submit" value="Submit" disabled /></center>
</form>
</div>

Here is the code where I'm adding input type file via javacsript

function AddChooseFile(opt){
    var typ = document.getElementById('panDamType').value;
    var par = document.getElementById('shwChoos');
    var cnt =  par.childElementCount;
    console.log('count is '+cnt);
    //create elements
    var elm1 = document.createElement('div');
    elm1.setAttribute('class', 'row');
    elm1.setAttribute('id', '');

    // elm1.innerHTML = '<div class="col-sm-4"><b>'+typ+'</b></div>'+
                    // '<div class="col-sm-4"><input type="file" name="mtu" accept="image/*" capture required ></div>'+
                    // '<div class="col-sm-4"><i class="fas fa-trash" onclick=""></i></div>';
    var elm2 = document.createElement('input');
    elm2.setAttribute('type', 'file');
    elm2.setAttribute('name', 'mtu');
    elm2.setAttribute('accept', 'image/*');


    elm1.appendChild(elm2);
    par.appendChild(elm1);
    par.appendChild(document.createElement('br'));


}
MeetuU
  • 185
  • 1
  • 12
  • Can you show us your form HTML code? – invisal Jan 21 '20 at 00:46
  • The is probably related, https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean. – user3783243 Jan 21 '20 at 00:48
  • Where and how is this a "php" question? – Funk Forty Niner Jan 21 '20 at 00:52
  • *"My $_FILE array shows empty."* - Did you mean *"My `$_FILES` array shows empty."* ? And about *"These appended input type files are not passing data to next page."* - You should post the HTML and PHP for all this; it's guesswork so far. – Funk Forty Niner Jan 21 '20 at 00:52
  • Can you add the HTML of the form? – Barmar Jan 21 '20 at 00:54
  • @invisal I've added my html code. Please let me know what I'm doing wrong. – MeetuU Jan 21 '20 at 02:59
  • @FunkFortyNiner Thanks for correcting me. I've added my HTML code now. Please have a look. – MeetuU Jan 21 '20 at 03:00
  • @Barmar I've added my HTML code – MeetuU Jan 21 '20 at 03:00
  • Are you adding multiple file choosers? They all have the same name, so only one will be put into `$_FILES`. When you have multiple inputs with the same name, put `[]` at the end of the name, then PHP will create an array of all the values. `elm2.setAttribute('name', 'mtu[]');` – Barmar Jan 21 '20 at 15:19
  • @Barmar Thank You. It's a single file chooser. But I found the issue. My Form tag was in the wrong place. I shifted it to the top of the div and it starting working. So silly of me. – MeetuU Jan 21 '20 at 23:37

1 Answers1

0

Thank you everyone. but I found the answer. The form tag was in the wrong place. I shifted it to the top of the main div and it started working. Apologies

MeetuU
  • 185
  • 1
  • 12