PassiveSmoking
12-06-2006, 10:17 AM
I am developing an application that allows users to submit information via a form. One of the sections of the form is a group of elements that will represent bullet points in the final output. The number of bullet points can vary from submission to submission. Here is the HTML code I wanted to use originally:
<input name="bul_text[]" id="bul_text_0" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text_1" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text_2" type="text" size="80" maxlength="255" value="" />
and so on. The square braces in the name attribute cause PHP (the server side language being used) to treat the various bul_text fields as a single array called bul_text, which makes processing it easier. the code passes validation as XHTML 1.1
One of the features of the system is form autocomplete using AJAX. If the user enters a product code and clicks a "lookup" button then the fields will be autopopulated from a server side script that looks up the product code and returns an XML document. Populating most of the fields works okay but the bullet points create a problem. I haven't found a way to get JavaScript to treat the fields as an array the same way PHP does except to do this:
<input name="bul_text[]" id="bul_text" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text" type="text" size="80" maxlength="255" value="" />
When I do this, the following JavaScript fragment then works as I want:
// Update bullet point fields
for (thisBullet = 0; thisBullet < XMLRoot.getElementsByTagName('bullet').length; thisBullet++)
{
if (XMLRoot.getElementsByTagName ('bullet')[thisBullet].firstChild)
{
document.forms ['nominate'].bul_text[thisBullet].value = (XMLRoot.getElementsByTagName('bullet')[thisBullet].firstChild.data);
}
}
but now the HTML fails validation and other useability issues are introduced (I can't use for= in label tags to associate a label with the field). The only solution I can see would involve rewriting the javascript to not use a loop but to explicitly fill each field in turn. This is not a good solution for obvious reasons though. If the number of bullets returned in the XML changes at some future date then the JavaScript will need rewriting as well as the HTML forms.
Does anyone know of a better way of doing this?
<input name="bul_text[]" id="bul_text_0" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text_1" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text_2" type="text" size="80" maxlength="255" value="" />
and so on. The square braces in the name attribute cause PHP (the server side language being used) to treat the various bul_text fields as a single array called bul_text, which makes processing it easier. the code passes validation as XHTML 1.1
One of the features of the system is form autocomplete using AJAX. If the user enters a product code and clicks a "lookup" button then the fields will be autopopulated from a server side script that looks up the product code and returns an XML document. Populating most of the fields works okay but the bullet points create a problem. I haven't found a way to get JavaScript to treat the fields as an array the same way PHP does except to do this:
<input name="bul_text[]" id="bul_text" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text" type="text" size="80" maxlength="255" value="" />
<input name="bul_text[]" id="bul_text" type="text" size="80" maxlength="255" value="" />
When I do this, the following JavaScript fragment then works as I want:
// Update bullet point fields
for (thisBullet = 0; thisBullet < XMLRoot.getElementsByTagName('bullet').length; thisBullet++)
{
if (XMLRoot.getElementsByTagName ('bullet')[thisBullet].firstChild)
{
document.forms ['nominate'].bul_text[thisBullet].value = (XMLRoot.getElementsByTagName('bullet')[thisBullet].firstChild.data);
}
}
but now the HTML fails validation and other useability issues are introduced (I can't use for= in label tags to associate a label with the field). The only solution I can see would involve rewriting the javascript to not use a loop but to explicitly fill each field in turn. This is not a good solution for obvious reasons though. If the number of bullets returned in the XML changes at some future date then the JavaScript will need rewriting as well as the HTML forms.
Does anyone know of a better way of doing this?