Skip to main content
added 3rd guideline
Source Link
Mołot
  • 21.9k
  • 8
  • 59
  • 112

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:

  1. 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.

  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

  3. 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. Using array() 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.

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:

  1. 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.

  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

  3. 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. 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.

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:

  1. 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.

  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

  3. 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. Using array() 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.

added 3rd guideline
Source Link
Mołot
  • 21.9k
  • 8
  • 59
  • 112

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 rulesguidelines:

  1. 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.

    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.

  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

    In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

  3. 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. 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.

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 rules:

  1. 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.
  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

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.

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:

  1. 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.

  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

  3. 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. 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.

added 90 characters in body
Source Link
Mołot
  • 21.9k
  • 8
  • 59
  • 112

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 rules:

  1. 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.
  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

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.

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 rules:

  1. Never use the same identifier at 2 different depths of an form array, as it leads to mistakes.
  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

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.

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 rules:

  1. 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.
  2. In AJAX callback always return the same element you set '#prefix' => '<div id="..."> on.

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.

Source Link
Mołot
  • 21.9k
  • 8
  • 59
  • 112
Loading