Hi, first off thank you to anyone for your response.
Here's my scenario. I have dynamically generate forms which create a list of items which allow you to click a vote button next to them. The amount of times the vote button has been clicked is displayed next to them and is updated by using an AHAH call. This all works extremely well and I was really surprised how easy it was to implement.
However the real problem arises with the more button on the bottom of the form. I have it set so that by default only 5 items on the form are shown for voting, a more button on the bottom of the form expands the form showing all of the items that are available for voting, this also uses an AHAH call. Now here is the problem. After I click the more button, if I click on the vote button for any of the items reveled it will simply do a page refresh and not run the AHAH call at all. I think this has something to do with the $form_state and page cache, but I really am not sure. I got the code that I'm currently using for most of the AHAH call back from the poll module.
I've pasted some of the code from the module below, I know it's sloppy and I'm sure that I'm doing the AHAH thing wrong since this is my first go at it. But any suggestions or recommendations would be greatly appreciated.
//This handles the generation of all forms for the holiday website
function holidayforms_build_form(&$form_state,$tid){
$form = array();
$form['#id'] = 'holidayforms_build_form'.$tid;
if($form_state['limit'] !== 1){
$item_query = db_query("SELECT {node}.nid,{node}.title FROM {node},{term_node} WHERE {term_node}.nid = {node}.nid && {term_node}.tid = %s ORDER BY {node}.weight ASC LIMIT 5",$tid);
$form_state['limit'] = 0;
}else{
$item_query = db_query("SELECT {node}.nid,{node}.title FROM {node},{term_node} WHERE {term_node}.nid = {node}.nid && {term_node}.tid = %s ORDER BY {node}.weight ASC",$tid);
}
$count = 1;
$form['choice_wrapper'] = array(
'#tree' => FALSE,
'#weight' => -4,
'#prefix' => "<div class='clear-block' id='result-choice-wrapper-$tid'>",
'#suffix' => '</div>',
);
while($form_item = db_fetch_object($item_query)){
$nid = $form_item->nid;
$total_query = db_query("SELECT COUNT(*) FROM {results} WHERE {results}.nid = %d",$nid);
$total = db_result($total_query);
if($weight == $previous_weight && $count !== 1){
$position = $count - 1;
}else{
$position = $count;
}
$form['choice_wrapper']['choice'][$form_item->nid] = array(
'#type' => 'button',
'#prefix' => t("<div class='entry clear-block'><div class='position'>".$position.".</div><div class='title'>".$form_item->title."</div><div class='total' id='form_total_$nid'>$total</div>"),
'#suffix' => t("</div>"),
'#value' => 'VOTE',
'#weight' => $weight,
'#ahah' => array(
'path' => 'holidayforms/js/'.$nid."/".$tid."/".$form_state['limit'],
'wrapper' => 'result-choice-wrapper-'.$tid,
'method' => 'replace',
'effect' => 'fade',
),
);
$previous_nid = $nid;
$previous_weight = $total * -1;
$count++;
}
if($form_state['limit'] !== 1){
$form['choice_wrapper']['choice']['more'] = array(
'#type' => 'submit',
'#value' => 'More',
'#ahah' => array(
'path' => 'holidayforms/more',
'wrapper' => 'result-choice-wrapper-'.$tid,
'method' => 'replace',
'effect' => 'fade',
),
);
}else{
$form['choice_wrapper']['choice']['less'] = array(
'#type' => 'submit',
'#value' => 'Less',
'#ahah' => array(
'path' => 'holidayforms/less',
'wrapper' => 'result-choice-wrapper-'.$tid,
'method' => 'replace',
'effect' => 'fade',
),
);
}
dsm($form);
return $form;
}
function holidayforms_build_submit(&$form,$form_state){
}
function holidayforms_js($nid,$tid,$limit) {
$ip = ip_address();
$delta = count($_POST['choice']);
db_query("INSERT INTO {results} (nid,tid,ip) VALUES (%d,%d,'%s')",$nid,$tid,$ip);
$query = db_query("SELECT COUNT(*) FROM {results} WHERE {results}.nid = %d",$nid);
$total = db_result($query);
$total = $total * -1;
db_query("UPDATE {node} SET {node}.weight = %d WHERE {node}.nid = %d",$total,$nid);
holidayforms_recreate_form($limit);
}
function holidayforms_more(){
holidayforms_recreate_form($limit = 1);
}
function holidayforms_less(){
holidayforms_recreate_form($limit = 0);
}
function holidayforms_recreate_form($limit){
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_state['limit'] = $limit;
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// We will run some of the submit handlers so we need to disable redirecting.
$form['#redirect'] = FALSE;
// We need to process the form, prepare for that by setting a few internals
// variables.
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Render the new output.
$choice_form = $form['choice_wrapper']['choice'];
unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent duplicate wrappers.
$output = theme('status_messages') . drupal_render($choice_form);
return drupal_json(array('status' => TRUE, 'data' => $output));
}