This topic is locked

Parameter passing not working

12/12/2017 6:49:25 AM
PHPRunner General questions
mbintex author

Hi at all,
I am trying to import a vcard from disk to database.
So far I have:

  1. A hidden input field in my HTML

<INPUT id="fileinput" style="display: none;" type="file">


2. A button to invoke a script adding a chosen vcard to the address table of my database. This consists of
Client Before tab

$("input").trigger("click");
var fileInput = document.getElementById('fileinput');
var file = fileInput.files[0];

var textType = /text.*/;
if (file.type.match(textType))

{

var reader = new FileReader();
reader.onload = function(e) {

var vcardinhalt =JSON.stringify(parse(reader.result),0,2);

alert(vcardinhalt);

params["vcard"]=vcardinhalt;

}
reader.readAsText(file);
}

else {

alert("Dateityp wird nicht unterstützt!")

}
function parse(input) {

var Re1 = /^(version|fn|title|org)<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=25210&image=1&table=forumtopics' class='bbc_emoticon' alt=':(' />.+)$/i;

var Re2 = /^([^:;]+);([^:]+)<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=25210&image=2&table=forumtopics' class='bbc_emoticon' alt=':(' />.+)$/;

var ReKey = /item\d{1,2}\./;

var fields = {};
input.split(/\r\n|\r|\n/).forEach(function (line) {

var results, key;
if (Re1.test(line)) {

results = line.match(Re1);

key = results[1].toLowerCase();

fields[key] = results[2];

} else if (Re2.test(line)) {

results = line.match(Re2);

key = results[1].replace(ReKey, '').toLowerCase();
var meta = {};

results[2].split(';')

.map(function (p, i) {

var match = p.match(/([a-z]+)=(.*)/i);

if (match) {

return [match[1], match[2]];

} else {

return ["TYPE" + (i === 0 ? "" : i), p];

}

})

.forEach(function (p) {

meta[p[0]] = p[1];

});
if (!fields[key]) fields[key] = [];
fields[key].push({

meta: meta,

value: results[3].split(';')

})

}

});
return fields;

};


Server tab

$vcard = json_decode($params["vcard"], true);
$firma=$vcard['org'];

$stellung=$vcard['title'];

$nachname=$vcard['fn'];
$sql=("insert into Adressen (Firma,Stellung,Nachname) values ('".$firma."','".$stellung."','".$nachname."')");
CustomQuery($sql);
$result["vcard"]=$vcard;

$result["sql"]=$sql;


Client After tab

location.reload();
alert(result["vcard"]);

alert(result["sql"]);


The alerts are just for testing purposes - they show me, that the parameters don´t get passed from client before to server tab.
Problem is, that the vcard JSON isn´t passed to the server PHP part and therefor I get only empty records.
Furthermore - I don´t know why at all - the list layout looses some of its columns. I can show them again via the list-button though.
Can anyone help?

mbintex author 12/15/2017

If anyone is interested.
That the parameter here is not passed to the server part is no bug in PHPrunner. Reason has to do with asynchronous handling of Javascripts. The parameter is passed, when you have a callback in your script and wait for the result. Something like this:

var fileInput = document.getElementById('fileinput');
var file = fileInput.files[0];

var textType = /text.*/;

params["sql"]="test";
function one(callback){
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];
var sqlquery="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+"')";

callback(sqlquery);

}
reader.readAsText(file);
}

else

{

alert("Dateityp wird nicht unterstützt!")

}

}
function two(response){

console.log(response);

params["sql"]=response;

}
function parse(input) {

var Re1 = /^(version|fn|title|org)<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=83929&image=1&table=forumreplies' class='bbc_emoticon' alt=':(' />.+)$/i;

var Re2 = /^([^:;]+);([^:]+)<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=83929&image=2&table=forumreplies' class='bbc_emoticon' alt=':(' />.+)$/;

var ReKey = /item\d{1,2}\./;

var fields = {};
input.split(/\r\n|\r|\n/).forEach(function (line) {

var results, key;
if (Re1.test(line)) {

results = line.match(Re1);

key = results[1].toLowerCase();

fields[key] = results[2];

} else if (Re2.test(line)) {

results = line.match(Re2);

key = results[1].replace(ReKey, '').toLowerCase();
var meta = {};

results[2].split(';')

.map(function (p, i) {

var match = p.match(/([a-z]+)=(.*)/i);

if (match) {

return [match[1], match[2]];

} else {

return ["TYPE" + (i === 0 ? "" : i), p];

}

})

.forEach(function (p) {

meta[p[0]] = p[1];

});
if (!fields[key]) fields[key] = [];
fields[key].push({

meta: meta,

value: results[3].split(';')

})

}

});
return fields;

};
one(two);
while(params["sql"]==="test")

{
alert("vCard wird jetzt verarbeitet.");

//one(two);

}
return true;


Sadly, vCards are such a mess of different versions, interpretations, extensions and faulty realizations, that it becomes really hard to successfully import vCards. Furthermore you get into trouble with Javascript timeouts, if you have really big vCards (either with pictures or with multiple addresses).