I am trying to POST a form that is generated through a while loop that iterates on some elements of a table and they can vary in number according to the different companies. Because the IDs of the input/edit fields are created by a while-loop they all have the same id and this is creating a problem when I try to submit the form, is there a way to change automatically the IDs? I tried to use Javascript with no success. Is there a way to name the IDs according to the loop cycle?
Also is there a better way to create a number of input/edit fields according to the number of items to be edited in an SQL table?
echo '<form id="myForm" action="edit.php" method="POST">';
echo '<div class="leftcontact">';
$i=0;
while($query_data = mysqli_fetch_row($result)) {
$_SESSION["List"]=$query_data[2];
$_SESSION["List_company_id"]=$query_data[0];
$result2 = mysqli_query($connection, "SELECT * FROM `calling_lists` WHERE `calling_list_id`='".$_SESSION["List"]."'");
$query_data2 = mysqli_fetch_row($result2);
$_SESSION["list_name"]=$query_data2[1];
echo '<div class="form-group">';
echo '<p>List number</p>';
echo '<select class="dropdown" name="ID">';
echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$_SESSION["list_name"]."</option>";
$query1="SELECT * FROM `calling_lists`";
$result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
while ($row1=mysqli_fetch_array($result1)) {
$list_name=$row1["calling_list_name"];
$list_description=$row1["calling_list_description"];
$list_id=$row1["calling_list_id"];
echo "<option value=\"$list_id\">"
. $list_name .
"</option>";
};
$i=$i+1;
};
it was late and I could not figure out the answer, but with a fresh look I found it. In the code there are actually 2 problems, one is that the operator is missing and there was a problem with " ' ".
The working code is here below, in case anybody needs it. It creates a number of dropdown menu, as many as my $results rows.
Hope it helps anybody.
$i=0;
while($query_data = mysqli_fetch_row($result)) {
$_SESSION["List_company_id"]=$query_data[3];
$_SESSION["List"]=$query_data[1];
$_SESSION["list_name"]=$query_data2[2];
echo '<div class="form-group">';
echo "<p>List number $i</p>";
echo '<span class="icon-case hidden-xs"><i class="fa fa-home"></i></span>';
echo "<select class='dropdown' name=\"".$_SESSION['List_company_id']."\">";
echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$query_data[2]."</option>";
$query1="SELECT * FROM `calling_lists`";
$result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
while ($row1=mysqli_fetch_array($result1)) {
$list_name=$row1["calling_list_name"];
$list_description=$row1["calling_list_description"];
$list_id=$row1["calling_list_id"];
echo "<option value=\"$list_id\">
$list_name
</option>";
};
echo "</select>";
echo "</div>";
$i=$i+1;
};
Just use your $i variable to create the id attribute of the controls you emit from your while loop. So they would be named for example “select1”, “select2” and so on.