Community

Getting "The form has become outdated..." after a radio button form.submit()

I'm putting a form together that contains a set of radio buttons. Each of the buttons has an onclick handler that does "form.submit(this)" -- it just tells that page to call back to itself so that it can redisplay its contents in a way determined by the radio button that was clicked. There will not be a "submit" button in the form itself.

This works, but, when the form is submitted, I get the "The form has become outdated..." message. I've found some other posts that suggested removing the #token field from the form; I've done that, and, while I no longer get the "outdated" message, I can see that the form process is taking a second pass through my form's after_build function. This makes me a bit suspicious about the possibility that I'm outsmarting myself here.

Any thoughts about this? Is there a better way to do what I'm trying to do?

Comments

You need to use the Form API

You need to use the Form API so as not to invalidate the form. The developer examples module has some ajax examples that may help.

Hmm -- I thought I was using

Hmm -- I thought I was using the Form API :). At least, I'm assigning the onClick action to the individual radio buttons by setting their #attributes value in an _after_build handler, sorta like this (where 'role' is the form element that got the radios back in my form function):

foreach ($form['role']['#options'] as $a_role_key => $a_role_value) {
     $form['role'][$a_role_key]['#attributes'] = array(
          'onclick' => 'form.submit(this)',
     );
}

Is there more in the way of the Form API that I can do / need to do to make this work properly?

Logical what are you trying

Logical what are you trying to do when the element is clicked?

Kinda what I said before: The

Kinda what I said before: The page does a database query, pulls back a bunch of records, and displays them. The radio buttons are meant to provide some tuning of exactly what records are shown -- "show me all the users", "show me just the users who are teachers", "show me just the users who are principals", etc. The page gets the form parameters and tailors the database call to get the corresponding entries. There's no need for a submit button; clicking the radio button should be all that's needed.

In the developers example see

In the developers example see the "Simplest AJAX Example".

You could use that as a starting point, replace the select with the radios and instead of changing the description for the text box, replace the textbox with a markup element that you will replace when a radio is selected

I don't think this is the

I don't think this is the right approach for what I'm doing -- I'm not just changing the contents of a text box, I'm doing a whole new database query based on the setting of the radio button. Yeah, you could do that with Ajax, but I'm not sure it solves anything.

Anyway, thanks to all. I'm going to assume for the moment that the repeated call to the _after_build handler is a cost of doing business this way, and will stumble forward.