Assigning An Increment Value To Dynamic Fields
I have dynamic input fields generated from a jquery function. There is the ability to add or delete through button clicks these input fields. The fields are populated with data fro
Solution 1:
If I'm not mistaken, you are wanting to know how to increment certain rows, but allow others to be "frozen" (because they are saved to the database). I have changed your code a bit more, here are the important notes:
- I removed the dynamic name attributes. You don't need to dynamically generate field names, you can just assign data-* attributes to hold the id or refer to
tr.find('input[name="person_id"]')
- Added a
data-saved
attribute to thetr
to know whether or not it should be included in the updatedauto increment id
or if it should just stay as it is - Added a save button next to each row, which as it is just sets the
data-saved
attribute on the row, you can add an AJAX call to save the record if you want
The Javascript:
$(document).ready(function () {
var $increment_num = $('#increment_num');
var interval = 5000; //3000 = 3 seconds
function doAjax() {
$.ajax({
type: 'POST',
url: 'personAutoIncrement.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
var $cloned = $('#input_table tr').not('[data-saved]');
var num = parseInt(data);
$increment_num.val(num);
$cloned.each(function(i){
var $this = $(this);
$this.find('[name^="person_id"]').first().val(num+i);
})
},
complete: function (data) {
// Schedule the next
setTimeout(doAjax, interval);
}
});
}
setTimeout(doAjax, interval);
var click_count = 0;
$('#btnAdd').click(function () {
click_count++;
var $clones = $('#input_table tr'),
num = $clones.size() + 1,
next_num = parseInt($clones.last().find('input[name^="person_id"]').val()) + 1,
$template = $clones.first(),
newSection = $template.clone().attr('id', 'pq_entry_'+num),
person_id = 'person_id_'+num;
person_fname = 'person_fname_'+num;
person_lname = 'person_lname_'+num;
newSection.removeAttr('data-saved');
// clear out all sections of new input
newSection.find('input[type="text"]').val('');
newSection.find('input[name^="person_id"]').attr({
'id': person_id
}).val(next_num);
newSection.find('input[name^="person_fname"]').attr({
'id': person_fname
});
newSection.find('input[type="button"]').attr('data-ident', next_num);
$('#input_table').append(newSection);
$('#btnDel').prop('disabled', '');
if (num == 100) $('#btnAdd').prop('disabled', 'disabled');
});
$('.save-button').click(function(){
var $parent = $(this).parents('.clonedSection')
var id = $parent.find('input[name="person_id"]').val();
// do whatever else here, save to db
$parent.attr('data-saved', '1');
})
$('#btnDel').click(function () {
var num = $('.clonedSection').length; // how many duplicate input fields we currently have
$('#pq_entry_' + num).remove(); // remove the last element
// enable the "add" button
$('#btnAdd').prop('disabled', '');
// if only one element remains, disable the "remove" button
if (num - 1 == 1) $('#btnDel').prop('disabled', 'disabled');
});
$('#btnDel').prop('disabled', 'disabled');
});
The HTML:
<form>
<strong>Start Increment from Next ID to be inserted in the DB:</strong><input id="increment_num" name="increment_num" type="text" /></br>
<table>
<thead>
<tr><th>ID from DB</th><th></th>
<th>First Name</th></tr>
</thead>
<tbody id="input_table" >
<tr id="pq_entry_1" class="clonedSection" data-saved="1">
<td><input type="text" name="person_id" value='1' readonly /></td>
<td><input id="person_fname_1" name="person_fname" placeholder=" First Name" type="text" value='James'/></td>
<td><input type="button" class="save-button" value="Save" />
</tr>
<tr id="pq_entry_2" class="clonedSection" >
<td><input type="text" name="person_id" value='2' readonly /></td>
<td><input id="person_fname_2" name="person_fname" placeholder=" First Name" type="text" value='Cynthia'/></td>
<td><input type="button" class="save-button" value="Save" />
</tr>
</tbody>
</table>
<input type='button' id='btnAdd' value='add another Person' />
<input type='button' id='btnDel' value='Delete' /></br>
</form>
Having said all of that, I probably would approach this differently by having a hidden element:
<input type="hidden" name="person_id" value="1" />
That when a new row was generated was nullified:
<input type="hidden" name="person_id" value="" />
Then in my PHP, I would allow MySQL to handle auto incrementing the id in a manner like this:
<?php
$params = $_POST;
if(is_numeric($params['person_id'])){
$sql = sprintf('UPDATE person SET fname = "%s", lname = "%s" WHERE person_id = %u LIMIT 1',
mysql_real_escape_string($params['fname']),
mysql_real_escape_string($params['lname']),
intval($params['person_id'])
);
} else {
// the NULL tells MySQL to select the correct next increment id automatically
$sql = sprintf('INSERT INTO person (id, fname, lname) VALUES (NULL, "%s", "%s")',
mysql_real_escape_string($params['fname']),
mysql_real_escape_string($params['lname']);
);
}
?>
Post a Comment for "Assigning An Increment Value To Dynamic Fields"