hi guys, got this code from this previous thread: http://www.daniweb.com/web-deveelopment/javascript-dhtml-ajax/threads/416185/adding-rows-to-table-per-click-manipulating-name-field-thereby

the code works fine but how do i modify the code so I will be able to get all the drop down data selected by the user?

let's say there were 5 drop downs that were created and selected how to get all those data?

here's the code from that link above:

<html>
  <head>
    <title>Adding rows</title>


<SCRIPT language="javascript">
  function addRowEntry(tableID){
      var table = document.getElementById(tableID);
      var rowCount = table.rows.length;
      // create a row element
      var row = document.createElement("tr");
      // add the row to the table
      table.appendChild(row);
      var colCount = table.rows[0].cells.length;
      for(var i=0; i<colCount; i++) {
          var newcell = row.insertCell(i);
          newcell.innerHTML = table.rows[0].cells[i].innerHTML;
      }
     // get the select element
     var dropdown = row.getElementsByTagName("select")[0];
     // get the current total of dropdowns in the table
     var total = table.getElementsByTagName("select").length;
     // set the name
     dropdown.setAttribute("name", "country" + total);
  }
</SCRIPT>      



  </head>
  <body>
    <form action="passToEvaluationScript.php" method="POST">
      <input type='button' value='add row entry' onClick="addRowEntry('myTable');">
        <table id="myTable">';
      <tr>
        <td>
              <SELECT name="country1">
                <OPTION value="in">India</OPTION>
                <OPTION value="de">Germany</OPTION>
                <OPTION value="fr">France</OPTION>
                <OPTION value="us">United States</OPTION>
                <OPTION value="ch">Switzerland</OPTION>
              </SELECT>
            </td>
          </tr>
        </table>
      <input type="submit" value="submit">
    </form>
  </body>
</html>

One technique is to name form fields in a way that will be translated into an array by PHP. Instead of naming the SELECT element "country1", "country2", etc you can name the fields "country[1]", "country[2]" and so on. When the form gets submitted to PHP, it will automatically convert these into an array that resides at $_POST['country'].

Also, change the script (line 24) to reflect the new naming convention:

dropdown.setAttribute("name", "country[" + total +"]");
commented: Thanks buddy :) +8

Brilliant madCoder, thanks for the great idea..

so to retrieve the data I will have to do the following please correct me if I'm wrong.

//just an example if let's say I want to echo the data

$count=sizeof($country);
for($counter=0;$counter<$count;$counter++){

echo ($country[$counter]);

}

or is there some efficient way to do this?

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.