0

I am totally new to Drupal Ajax. In my projects one simple form with submit button only there for insert "yes" value to Database. so it should be done with Drupal Ajax. So When that button submit the value should be store in Db with out page load and after submit display text " successfully selected" instead of button.

So please anyone can help me for this task

Anaz
  • 1
  • 4

2 Answers2

0

Using Drupal Webform Ajax Module may help you.

rana_gee
  • 43
  • 6
0

Here I have created a button that do submit without page reload.

<?php

function db_store_form($form, &$form_state)
{
    $form['add_button'] = array(
        '#type'  => 'submit',
        '#name'  => 'Yes',
        '#value' => 'Yes',
        '#prefix' => '<div id="wrapper">',
        '#suffix' => '</div>',
        '#ajax' => array(
            'callback' => 'ajax_yes_callback',
            'wrapper' => 'wrapper',
            'method' => 'replace',
            'effect' => 'fade',
        ),
    );
    return $form;
}

function db_store_form_submit($form, &$form_state)
{
    drupal_set_message('Yes has been stored in yes_variable');
    // Store yes in db
    variable_set('yes_variable', 'yes');
}

function ajax_yes_callback($form, $form_state) {
    return $form['add_button'];
}

function db_store_menu()
{
    $items = array();

    $items['db_store'] = array(
        'title'           => 'db store',
        'page callback'   => 'drupal_get_form',
        'page arguments'  => array('db_store_form'),
        'access callback' => array(TRUE),
        'type'            => MENU_CALLBACK,
    );

    return $items;
}
Triss
  • 96
  • 5