I'm learning to code and i've come across a probably quite easy problem to solve.
I make a request from the database, that returns a number of users. For each user i create a few input fields like name, email etc.
Now what i want to do is use the same php variable (that contains the number of users), to create or repeat a function x times.
This is what i've tried so far.
var j = 0;
while (j <= i) { //i is the variable passed from php, containing the number of useres
j++;
$('#btnEdit'+j).click(function() {
$("#prename"+j).removeAttr('disabled');
$("#surname"+j).removeAttr('disabled');
$("#function"+j).removeAttr('disabled');
$("#prefix"+j).removeAttr('disabled');
$("#phone_number"+j).removeAttr('disabled');
$("#email"+j).removeAttr('disabled');
});
}
The HTML looks something like this:
Input
<label>Prename</label>
<input name="prename" id="prename<?php echo $i; ?>" value="<?php echo $row['db_prename_value']; ?>" maxlength="100" type="text" placeholder="Prename" disabled="disabled" />
Button
<input type="button" id="btnEdit<?php echo $i; ?>"
name="button<?php echo $i; ?>" value="Edit"></input>
To give a short explanation:
If the database returns 2 users, all the above fields are created for 2 users in php. User 1 will have prename1, surname1 etc. while user 2 will have prename2, surname2 and so on.
I won't know how many users get returned by the database so i can't just manually create the above snippet a set amount of times.
To clarify, i've already passed the variable from php to javascript (variable i). The question is how to repeat the javascript function x times, as the snippet i've posted is not working.
As far as my understanding goes, what my code does is iterate through the while loop x amount of times, but stops at this line, since the button is not being clicked.
$('#btnEdit'+j).click(function() {
Maybe i'm just confused about how to do what i'm trying to do.
Let's make an example. If the variable i is 2, i want the loop to create 2 functions, the first function has to be executed when the button with the id btnEdit1 is being clicked, the second function needs to execute when a button with the id btnEdit2 is being clicked etc.
Thanks