Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6585121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:37:20+00:00 2026-05-25T16:37:20+00:00

I’m updating a module I’ve written from D6 to D7, and therefore have to

  • 0

I’m updating a module I’ve written from D6 to D7, and therefore have to exchange the old AHAH by the new #ajax in forms.

I’m performing an ajax request which creates a result and replaces a form element with it. This basically works fine, but after the first request, the result is cached and does not take changes in the form into account. I think this is probably a browser-issue, but could it be that Drupal sends an expiration header which induces the browser to take the cached version? Ot any other ideas?

The fragment in hook_cache():

  $form['fieldset']['mybutton'] = array(
    '#type' => 'button',   
    '#value' => t('Send request'),
    '#ajax' => array(
          'callback' => 'mycallback',
          'wrapper' => 'mywrapper',
          'method' => 'replace',
          'effect' => 'fade',  
    )

A snippet of the callback:

function mycallback($form, $form_state) {

        [..]

        $form['fieldset']['mywrapper']['#markup'] = 'test';
        return $form['fieldset']['mywrapper']['#markup'];
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:37:20+00:00Added an answer on May 25, 2026 at 4:37 pm

    I’ve run into this problem a few times and it isn’t a caching issue. The problem is you’ll originally have a <div> wrapped around your mywrapper element, but in your ajax callback you’re replacing it with a string…the <div> wrapper is therefore replace and next time you press the button the script can’t find the <div> it needs to replace as it’s not there any more!

    Also, the arguments for your mycallback function need to be passed in by reference so change the signature to this: function mycallback(&$form, &$form_state) {.

    Try making your code look a bit more like this:

    function mymodule_my_form($form, &$form_state) {
      $form['fieldset'] = array(
        '#type' => 'fieldset'
      );
    
      $form['fieldset']['my_element'] = array(
        '#markup' => 'Some initial markup',
        '#prefix' => '<div id="mywrapper">',
        '#suffix' => '</div>'
      );
    
      $form['fieldset']['mybutton'] = array(
        '#type' => 'button',   
        '#value' => t('Send request'),
        '#ajax' => array(
          'callback' => 'mymodule_mycallback',
          'wrapper' => 'mywrapper',
          'method' => 'replace',
          'effect' => 'fade',  
        )
      );
    
      return $form;
    }
    
    
    function mymodule_mycallback(&$form, &$form_state) {
      $form['fieldset']['my_element']['#markup'] = 'New Markup';
    
      // Always, always, always return an element here, not a string.
      // This makes sure the form state stays consistent.
      return $form['fieldset']['my_element'];
    }
    

    If in doubt have a look at the examples module, specifically the ajax_example_submit_driven_ajax() example in the ajax_example module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.