Click to See Complete Forum and Search --> : Handling Form elements like name="product2[]"


scoob
07-29-2003, 04:20 AM
When you use the same name with a [] for multiple fields (hidden and text), the result is concatenated. Because of the shopping cart I use (mals-e.com), I have to use this technique.

But I'm having difficulting doing some form validation on those product2[] elements (see code below).

I'd like to have a "onsubmit" javascript event handler
tied to the form that could verify that if the user has
clicked on the checkbox (qty2), then he MUST have entered
an account number in product2[] (the 4th on that is!).

I don't do javascript and this is only giving me a headache
in trying to figure out how to reference this particular
DOM form object in the verification function.
Any javascript code example would be greatly appreciated!

.... stuff deleted ....

<input type="hidden" name="product2[]" value="Use my Shipping (10% off)">
<input type="checkbox" name="qty2" value="1">
<input type="hidden" name="price2" value="-4.80">

<select size="1" name="product2[]"
<option value="Fedex">Fedex</option>
<option value="UPS">UPS</option>
<option value="DHL">DHL</option>
<option value="Airborne">Airborne</option>
</select>

<i>account#</i>
<input type="hidden" name="product2[]" value="acct #">
<input type="text" name="product2[]" size="20" >

.... stuff deleted ....

Cheers!
- Christian

xataku_nakusute
07-29-2003, 04:28 AM
perhaps it is becuz you have two fields....(<input> and <select>) assigned to the same name(product2[])

scoob
07-29-2003, 04:34 AM
Originally posted by xataku_nakusute
perhaps it is becuz you have two fields....(<input> and <select>) assigned to the same name(product2[])

Actually, if you look closely, you will see that there are 4 fields with the same name (product2[] ). I know it's strange but it's legit html form stuff. It is interpreted by my shopping cart as concatenating each of those elements onto one product desctiption line.

So, the issue I have is: it's legit html form code but I have no idea how to access the 4th instance of these elements (the one where the user enters an account number.

- Scoob

AdamGundry
07-29-2003, 04:42 AM
You should be able to use formname.elements[#] where # is the number of the form element (in source code order). See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/form.html#1193375 for more details.

Adam

xataku_nakusute
07-29-2003, 04:46 AM
sorry, i mustve overlooked the "hidden" attribute in the tag.....

perhaps, you may assign an id to the field....

maybe:

<script type="text/javascript">
checker = document.f1.i1.disabled;
function tos()
{
document.f1.i1.disabled=false;
}
function dis()
{
document.f1.i1.disabled=true;
}
function check(checker)
{
if(document.f1.i1.disabled==1)
{
tos();
}
else
{
dis();
}
}
</script>

and with your checkbox....

onclick="check()"

uhhh....im not sure if thats what youre asking for...im just a bit confused with what youre trying to ask...

the script i provided above will simply disable your submit button(with the name of i1) in your form(with the name of f1)

scoob
07-29-2003, 05:50 AM
Thank you AdamGundry for the
rapid info that lead to the perfect solution!

Solution:

function validateEntries (form) {
var FedexAccountEleIdx = 9;
with(form) {

// First, if user checked Fedex checkbox he must also enter account #
if (qty2.checked) {
if (form.elements[FedexAccountEleIdx].value.length < 3) {
alert("You must enter a valid Shipping account number!");
return(false);
}
}
}
}