0

I have this code:

var fileInput = document.getElementById('fileinput');

var file = fileInput.files[0];
var textType = /text.*/;
var sqlqueryvcard="select * from Adressen where ID=1";

if (file.type.match(textType)) 
{
    var reader = new FileReader();

    reader.onload = function(e) {
        var vcardinhalt =JSON.stringify(parse(reader.result),0,2);

        var vcard=JSON.parse(vcardinhalt);

        var firma=vcard.org;
        var fullname=vcard.fn;

        var vorname = fullname.split(' ').slice(0, -1).join(' ');
        var name = fullname.split(' ').slice(-1).join(' ');

        var stellung=vcard.title;
        var telefon=vcard.tel[0].value;
        var strasse=vcard.adr[0].value[2];
        var ort=vcard.adr[0].value[3];
        var land=vcard.adr[0].value[4];
        var plz=vcard.adr[0].value[5];
        var staat=vcard.adr[0].value[6];
        var website=vcard.url[0].value[0];
        var mail=vcard.email[0].value[0];

        sqlqueryvcard="insert into Adressen (Kurzname,Firma,Stellung,Vorname, Nachname, Strasse, PLZ, Ort, Bundesland, Staat, Website, Email) values ('"+fullname+"','"+firma+"','"+stellung+"','"+vorname+"','"+name+"','"+strasse+"','"+plz+"','"+ort+"','"+land+"','"+staat+"','"+website+"','"+mail+"')";
        alert(sqlqueryvcard);
    }

    reader.readAsText(file);    
}

else {
    alert("Dateityp wird nicht unterstützt!")
}

//params["sqlqueryvcard"]="select * from Adressen where ID=1";
params["sqlqueryvcard"]=sqlqueryvcard;

and while everything works, I don´t get the hopefully global variable into this

params["sqlqueryvcard"]

which I need to go on with PHP in PHPrunner.

params is always empty.

I tried wiht asynchronous and callbacks to no result.

Any input on how I get my variable filled would be appreciated. Thanks in advance

  • `onload` is an async callback. Also, your code has bad indentation. Please, fix the indentation so is easier for the readers to understand the code. Also, It will be better for you. Good indentation is a must if you want to be a good programmer. – Jorge Fuentes González Dec 13 '17 at 15:16
  • 1
    *"I tried wiht asynchronous and callbacks"* - No you didn't. You're trying to set the value to `params["sqlqueryvcard"]` *immediately*, before the asynchronous operation has executed. Set the value *in the asynchronous callback*. – David Dec 13 '17 at 15:17
  • I did before, but it didn´t work either: window.sqlqueryvcard="insert into Adressen (Kurzname,Firma,Stellung,Vorname, Nachname, Strasse, PLZ, Ort, Bundesland, Staat, Website, Email) values ('"+ful...site+"','"+mail+"')"; callback(); } reader.readAsText(file); } else { alert("Dateityp wird nicht unterstützt!") } //params["sqlqueryvcard"]="select * from Adressen where ID=1"; function callback(){ alert(window.sqlqueryvcard); params["sqlqueryvcard"]=window.sqlqueryvcard; } – Martin Boehmer Dec 13 '17 at 15:21
  • @MartinBoehmer: Code posted in comments is unreadable. If you have indeed made some attempt and it didn't work in some way, that's potentially a case for a Stack Overflow question. The attempt shown in the question above tries to set the value *outside* of the asynchronous operation. Which makes the question a duplicate of the linked question. That linked question and its answered are worth reading and understanding, as this is a *very* common mistake for new developers who don't understand asynchronous code. – David Dec 13 '17 at 15:53
  • I understand the overall problem of this asynchronous stuff, but I don´t really get it functioning into my code :-( You have to know, that I am a newbie coming from FileMaker to PHPrunner, where Javascript is only a little part of all. – Martin Boehmer Dec 13 '17 at 16:18
  • @MartinBoehmer: We can sympathize with being new, we were all there once. The information you're looking for about asynchronous operations is in the linked duplicate. Consider what this line does: `reader.onload = function(e) {` Is basically says, "Here's a function to execute at some later time when you finish what you're doing." After setting that function (*not* executing it), your code continues. That function then executes *at some later time*. So, temporally, you're trying to *read* a value in your code *before* the value was set by that function. – David Dec 13 '17 at 17:09
  • What I don´t really get is, how alert can have the result of my formulas parsing the vcard and passing this value at the same time does not work ... – Martin Boehmer Dec 13 '17 at 22:21

0 Answers0