Hello, i have a script:
function popup() {
var urls = document.p.elements["url[]"];
var times = document.p.elements["time[]"];
var types = document.p.elements["type[]"];
var output = [];
var counters = document.p.elements["counter[]"];
resObjekt.open('POST', '/test.php' ,true);
resObjekt.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
resObjekt.onreadystatechange = handleResponse;
alert(output.length);
for(var i=0;i<=urls.length;i++)
{
var output = output + urls[i].value + ':' + times[i].value + ':' + types[i].value + ':' + counters[i].value + ';';
}
str = 'command=' + output;
resObjekt.send(str);
alert(output);
}
And a form
URL: <input name="url[]" type="text"> Time: <select name="time[]"><option value="1">Morning</option><option value="2">Midday</option><option value="3">Evening</option><option value="4">Night</option></select> Agression: <select name="type[]"><option value="0">Silent</option><option value="1">Loud</option></select> <input value="-" onclick="removeElement(1);" type="button"> <input name="counter[]" value="1" type="hidden"><br></div><div id="2">URL: <input name="url[]" type="text"> Time: <select name="time[]"><option value="1">Morning</option><option value="2">Midday</option><option value="3">Evening</option><option value="4">Night</option></select> Agression: <select name="type[]"><option value="0">Silent</option><option value="1">Loud</option></select> <input value="-" onclick="removeElement(2);" type="button"> <input name="counter[]" value="2" type="hidden"><br></div><div id="3">URL: <input name="url[]" type="text"> Time: <select name="time[]"><option value="1">Morning</option><option value="2">Midday</option><option value="3">Evening</option><option value="4">Night</option></select> Agression: <select name="type[]"><option value="0">Silent</option><option value="1">Loud</option></select> <input value="-" onclick="removeElement(3);" type="button"> <input name="counter[]" value="3" type="hidden"><br></div><div id="4">URL: <input name="url[]" type="text"> Time: <select name="time[]"><option value="1">Morning</option><option value="2">Midday</option><option value="3">Evening</option><option value="4">Night</option></select> Agression: <select name="type[]"><option value="0">Silent</option><option value="1">Loud</option></select> <input value="-" onclick="removeElement(4);" type="button"> <input name="counter[]" value="4" type="hidden"><br></div><div id="5">URL: <input name="url[]" type="text"> Time: <select name="time[]"><option value="1">Morning</option><option value="2">Midday</option><option value="3">Evening</option><option value="4">Night</option></select> Agression: <select name="type[]"><option value="0">Silent</option><option value="1">Loud</option></select> <input value="-" onclick="removeElement(5);" type="button"> <input name="counter[]" value="5" type="hidden">
Now the js will extract all the form values and adds them into an array and the loop is gonna to extract em. I want to join all the output stuff into one string so i can pass it to the php script.
the problem with my script is, that if the html is only 1 field, the output string will be empty and nothing will be sent.
does anybody have a fix for this, so the script will also work, if there’s only 1 input given?