lundi 30 mars 2015

Best method of targeting values in a dynamic form

I have created a website that displays form inputs based on how much the user enters.For example, if the user enters 3 there are displayed 3 groups of inputs.



<form method="post" action="profile.php?create_fitness&amp;step2#tabs-2">
<input type="number" name="num_exercises" placeholder="Number of Exercises">
<input type="submit" name="exercises" value="Create">
</form>


Then based on this input I have created a function that displays these inputs based on how many the user entered.The maximum number of these is 15



function echo_exercise_fields($num_fields, $day) {
for($i = 0; $i <= $num_fields - 1;$i++) {
$ex_number = $i + 1;
$form_day =
'<input type="text" name="workout_' . $day . "_" . $ex_number . '" id="workout" placeholder="Workout"><br>
<input type="number" name="sets_' . $day . "_" . $ex_number . '" id="sets" placeholder="sets(4)"><br>
<input type="text" name="reps_' . $day . "_" . $ex_number . '" id="reps" placeholder="reps(10 or 8,10,12)"><br>
<input type="text" name="rest_' . $day . "_" . $ex_number . '" id="rest" placeholder="rest(30 - 90)"><br>';
echo "<p>Exercise " . $ex_number . "<p>";
echo $form_day;
}
}


All the above code works just fine, but then I need to insert these values into a database.I created a separate database just for these inputs with 7 tables that represent the days of the week.


I wrote this code:



foreach($_POST as $key => $value) {
$exploded = explode("_",$key);
$element = $exploded[0];
$day = $exploded[1];
$exercise_number = $exploded[2];

$result = $_POST[$element . "_" . $day . "_" . $exercise_number];
}


But then I realized that this won't work.


So my question is how should I proceed in inserting the values that I get from the user into the database.Should I redesign the database?


Any help on this matter would be very much appreciated.


Aucun commentaire:

Enregistrer un commentaire