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:

    <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
      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);

    <form action="passToEvaluationScript.php" method="POST">
      <input type='button' value='add row entry' onClick="addRowEntry('myTable');">
        <table id="myTable">';
              <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>
      <input type="submit" value="submit">

Recommended Answers

All 2 Replies

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


echo ($country[$counter]);


or is there some efficient way to do this?


Be a part of the DaniWeb community

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