0

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>

Edited by cguan_77

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by cguan_77
1

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 +"]");
Votes + Comments
Thanks buddy :)
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.