Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    Validating selectlist in an array

    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

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    Sorry of posting the request twice. i hit the button twice by mistake

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    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

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    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[]"

    Quote Originally Posted by bhakti_thakkar View Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •