To whom it may concern:
The usual FORM-element array with <input>'s named "xyz[someindexname]" cannot be used when passing them to javascript.
Why? Javascript itself creates automatically an array from <input> fields that have the same name. So you just call them "xyz". Or javascript chokes on the []. The use of associative array indexes (HTML-level) is however ruled out by this behaviour.
Don't know if this is exactly the truth, but it's what I have found out these days.
So in my case how have I dealt with it:
Added a hidden input field which contains $opdracht value. So that array will store the associative index keys.
HTML Code:
<form id="editrapport" name="editrapport">
<fieldset class="editrapport">
<legend>Bewerk rapport:</legend>
<table class="editrapport">
<tbody><tr>
<!-- <th>Volgorde:</th> -->
<th>Type:</th>
<th>Nr:</th>
<th>Titel:</th>
<th>Punt:</th>
</tr><tr><td class="types">
<select id="type" name="type" class="ins_type">
<option value="1">
Kleine overhoring (KO)
</option><option value="4">
Leesoefening (LS)
</option><option value="3">
Synthese proef (SP)
</option><option selected="" value="2">
Taak (TK)
</option>
</select>
</td>
<td class="nummer">
1
</td>
<td class="opdrachten">
<input type="text" value="Schrijven: Wie ben ik?" onkeyup="SearchSuggestions(32,1,this.value);" name="opdracht_titel" id="opdracht_titel" class="ins_opd">
<div style="display: none;" id="autosug"></div>
</td>
<td class="punten">
<input type="text" value="10" name="punt" id="punt" class="ins_punt">
</td>
<input type="hidden" value="1" name="opdracht" id="opdracht">
</tr><tr><td class="types">
<select id="type" name="type" class="ins_type">
<option selected="" value="1">
Kleine overhoring (KO)
</option><option value="4">
Leesoefening (LS)
</option><option value="3">
Synthese proef (SP)
</option><option value="2">
Taak (TK)
</option>
</select>
</td>
<td class="nummer">
1
</td>
<td class="opdrachten">
<input type="text" value="Schooltaalwoorden" onkeyup="SearchSuggestions(32,1,this.value);" name="opdracht_titel" id="opdracht_titel" class="ins_opd">
<div style="display: none;" id="autosug"></div>
</td>
<td class="punten">
<input type="text" value="10" name="punt" id="punt" class="ins_punt">
</td>
<input type="hidden" value="2" name="opdracht" id="opdracht">
</tr><tr><td class="types">
<select id="type" name="type" class="ins_type">
<option value="1">
Kleine overhoring (KO)
</option><option selected="" value="4">
Leesoefening (LS)
</option><option value="3">
Synthese proef (SP)
</option><option value="2">
Taak (TK)
</option>
</select>
</td>
<td class="nummer">
1
</td>
<td class="opdrachten">
<input type="text" value="Nieuwsbegrip" onkeyup="SearchSuggestions(32,1,this.value);" name="opdracht_titel" id="opdracht_titel" class="ins_opd">
<div style="display: none;" id="autosug"></div>
</td>
<td class="punten">
<input type="text" value="15" name="punt" id="punt" class="ins_punt">
</td>
<input type="hidden" value="3" name="opdracht" id="opdracht">
</tr></tbody></table>
</fieldset>
<fieldset class="submit">
<button onclick="SaveChangesRapport(32,1);" name="submit" type="button" class="forms">
<img src="/pics/lco_save.png">
Opslaan
</button>
<button name="reset" type="reset" class="forms">
<img src="/pics/lco_reload.png">
Herstellen
</button>
<button onclick="GetRapport(32,1);" name="cancel" type="button" class="forms">
<img src="/pics/lco_nokay.png">
Annuleren
</button>
</fieldset>
</form>
Notice the reoccuring sequence of <input> fields with the same name property. (background info: the form is dynamic so based on database content this can imply more sequences of the same fields, or less)
Then in javascript I do:
Code:
var form = document.getElementById('editrapport');
var posttitel="";
var posttype="";
var postpunt="";
var postopdracht="";
for(i=0;i < form.elements['opdracht_titel'].length;i++){
posttitel = posttitel+"&titel[]="+form.elements['opdracht_titel'][i].value;
}
for(i=0;i < form.elements['type'].length;i++){
posttype = posttype+"&type[]="+form.elements['type'][i].value;
}
for(i=0;i < form.elements['punt'].length;i++){
postpunt = postpunt+"&punt[]="+form.elements['punt'][i].value;
}
for(i=0;i < form.elements['opdracht'].length;i++){
postopdracht = postopdracht+"&opdracht[]="+form.elements['opdracht'][i].value;
}
postvars="rapport="+rapport+"&klas="+klas+posttitel+posttype+postpunt+postopdracht;
and post it with xmlhttp resulting on php side in:
Code:
array(6) { ["rapport"]=> string(1) "1" ["klas"]=> string(2) "32" ["titel"]=> array(3) { [0]=> string(22) "Schrijven: Wie ben ik?" [1]=> string(17) "Schooltaalwoorden" [2]=> string(12) "Nieuwsbegrip" } ["type"]=> array(3) { [0]=> string(1) "2" [1]=> string(1) "1" [2]=> string(1) "4" } ["punt"]=> array(3) { [0]=> string(2) "10" [1]=> string(2) "10" [2]=> string(2) "15" } ["opdracht"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } }
Which is as expected.
I'm still open for suggestions on how to do it better, but this seems to work.
Bookmarks