I made a simple GPA (grade point average) calculator and I am just playing around with some stuff and would really like to know how to allow the user to add rows as they please. The best example that I could find of someone doing this is at this website: http://gpacalculator.net/high-school-gpa-calculator/
I think I might understand when the user clicks the button, a jquery function could add a row but how do i get it so the name of the input element is different every time someone adds a row. How will the php file know the name of the $_POST[‘someName’].
So question in short: How do I add a row using jquery, and then submit that form to a php file so it is different than the pre-made rows.
Just in case you need to know, I store the values that are sent to the php file in variables. and then enter them into the formula that calculates the GPA.
EDIT HERE IS CODE FOR TABLE:
<table>
<form id = "myform" name = "myform" method = "POST" action = "">
<tr>
<th> Grade in test </th>
<th> Weight of grade </th> <br />
</tr>
<tr> <td><input type = "text" name = "grade1" /> </td> <td> <input type = "text" name = "weight1" /> </td> </tr>
<tr> <td><input type = "text" name = "grade2" /> </td> <td> <input type = "text" name = "weight2" /> </td> </tr>
<tr> <td><input type = "text" name = "grade3" /> </td> <td> <input type = "text" name = "weight3" /> </td> </tr>
<tr> <td><input type = "text" name = "grade4" /> </td> <td> <input type = "text" name = "weight4" /> </td> </tr>
<tr> <td><input type = "text" name = "grade5" /> </td> <td> <input type = "text" name = "weight5" /> </td> </tr>
<tr> <td><input type = "text" name = "grade6" /> </td> <td> <input type = "text" name = "weight6" /> </td> </tr>
<tr> <td><input type = "text" name = "grade7" /> </td> <td> <input type = "text" name = "weight7" /> </td> </tr>
<tr> <td><input type = "text" name = "grade8" /> </td> <td> <input type = "text" name = "weight8" /> </td> </tr>
<tr> <td><input type = "text" name = "grade9" /> </td> <td> <input type = "text" name = "weight9" /> </td> </tr>
<tr> <td><input type = "text" name = "grade10" /> </td> <td> <input type = "text" name = "weight10" /> </td> </tr>
<tr> <td><input type = "text" name = "grade11" /> </td> <td> <input type = "text" name = "weight11" /> </td> </tr>
<tr> <td><input type = "text" name = "grade12" /> </td> <td> <input type = "text" name = "weight12" /> </td> </tr>
<tr>
<td> <div id = "submitButton">
<input type = "submit" value = "SUBMIT" style = " font-family:'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; padding: 10px;border: none; color: white; background-color: #0499ff; width: 100%; font-size: 13px; border-radius: 5px;""/></div></td> <td> <input type = "reset" /> </td></tr>
</form>
</table>
<div id = "results2"></div>
</div>
AND THEN THE CODE IN PHP FILE CUT DOWN TO THE INITIAL VARIABLES:
/* GRADES VARS */
$grade1 = $_POST['grade1'];
$grade2 = $_POST['grade2'];
$grade3 = $_POST['grade3'];
$grade4 = $_POST['grade4'];
$grade5 = $_POST['grade5'];
$grade6 = $_POST['grade6'];
$grade7 = $_POST['grade7'];
$grade8 = $_POST['grade8'];
$grade9 = $_POST['grade9'];
$grade10 = $_POST['grade10'];
$grade11 = $_POST['grade11'];
$grade12 = $_POST['grade12'];
/* GRADE WEIGHT VARS */
$weight1 = $_POST['weight1'];
$weight2 = $_POST['weight2'];
$weight3 = $_POST['weight3'];
$weight4 = $_POST['weight4'];
$weight5 = $_POST['weight5'];
$weight6 = $_POST['weight6'];
$weight7 = $_POST['weight7'];
$weight8 = $_POST['weight8'];
$weight9 = $_POST['weight9'];
$weight10 = $_POST['weight10'];
$weight11 = $_POST['weight11'];
$weight12 = $_POST['weight12'];
THANKS!
So basically you need to use field names as an array so take this example:
When you do a for submit and post the values to the server you will be able to access them with PHP as an array like so:
Now since you won’t know how many rows there are you can loop over it:
When adding the button with javascript you just need to make sure the name is the same and with brackets:
As long as you have that and inputs are inside of form tag that can be submitted this will work.
Edit so based on the code you added to your initial answer I would recommend taking advantage of arrays in PHP.
So lets say you use the inputs like I explained above (by the way this can be used on any HTML element that can be posted to the backend [checkboxes, inputs, textareas, selects, etc…]). You could do this:
This should allow you to clean up your code and you can do the same thing with your weights.