1

i want to pass on an int (The project id) to make a query in the php file that uploads the file, everything works, but i have to pass on the int which is $project[0][0]; towards the file_upload_parse.php. someone please help me out

this is currently the code (HTML/ PHP):

 <?php
    if ($project[0][5] == true) {
        echo "<div class=\"ui cards\">
        <div class=\"card\" style=\"width: 100vw;\">
            <div class=\"content\">
                <div class=\"header\">
                    <div class=\"meta\">
                        <span class=\"right floated time\">" . $project[0][2] . "</span>
                        <a style=\"font-size: 0.7em\" class=\"category\"> <i class=\"Reply icon\"></i>" . $lang['VIEW_PREVIOUS'] . "</a>
                    </div>
                    <h1><i class=\"File Pdf Outline icon\" style=\"font-size: 2em; padding:14px; margin-right: 20px;\"></i>" . $project[0][1] . " " . $lang['PRESENTATION'] . " </h1>
                </div>
                <br>
            </div>
            <form style='margin-bottom:0px;' id='upload_form' method='POST' enctype='multipart/form-data'>
            <div class=\"extra content\">
                <div class=\"ui two buttons\">
                    <a href='uploads/".$upload[0][1]."' download='' class=\"ui green button\"><i class=\"download icon\"></i>" . $lang['DOWNLOAD'] . "</a>
                    <label for=\"presentation\" class=\"ui icon button\">
                    <i class=\"file icon\"></i>
                    " . $lang['UPLOAD_NEW_PRESENTATION'] . "</label>
                    <input onchange=\"selectFile()\" type='file' name='presentation' style='display:none;' id=\"presentation\" class=\"ui basic red button\"></div>
                </div>
            </div>
            </form>
        </div>
    </div>";
    }
    ?>
</div>

this is currently the code (THE JS IS LOCATED ON THE BOTTOM OF THE PAGE) (JS/PHP):

function selectFile() {
    var file = _("presentation").files[0];
    uploadFile(file);
    $('.ui.modal')
        .modal('show')
    ;
}

function uploadFile(file) {
    var file = _("presentation").files[0];

    var formdata = new FormData();
    formdata.append("presentation", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "templates/upload/file_upload_parse.php");
    ajax.send(formdata);



}

function progressHandler(event) {
    var percent = (event.loaded / event.total) * 100;
    console.log(percent);
    // _("progressBar").value = percent = Math.round(percent);
    // _("status").innerHTML = Math.round(percent)+ " uploaded";
}
function completeHandler(event) {
    $('.ui.modal')
        .modal('hide')
    ;
    console.log("closed");
}
function errorHandler(event) {
    _("status").innerHTML = "failed";
}
function abortHandler(event) {
    _("progressBar").value = 0;
}

this is currently the code (HTML/ PHP):

<?php
include "../../models/login.php";
$con = new User();

if (isset($_FILES['presentation'])){
$filename = $_FILES['presentation']['name'];
$fileTmpLoc = $_FILES['presentation']['tmp_name'];
$fileType = $_FILES['presentation']['type'];
$fileSize = $_FILES['presentation']['size'];
$fileErrorMsg = $_FILES['presentation']['error'];

$file_ext = explode('.',$filename);
$file_ext = strtolower(end($file_ext));

$allowed =  array('txt' , 'pdf','png','jpg');
if (in_array($file_ext, $allowed)){
    if ($fileSize <= 2000000000){
        $file_name_new = uniqid('', true) . '.' . $file_ext;
        if (move_uploaded_file($fileTmpLoc, "../../uploads/$file_name_new")){
         $con->executeQuery("INSERT INTO `uploads`(`project_id`, `location`, `file_kind`) VALUES (".$_GET['projectid'].",'".$file_name_new."','presentation')");
        $con->executeQuery("UPDATE `GGD`.`projects` SET `presentation` = '1' WHERE `projects`.`id` =".$_GET['projectid']);
    }

}
}

}

1 Answers1

0

In your upload file function uploadFile(file), you need to pass a second parameter which will be your project_id and then append it to your formdata object. i.e.

function uploadFile(file, project_id) {
// ...
   console.log('passed project_id', project_id); // make sure you are sending the right ID.
   formdata.append("projectID", project_id);   // key, value
   ajax.send(formdata);
}

In Php(file_upload_parse.php), you can access this via $_POST['projectID']

You can also send projectId via get method in this way

function uploadFile(file, project_id) {
//...
console.log('passed project_id', project_id); // make sure you are sending the right ID.
ajax.open("POST", "templates/upload/file_upload_parse.php?projectID="+project_id);
//...
}

And in Php(file_upload_parse.php), you can get this by $_GET['projectID'].

For your case, you have called uploadFile() inside selectFile() so, you need to pass your projectID to selectFile

<input onchange=\"selectFile(". $project[0][0] .")\" type='file' name='presentation' style='display:none;' id=\"presentation\" class=\"ui basic red button\"></div>

And the place where you have defined selectFile(), you pass this projectId to the uploadFile function

function selectFile(project_id) {
    // ...
    console.log('selectFile project_id', project_id); // make sure you are sending the right ID
    uploadFile(file, project_id);
    // ...
}
Niket Pathak
  • 6,323
  • 1
  • 39
  • 51
  • thanks for the fast response bro! What i've done is add an action to the form which directs me to the php page, i still get the following error : Notice: Undefined index: projectID in /Applications/MAMP/htdocs/GGD/templates/upload/file_upload_parse.php on line 4 – Daan Van Dalen Mar 09 '17 at 10:45
  • are you sure you are sending the project_id? I updated my answer and added a console.log() so you can verify. Also, check the answer to this [SO post](http://stackoverflow.com/questions/23802133/send-xmlhttprequest-with-both-header-and-formdata). it seems like a similar scenario. – Niket Pathak Mar 09 '17 at 10:57
  • i geuss it works but it just doensn't execute the query's ( ill edit the post ) – Daan Van Dalen Mar 09 '17 at 11:30
  • okay, if you can confirm that it works, kindly check the answer as correct. Also, another thing, its always a good idea to cast to int while inserting into database. **(int) $_GET['projectid']** i.e. in your case `$con->executeQuery("UPDATE GGD.projects SET presentation = 1 WHERE projects.id =". (int) $_GET['projectid']);` – Niket Pathak Mar 09 '17 at 11:45
  • i mean the query works, but the pass on of the id doens't yet – Daan Van Dalen Mar 09 '17 at 12:01