...

View Full Version : Validating selectlist in an array



bhakti_thakkar
12-08-2006, 09:40 AM
hi,
i have a php page wherein i am loading few item from DB in checkbox.if the user is selecting any checkbox he has to enter related data. i.e any inspection is selected then he has to enter basis on which he would be charged [Contractual or PER SERVICE], amount and currency [INR / USD /EURO].i am stuck in the javascript validation .i am unable to complete it as when i try to alert the bold line it gives javascript error of :"document.mainform.elements[....]value" is null or not an object. IT doesnt find the basis[] object.
it goes as below:

///////////////////////////////////////////////
<script language='javascript'>
function ValidCheckbox(){
var chk=document.getElementsByName("inspectionid[]");

var frm=document.mainform;
if (chk.length>0)
{
var flag=0;
var selectflag=0;
var amtflag=0;
var currflag=0;
var j=0;

for (var k = 0; k < chk.length ; k++) {

if (chk[k].checked==true) {
j++;
flag++;

/* VALIDATION OF RELATED ITEM GOES HERE
var submenu = document.mainform.elements['basis['+0+']'].value;
alert(submenu);
*/
}
} // end of main for
} // end of it


}
</script>


<body>
<form name="mainform">
<table>
<tr>
<td><B>Inspections</B></td>
<td><B>Basis</B></td>
<td><B>Rate</B></td>
<td><B>Currency</B></td>
<?
while ($rs=mysql_fetch_assoc($rid)) {
?><tr>
<td class='newtd' width='50%'><INPUT TYPE="checkbox" NAME="inspectionid[]" value="<?=$rs['inspectionid']?>" <? if ($db->isinarray($inspectionid,$rs['inspectionid'])) { print 'Checked'; } ?>> <?=$rs['inspectionname']?>
<td width="20%" class='newtd'>
<select name='basis[]' class='sel1'>
<option value="">Select Basis </option>
<option value="Contractual">Contractual</option>
<option value="Per Unit">Per Unit</option>
</select>
<td class='newtd' width="20%"><INPUT TYPE="text" class='smtxtbox' NAME="amt[]"></td>
<td class='newtd' width="10%"> <?
$result = mysql_query("SELECT currencyid, shortname FROM currency ORDER BY currencyname ");
$out = "<select size='1' name='currencyid' class='sel1'>";
$out .= "<option value=''>Select </option>";
while ($row = mysql_fetch_assoc($result)) {
if ($currencyid==$row['currencyid']) {
$selected='selected';
} else {
$selected='';
}
$out .= '<option value="' . $row['currencyid'] . '" '.$selected.'>' . $row['shortname'] . '</option>';
}
$out .= '</select>';
mysql_free_result($result);?>
<?=$out?></td>
</tr>
<? $i++; } ?>


<? } ?>


/////////////////////////////////////////////////////////////////

Please help me
Million thanks in advance
Bhakti

bhakti_thakkar
12-08-2006, 09:42 AM
Sorry of posting the request twice. i hit the button twice by mistake

david_kw
12-08-2006, 10:10 AM
I'm just wondering why you have

'basis['+0+']'

when your name is

'basis[]'

Is that supposed to do something different or is that your error?

david_kw

bhakti_thakkar
12-08-2006, 11:27 AM
hi david_kw,
i just want to know if the first item of inspectionid[] is checked and whether related data is filled or not. Thanks i got the solution by looking at your doubt.Instead of this:
var submenu = document.mainform.elements['basis['+0+']'].value;
and that was wrong.
i can do this

if (chk[k].checked==true) {

if(select1[k].value!="" && ratei[k].value!="" && currencyid[k].value!="" ){
flag++;
} else {
flag=0;
}

///////////////////////////////////
lastly check whether the flag is > 0


thanks your doubt made me clear where i was failing

bhakti_thakkar
12-08-2006, 11:29 AM
of course i will have to put this code right at the begining
var select1=document.getElementsByName("basis[]");
var ratei=document.getElementsByName("amt[]");
var currencyid=document.getElementsByName("currencyid[]"


hi david_kw,
i just want to know if the first item of inspectionid[] is checked and whether related data is filled or not. Thanks i got the solution by looking at your doubt.Instead of this:
var submenu = document.mainform.elements['basis['+0+']'].value;
and that was wrong.
i can do this

if (chk[k].checked==true) {

if(select1[k].value!="" && ratei[k].value!="" && currencyid[k].value!="" ){
flag++;
} else {
flag=0;
}

///////////////////////////////////
lastly check whether the flag is > 0


thanks your doubt made me clear where i was failing



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum