简体   繁体   中英

Uncaught type error for input type file

I have a form and I am trying to upload the file but i have to get the file to upload it but I am unable to do so as I am getting this error

main.js:5 Uncaught TypeError: Cannot read property 'value' of null

function upload() {
    var data = new XMLHttpRequest();
    var url = "includes/upload.php";
    var file = document.getElementById("filename").value;
    var image = document.getElementById("image").value;
    var span = document.querySelector('#uploadPreview[style]'),
        a = window.getComputedStyle(span, null).transform,
        b = window.getComputedStyle(span, null).transformOrigin;
    val = 'transform:' + a + ';transform-origin:' + b + ';backface-visibility: hidden;';

    var vars = "style=" + val + "&image=" + image + "&filename=" + file;

    data.open("POST", url, true);
    data.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    data.onreadystatechange = function() {
        if (data.readyState == 4 && data.status == 200) {
            var return_data = data.responseText;
            document.getElementById("comment_box").innerHTML = return_data;
        }
    }

    data.send(vars);
}

My HTML code:

<form method="POST" enctype="multipart/form-data" style="text-align:center;">
    <input id="uploadImage" type="file" id="image" name="image" style="margin:auto;" />
    <input type="hidden" id="x" name="x" />
    <input type="hidden" id="y" name="y" />
    <input type="hidden" id="w" name="w" />
    <input type="hidden" id="h" name="h" />
    <input type="hidden" id="filename" name="filename" value="<?php if(!empty($data['profile_pic'])) { echo $data['profile_pic'];} ?>" />
    <button type="button" name="update_picture" class="btn_form" onclick="upload();">Update Picture</button>
</form> 

You have 2 id's here:

 <input id="uploadImage" type="file" id="image" name="image" style="margin:auto;" />

Try using just to one id.

You cant have both, you can reference this question here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM