Problem with array loop

Please kindly assist with the solution to this function. I have 2 controls (i.e Select dropdown and a textbox). The two controls are arrays loop. I want the value of the input control stored in another textbox at evert loop

function invests() {
  var a=document.getElementById("chk[]").value;
  var b=document.getElementById("invest[]").value;
  var c=document.getElementById("invest-result[]").value;

  document.getElementById('hdinvest[]').value=a;
  document.getElementById('hdinvests[]').value=b;
}

This looks like JavaScript rather than PHP?

Yes it is a Javascript function

I’ve moved this to the JS forum.

Thanks, waiting for the solution to the problem

Can you show us the HTML you are applying this JavaScript to? It’s very difficult to understand what is supposed to happen otherwise.

<div id="invest" class="tab-pane fade">
    
    <fieldset style="border:1px solid;">
   <legend class="scheduler-border" style="color:#00bfff;">Investigation &raquo; <INPUT type="checkbox" name="chk-invest" id="chk-invest"/> <b style="font-size:14px;color:#f00;">Uncovered Investigation</b> </legend> 
   <b style="font-size:14px;padding-left:10px;">Covered Investigation</b>
   <table id="dataTable" class="table table-hover borderless">
     <thead>
        <TR>
        <TD><INPUT type="checkbox" name="chk"/><input type="text" id="hdinvest" name="hdinvest"/><input type="text" id="hdinvests" name="hdinvests"/></TD>
            <TD id="invests">
                <SELECT class="form-control invest" name="invest" id="invest" onchange="invests()">
                    <OPTION value="">Select Investigation</OPTION>
                    <?php 
                    $query2="select laboption,fee from investigation group by laboption order by laboption asc";
                    $resultquery2=mysql_query($query2) or die(mysql_error());
                    while($row2=mysql_fetch_array($resultquery2)){
                    extract($row2);
                    ?>
                    <OPTION value="<?php echo $fee;?>"><?php echo $laboption;?></OPTION>
                    <?php }?>
                    
                </SELECT>
            </TD>
            
            <TD id="results"><INPUT class="form-control" type="text" name="invest-result" id="invest-result" placeholder="Investigation Result"/></TD>
        </TR>

          </thead>
    </table>
    
        <div style="text-align:center;">
        <INPUT type="button" class="btn btn-info" value="Add Row" onclick="addRow('dataTable')" />
    <INPUT class="btn btn-info" type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
</div>
    
    <b style="font-size:14px;padding-left:10px;display:none;" id="und">Uncovered Investigation</b>
    <table id="dataTablex" class="table table-hover borderless" style="margin:0px;padding:0px;display:none;">
     <thead>
        <TR>
        <TD><INPUT type="checkbox" name="chk"/><input type="text" id="hdinvestx" name="hdinvestx"/><input type="text" id="hdinvestsx" name="hdinvestsx"/></TD>
            <TD id="investsx">
                <SELECT class="form-control invest" name="investx" id="investx" onchange="invests()">
                    <OPTION value="">Select Investigation</OPTION>
                    <?php 
                    $query2="select laboption,fee from investigation group by laboption order by laboption asc";
                    $resultquery2=mysql_query($query2) or die(mysql_error());
                    while($row2=mysql_fetch_array($resultquery2)){
                    extract($row2);
                    ?>
                    <OPTION value="<?php echo $fee;?>"><?php echo $laboption;?></OPTION>
                    <?php }?>
                    
                </SELECT>
            </TD>
            
            <TD id="resultsx"><INPUT class="form-control" type="text" name="invest-resultx" id="invest-resultx" placeholder="Investigation Result"/></TD>
        </TR>

          </thead>
    </table>

<div id="uncoveredinvest" style="text-align:center;display:none;">
        <INPUT type="button" class="btn btn-info" value="Add Row" onclick="addRow('dataTablex')" />
    <INPUT class="btn btn-info" type="button" value="Delete Row" onclick="deleteRow('dataTablex')" />
</div>
    
    <table class="table">
    <tr>
  <td style="width:100px;"> Additional</td>
    <td colspan="3">
     <textarea class="form-control" rows="2" id="Additionalinvest" name="Additionalinvest" ></textarea>
    </td>
    <td>&nbsp;</td>
  </tr>
  </table>

<table class="table table-hover borderless" cellpadding="0" cellspacing="0" border="0">
  <tbody>
    <tr style="background:#2A3F54;color:#fff;">
      <td>S/N</td>
      <td>Date</td>
      <td>Time</td>
      <td>Investigations</td>
      <td>Investigation Results</td>
      <td>Clinician</td>
    </tr>
    
    <tr>
     <td>1</td>
     <td>1</td>
     <td>1</td>
     <td>1</td>
     <td>1</td>
     <td>1</td>
    
  <tbody>
  </table>
  
      </fieldset>
      
  </div>

None of those ids exist in the HTML and so none of them will have a value attribute

How to pass selected index array value into a textbox control. ie.

<SELECT class="form-control invest" name="invest[]" id="invest[]" onchange="invests('dataTable')">
                    <OPTION value="">Select Investigation</OPTION>
                    <?php 
                    $query2="select laboption,fee from investigation group by laboption order by laboption asc";
                    $resultquery2=mysql_query($query2) or die(mysql_error());
                    while($row2=mysql_fetch_array($resultquery2)){
                    extract($row2);
                    ?>
                    <OPTION value="<?php echo $fee;?>"><?php echo $laboption;?></OPTION>
                    <?php }?>
                    
                </SELECT>
<input type="text" id="myname[]" name="myname[]"/>
<SELECT class="form-control invest" name="invest[]" id="invest[]" onchange="invests('dataTable')">
                    <OPTION value="">Select Investigation</OPTION>
                    <?php 
                    $query2="select laboption,fee from investigation group by laboption order by laboption asc";
                    $resultquery2=mysql_query($query2) or die(mysql_error());
                    while($row2=mysql_fetch_array($resultquery2)){
                    extract($row2);
                    ?>
                    <OPTION value="<?php echo $fee;?>"><?php echo $laboption;?></OPTION>
                    <?php }?>
                    
                </SELECT>
<input type="text" id="myname[]" name="myname[]"/>

arrays do not exist in HTML.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.