Attached is an example screenshot of the form I'm using PHP and AJAX in Drupal 7.
By default, the form will display 10 fields. The form uses AJAX to enable users to add a potentially infinite number of input fields to the initial 10 in groups of 5. What I need it to do is upon retrieval, repopulate the fields as it was saved, with the same functionality available, should the user wish to edit.

I have on a previous attempt used $form_state in entries_form_add_five to add to the number of input boxes and returned using the ajax call but I couldn't get this to work when loading the data to edit. How can I rebuild the form with a new $node-->entries_form['term'] array which has been increased by 5?
<?php
function entries_form_form_entries_node_form_alter(&$form, &$form_state, $form_id) {
//trimmed
$node = $form['#node'];
$form["section"]["term"]["#tree"] = TRUE;
$items = $node->entries_form['term'];
foreach($items as $key => $item) {
$form["section"]["term"][$key] = array(
'#type' => 'textfield',
'#size' => 10,
'#attributes' => array(
'class' => array('left'),
),
'#value' => $item,
);
}
//trimmed
}
function entries_form_commands_add_callback($form, $form_state) {
return $form['section']['term'];
}
function entries_form_add_five($node, $form, &$form_state){
$node->entries_form['term'] = array_push($node->entries_form['term'],'', '', '', '', '');
$form_state['rebuild'] = TRUE;
}
function entries_form_node_prepare($node) {
if (empty($node->entries_form)) {
// Set default 10 empty values, since this only runs when adding a new node.
$node->entries_form['term'] = array_fill(0, 10, '');
}
}
function entries_form_node_load($nodes, $types) {
if($types[0] == 'entries'){
$result = db_query('SELECT * FROM {entries_form_node_form_alter} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)))->fetchAllAssoc('nid');
foreach ($nodes as &$node) {
$node->entries_form['term'] = json_decode($result[$node->nid]->term);
}
}
}
Any help very much appreciated.
Thanks!