I’m creating a database that stores student scores, so I wrote a js function that allows the user to add as many scores as they need. However, How do I access the different entries in php to store them in my database? Different elements in every entry have the same id and name, so how do I access the $_POST variable in PHP to put in my database? ChatGPT couldn’t solve this and I couldn’t find information on this anywhere, here is my code:
<!DOCTYPE html>
<html>
<head>
<button onclick = "location.href='index.html'">
Go Back</button>
<title>TOEFL Scores</title>
<script>
var times=0;
function addEntry() {
var container = document.getElementById("entry-container");
var entry = document.createElement("div");
entry.innerHTML = `
<label for="r">Reading Score:</label>
<input type="number" id="r" name="r"><br>
<label for="l">Listening Score:</label>
<input type="number" id="l" name="l"><br>
<label for="s">Speaking Score:</label>
<input type="number" id="s" name="s"><br>
<label for="w">Writing Score:</label>
<input type="number" id="w" name="w"><br>
<label for="year">Year:</label>
<select id="year" name="year">
<option value=""></option>
<?php
$current_year = date('Y');
for ($i = $current_year; $i >= $current_year - 5; $i--) {
echo "<option value='$i'>$i</option>";
}
?>
</select>
<label for="month">Month:</label>
<select id="month" name="month">
<option value=""></option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<label for="day">Day:</label>
<select id="day" name="day ">
<option value=""></option>
<?php
for ($i = 1; $i <= 31; $i++) {
echo "<option value='$i'>$i</option>";
}
?>
</select>
<br><br>
<button onclick="deleteEntry(this)">Delete</button>
<br><br>
`;
container.appendChild(entry);
times++;
}
function deleteEntry(button) {
var div = button.parentNode;
div.parentNode.removeChild(div);
}
</script>
</head>
<body>
<h3>TOEFL Scores</h3>
<form method="post" action="process_score.php">
<div id="entry-container">
<!-- Initial entry goes here -->
</div>
</div>
<button type="button" onclick="addEntry()">Add Score</button>
<br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
This was the answer that ChatGPT provided
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Loop through each submitted entry
for ($i = 0; $i < count($_POST['r']); $i++) {
$reading = $_POST['r'][$i];
$listening = $_POST['l'][$i];
$speaking = $_POST['s'][$i];
$writing = $_POST['w'][$i];
$year = $_POST['year'][$i];
$month = $_POST['month'][$i];
$day = $_POST['day'][$i];
// Echo the values for this entry
echo "Reading Score: " . $reading . "<br>";
echo "Listening Score: " . $listening . "<br>";
echo "Speaking Score: " . $speaking . "<br>";
echo "Writing Score: " . $writing . "<br>";
echo "Test Date: " . $year . "-" . $month . "-" . $day . "<br>";
echo "<br>";
}
}
2
Answers
To support the comment made above, an example that simplifies the HTML by removing ID attributes and assigns array syntax style names to form elements. By completing and submitting this form within PHP the individual scores can be accessed by index very easily.
For example, adding 3 scores and submitting the form will yield results like this:
The PHP that processes the POST results would then produce HTML like this ( though of course you would not generate this HTML before the HTML document as here! )
In the FORM above the
action
is set to the same page just for the example – you would not want to generate HTML output before the HTML document begins like this… The above PHP could log the results to database just as easily as echoing them to screen. The question suggests that this is the end goal so within thatforeach
loop you would call your database insert function (using a Stored Procedure!)As written in the comments, id’s have to be unique. Second, when posting a form only the
name
is relevant, as it becomes the array key for the value.If you want to group your inputs, so the POST array becomes
you have to create unique names, grouped as arrays:
In your javascript add a global
var entry_counter=0
and in the functionaddEntry()
upp it every time you add an entry (it doesn’t matter if the keys have gaps after you delete an entry, it’s only to make distinct groups).The POST result in PHP then looks like above.
I made you a (partial) fiddle, so you can see how I would have done it.