Do i need a folder to post files?

Question

I am trying to send a file to a site using an api call.

You fill out a form with name, email and attach a file then you press "apply". When you press the "apply" button, the name, email and file should be sent to a site that i use to store information(jira). I am able to send name and email directly from my site to the other site using ajax and php, but the file doesn’t get sent.
So do i need a folder to temporarely store the file so i can send it? Or should i be able to send the file directly like i do with name and email? Also when i var_dump the file, i get NULL, but not name and email.

Btw, i have posted this in stackoverflow but got no help and got my question closed, so i try my luck here!

AJAX code:

function sendD(leadId){
    let userName = document.querySelector(`.name-${leadId}`).value;
    let userEmail = document.querySelector(`.email-${leadId}`).value;
    let userMsg = document.querySelector(`.msg-${leadId}`).value;
    let files = document.querySelector(`.fileToUpload-${leadId}`).files;

    if(files.length > 0 ){
        var formData = new FormData();
        formData.append("file", files[0]);
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "fatlum-test", true);
        xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        xhr.send(`name=${userName}&email=${userEmail}&msg=${userMsg}&lead=${leadId}&cv=${formData}`);
    }else{
        alert("Please select a file");
    }
}

The form in testmail.php:

<form action="testmail.php" method="post" enctype="multipart/form-data">
    <label>Full Name</label>
    <input type="text" name="name" class="name-<?php echo $allIssues->key;?>"/>
    <label>Email</label>
    <input type="email" name="email" class="email-<?php echo $allIssues->key;?>"/>
    Upload your CV:
    <input type="file" name="fileToUpload" 
    class="fileToUpload-<?php echo $allIssues->key;?>"/>
    <label>Cover Letter</label>
    <textarea class="msg-<?php echo $allIssues->key;?>"></textarea>
    
</form>

The button:

<button class="app-btn" type="submit" value="Submit" onclick="sendD('<?php echo $allIssues->key;? >');testFunc();"> Apply</button>

The php code in testmail.php:

if(isset($_POST['name'])){
    $name = $_POST['name'];
    $file = $_FILES['fileToUpload'];
   
    var_dump($file,$name);
0
Thinsanta 2 years 2020-12-16T13:10:23-05:00 0 Answers 5 views 0

Leave an answer

Browse
Browse