Forum Moderators: open
The relevant JavaScript code is
/* Send the POST request */
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('answer=' + target.elements['answer'].value);
I don't know how to xmlhttp.send more than one piece of POST data. This might work -
xmlhttp.send('correct=' + target.elements['correct'].value);
The array-style notation can be useful when you need to loop through all the elements of a form. In that case, use the index to refer to the element, i.e. form.elements[2] refers to the third element in the form.
The only other situation where array-style notation can be useful is when a field's name contains square brackets. For instance for handling by a server-side script:
<input type="text" name="foo[]"><br>
<input type="text" name="foo[]"> In that case you'll need this lovely syntax to get the value of the second field:
form.elements['john[]'][1].value escape(target.elements['answer'].value) var moodValue = target.elements[isHappy()?"happy":"sad"].value;
2) When the memberName isn't JS-legal:
PHP expect form elements that share a name - thus creating an array of values on the server-side - to have the names suffixed with [].
<form name="f0">
<input type="checkbox" name="thing[]">
<input type="checkbox" name="thing[]">
....
</form>
....
var checks = document.forms.f0.elements["thing[]"];
----------------
In Javascript, these are synonyms:
myObject.aMemberName
myObject["aMemberName"]
Actually, you can use the latter form to get/set a property with any string. However, If it isn't a legal JS "dot syntax" memberName, then you are restricted to always using "square bracket syntax" to access it.
This feature is applied to all objects you'll come across in a scripting environment.