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 602415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:49:13+00:00 2026-05-13T16:49:13+00:00

I have a form that is added through the nodeapi displayed only on view

  • 0

I have a form that is added through the nodeapi displayed only on view mode. Users can select an item from a select menu and their choice will automatically get saved to the database with a hook_menu callback on change. If the user has javascript disabled, it’ll submit normally with the form api. This is all working fine, but now for security reasons I want to submit the ajax version via the form api too. My form_name_submit is simple like:

function mymodule_test_form_submit($form, &$form_state) {
  global $user;
  db_query("INSERT INTO {mymodule} (nid, uid, status, created) VALUES (%d, %d, %d, " . time() . ")", $form['#parameters'][2], $user->uid, $form_state['values']['mymodule_status']);
}

My ajax:

$('.mysubmit').css('display', 'none');
$('.myclass').change(function() {
  $.ajax({
    type: 'POST',
    url: Drupal.settings.basePath + 'mymodule/set/' + $nid + '/' + $('.myclass').val(),
    dataType: 'json',
    data: { 'ajax' : true, 'form_build_id' : $('#mymodule-form input[name=form_build_id]').val() }
  });
});

And my callback function:

function mymodule_set($nid, $status) {
  $form_build_id = $_POST['form_build_id'];
  $form_state = array('storage' => NULL, 'submitted' => FALSE);
  $form = form_get_cache($form_build_id, $form_state);
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form['#post'] = $_POST;
  $form['#redirect'] = FALSE;
  $form['#programmed'] = FALSE;
  $form_state['post'] = $_POST;
  drupal_process_form($form_id, $form, $form_state);
}

Originally my callback function was about the same as my submit function, but now I’m trying to use the submit function with ajax as well. Is this the right way to use drupal_process_form? The form is grabbed from the cache, it get’s validated and submitted if no errors? I’m using some code from this tutorial to apply to my situation: http://drupal.org/node/331941 There doesn’t seem to be any examples of what I’m trying to do. I also have $form[‘#cache’] = TRUE; in my form function.

How does drupal_process_form compare the submitted values with the original form to check for integrity? Am I supposed to add my values to the form_state since the form state will be empty with ajax. Been stuck on this for a few days, hopefully someone has experience with this.

Thanks.

  • 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-13T16:49:13+00:00Added an answer on May 13, 2026 at 4:49 pm

    I had to do somenthing similar to you in the past and read the same tutorial you posted, unfortunately there isn’t much information avalaible about this and it was headache for me to make it work. I don’t remember well the details but I was taking a look to the code I wrote and here are a couple of suggestions that may work for you:

    • Do not submit the form using your own jQuery code, instead use the new “#ahah” property of form elements that can be used for calling your callback in the onchange of the select http://api.drupal.org/api/drupal/developer–topics–forms_api_reference.html/6#ahah

    IF you are doing this in a node form, adding the #ahah property in a form alter may not work, I remember having seen an issue about this that wasn’t solved at that moment. if this is the case, use this code for attaching the ahah binding, you’ll not need “efect”, “method” or “progress” since you just want to submit the form, not to change anything about it:

    function YOURMODULE_form_alter(&$form, $form_state, $form_id) {
      if ('YOURCONTENTTYPE_node_form' == $form_id) {
        //the only way I could make it work for exisiting fields is adding the binding "manually"
        $ahah_binding = array(
          'url'   => url('YOURCALLBACKPATH'), 
          'event' => 'change',
          'wrapper' => 'FIELD-wrapper',
          'selector' => '#FIELD',
          'effect'   => 'fade',
          'method'   => 'replace',
          'progress' => array('type' => 'throbber'),
        );
    
        drupal_add_js('misc/jquery.form.js');
        drupal_add_js('misc/ahah.js');
        drupal_add_js(array('ahah' => array('FIELDd' => $ahah_binding)), 'setting');
    
        //force the form to be cached
        $form['#cache'] = TRUE;
      }
    }
    
    • Here is my callback function, note that it has some modifications from the tutorial you posted:

      function YOURMODULE_js() {
      
        // The form is generated in an include file which we need to include manually.
        include_once 'modules/node/node.pages.inc';
        // We're starting in step #3, preparing for #4.
        //I have to add the 'rebuild' element, if not an empty node was created
        $form_state = array('storage' => NULL, 'submitted' => FALSE, 'rebuild' => TRUE);
        $form_build_id = $_POST['form_build_id'];
        // Step #4.
        $form = form_get_cache($form_build_id, $form_state);
      
        // Preparing for #5.
        $args = $form['#parameters'];
        $form_id = array_shift($args);
        $form_state['post'] = $form['#post'] = $_POST;
        $form['#programmed'] = $form['#redirect'] = FALSE;
      
        // if you want to do any modification to the form values, this is the place 
      
        // Step #5.
        drupal_process_form($form_id, $form, $form_state);
        // Step #6 and #7 and #8.
        $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
      
      
        // Final rendering callback.
        drupal_json(array('status' => TRUE, 'data' => $output));
      }
      

    As I said before there are details that I have forgot but maybe this will help you.

    Good Luck.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The data model handles the data and the abstract relationships… May 14, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer What I suggest. Open Eclipse Create a new Java project… May 14, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer It looks like IIS is denying you access to the… May 14, 2026 at 7:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.