[eluser]iamzvonko[/eluser]
I have an input form that’s used to specify information about a “volunteer”. The volunteer can be a single person or a group of people. At the bottom of the form there is a checkbox that’s selected when the information is about a group of people. When it’s selected, I use jquery to show a DIV that has a table (initially just 1 row) where information about the other members of the group can be entered.
The user can click an image to trigger a jquery script function that will dynamically add a row to the table.
My problem is that I can’t figure out how to repopulate the values in the row(s) if the form has to be redisplayed because of a validation error. Since I’m generating the HTML with script, I can’t put in PHP code because it gets interpreted at the time the page is loaded.
Maybe there is a better way to do this whole thing (and I’m open to suggestions) but here’s what I have now.
Thanks in advance for any help.
Here’s the jQuery script to add new row:
Code:
function addMember() {
var $counter = parseInt($("#rowCounter").val());
if ($counter > 1) {
var $firstName = "#firstName"+$counter;
var $lastName = "#lastName"+$counter;
var $email = "#email"+$counter;
if ($($firstName).val() == "" && $($lastName).val() == "" && $($email).val() == "") {
alert("You must fill out member row before adding another!");
return false;
}
}
$memberRowNum = $counter;
$counter+=1;
var $appendVal = '<tr><td>'+$memberRowNum+'<input type="hidden" name="volID'+$counter+'" id="volID'+$counter+'"></td>';
$appendVal += '<td><input type="text" name="firstName~~" id="firstName~~" size="15" ></td>';
$appendVal += '<td><input type="text" name="lastName~~" id="lastName~~" size="20" ></td>';
$appendVal += '<td><input type="text" name="email~~" id="email~~" size="25" ></td>';
$appendVal += '<td align="center"><input type="checkbox" name="student_~~" id="student_~~" /></td>';
$appendVal += '<td><select name="grade_~~" id="grade_~~"><option value=""></option><option value="13">College</option></option><option value="12">12th</option><option value="11">11th</option><option value="10">10th</option><option value="9">9th</option><option value="8">8th</option><option value="7">7th</option><option value="6">6th</option><option value="5">5th</option><option value="4">4th</option><option value="3">3rd</option></select></td>';
$appendVal += '</tr>';
$appendVal = $appendVal.replace(/~~/g,$counter);
$appendVal = $appendVal.replace(/@@/g,"?");
$(".groupMembers > tbody:last").append($appendVal);
$("#rowCounter").val($counter);
}
Here’s the HTML/PHP snippet after which, the row(s) are appended:
Code:
<li><label>Are you registering as Group?</label><?php echo form_checkbox($asGroup);?>
<span id="handleGroupInfo">
<br>Group Name:<?php echo form_input($groupName);?>
<br><br>List names and emails of all group members below. Click plus sign to add more members to group<br>
<table class="groupMembers">
<tbody>
<th> <img id="add_row" alt="Add Group Member"
src="<?php echo base_url();?>assets/images/green-plus-sign-tiny.png" /> </th>
<th>First Name</th>
<th>Last Name</th>
<th>email</th>
<th>Student?</th>
<th>Grade</th>
</tbody>
</table>
</span>
</li>
</ul>