I have a function, which createssets of text inputs:
jQuery(function($) {
var scntDiv = $('#textfields');
var i = $('#textfields p').size() + 2;
var a = 0;
$('#addbutton').click (function() {
$('<p><input type="text" id="test_testbutton" name="test_testbutton['+a+'][0]" value="" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton['+a+'][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>').appendTo(scntDiv);
i++;
a++;
return false;
});
$('#removebutton').live('click',function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
as you can see it puts in every input's 'name' field a incremented value. It looks like:
name="test_testbutton[0][0]", name="test_testbutton[1][0]", name="test_testbutton[2][0]", etc.
The function can remove selected input on demand. It is connected with some PHP code (doesn't matter what the code look like), which saves everything and stores as an array. When Array is created the saved inputs are displayed. They have the same numbers in 'name' field as previously cerated field by above function. so it looks like:
<p><input type="text" id="" name="test_testbutton[0][0]" value="" placeholder="" />
<input type="text" id="" name="test_testbutton[0][1]" value="" placeholder="" /></p>
<p><input type="text" id="" name="test_testbutton[1][0]" value="" placeholder="" />
<input type="text" id="" name="test_testbutton[1][1]" value="" placeholder="" /></>
and so on......
But the problem appears when, after saving, I want to add another text inputs. My jQuery function starts counting from 0. I'd like it to find a highest value of every 'name' fields and create another inputs with higher value, for example:
if the 'name' of input with highest value is 'name="test_testbutton[25][0]' after clcking 'Add' it creates 'name="test_testbutton[26][0]', and so on...
And now!.. my question, badam tsss!!: how to achieve it?
EDIT!
The whole PHP code is based on form submitting. I think I didn't give enough details about PHP code relative to my jQuery function, so there it is:
Function directly relative to jQuery I provided above, it stores inputs texts as an array 'test_testbutton', thanks to code presented below everything is displayed back as a inputs sets:
<a href="#" id="addbutton">Add</a>
<div id="textfields">
<?php
if (isset($_POST['test_testbutton']) && is_array($_POST['test_testbutton'])) {
$_POST['test_testbutton'];}
$textfields = get_option('test_testbutton');
if (!empty($textfields)) {
foreach ($textfields as $key => $textfield):?>
<p><input type="text" id="test_testbutton" name="test_testbutton[<?php echo $key;?>][0]" value="<?php echo $textfield[0];?>" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton[<?php echo $key;?>][1]" value="<?php echo $textfield[1];?>" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p><?php
endforeach;}
?>
</div>
And a function which saves everything in database on submit:
function theme_settings_page() {
global $themename,$theme_options;
$i=0;
$message='';
if ( 'save' == $_REQUEST['action'] ) {
foreach ($theme_options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
foreach ($theme_options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ] ); } else { delete_option( $value['id'] ); } }
$message='saved';
}
else if( 'reset' == $_REQUEST['action'] ) {
foreach ($theme_options as $value) {
delete_option( $value['id'] ); }
$message='reset';
}
if ( $message=='saved' && $i==0 ) echo '<div id="message" class="apply"><strong>Saved.</strong></div>' ;
if ( $message=='reset' ) echo '<div id="message" class="reset"><strong>Reset.</strong></div>';
?>
So again, I need a function which searches through all 'test_testbutton['+a+'][0]' values on website after they being saved on submit, and when I click Add button it puts in place of ['+a+'] 'highest value +1'.