I have a dynamic table in a form, where depending on the user Input rows will be added to the table. Table column have Id, Name, Types of games(Dynamic list fetched from DB Table).
Now when user enters any number in No. of Students field and click on Load Games button, I am expecting that table should expand to that many rows so that user can insert the data in fields.
Its working as well but the problem is third field of the table is containing a list. and I am unable to understand how to populate the list for game_id field in every row of the table.
Code
<?php
$db = new PDO("sqlite:c:/sqlite/games.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query_game = $db->prepare("select distinct games as di from outdoor_games;");
$query_game->execute();
$data = $query_game->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
<title>Access form</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script language="JavaScript"> </script>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<h2 style="text-align:center;">Test Form</h2>
<fieldset style="width: 1000px;">
<legend><b> Source Profile</b></legend>
<TABLE id="STable" width="350px" border="1">
<tr>
<th bgcolor="WHITESMOKE">Student_ide</th>
<th bgcolor="WHITESMOKE">Student_Name</th>
<th bgcolor="WHITESMOKE">Game Id </th>
</tr>
<TR>
<td><input type= "text" name= "Stu_Id[]" value=1 /> </td>
<td><input type= "text" name= "Stu_Name[]" /> </td>
<td><select name= "Game_id[]" />
<option value=""><b>Games</b></option>
<?php foreach ($data as $row){echo '<option value="' . $row['di'] . '">' . $row ['di'] . '</option>';} ?>
</select>
</tr>
</td>
</TR></TABLE></br>
<label>No. of Students <input type= "text" id="s_cnt" name= "d_Count"></label>
<button type="button" class='loadgames' >Load Games</button>
<input type="submit" name ="add_SP" value ="Add Student Info" style="float: right;" /> </br> </br>
</fieldset ></br> </br>
<input type="submit" name ="exit" value ="EXIT" />
</form>
<script>
$(".loadgames").on('click',function(){
var num = $("#s_cnt").val();
$("#STable tr").slice(2).remove();
for (var i = 2; i <= num; i++)
{
var data = "<tr><td><input type='text' id='Stu_Id"+i+"' class='Stu_Id' name='Stu_Id[]' value = "+i+" /></td><td><input type='text' id='Stu_Name"+i+"' name='Stu_Name[]' /></td> <td><select name= 'Game_id[]' /><option value=""><b>Games</b></option>"+<?php foreach ($data as $row){echo '<option value="' . $row['di'] . '">' . $row ['di'] . '</option>';} ?>+"</select></tr>";
$("#STable").append(data);
}
});
</script>
</body>
</html>