The problem is that you set your wrapper div in $form['price_form']['textfields']
, but in the AJAX callback you are replacing it with the $form['textfields']
that does not have it defined. To be safe, keep these guidelines:
Never use the same identifier at 2 different depths of an form array, as it leads to mistakes. Be sure to set
'#tree' => TRUE
if you need to use identifier twice, to avoid conflicts.In AJAX callback always return the same element you set
'#prefix' => '<div id="...">
on.Be sure to only define each element once. If you are using
$form['price_form']['textfields'] = array( '#prefix' => '<div id="listDiv">', '#suffix' => '</div>', '#markup' => '', );
to define element, don't reset it with
$form['price_form']['textfields'] = array(
later in code. Usingarray()
creates new empty array effectively deleting everything set earlier. Write directly to it's members like this:$form['price_form']['textfields']['#markup'] = t('Already exist');
Of course they are not set in stone, but don't break them unless you know why and what you're doing. In your case, you have 'textfields'
twice, you are setting wrapper on one and returning another. That's why it only works once. Second time there is no element FAPI's AJAX is supposed to replace.