0
votes

i have setup drupal webform, and trying to create popup windows to input submission. i put all of stuff in my custom module. my goals are:

  • when visitor input email address then click join, if email is valid then the data saved into database, and on the same popup windows visitor will get message "thanks for joining our program bla bla bla", so i want this pop up doesnt close after data submitted. other word, i don't want visitor be redirected to another page, i want visitor stay on this pop up.
  • when email address is not valid or input email form still empty then on this pop up windows visitor will get message "sorry email is required or bla bla bla".
  • pop up windows will not close until visitor click close button.

these are codes i write, the data submitted but it's still return form, i want it returns confirmation text.

my_module.module:

/* menu callback */

function my_module_menu() {
    $items['newsletter-popup'] = array(
        'title' => 'Join Club',
        'page callback' => 'ctools_ajax_newsletter',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_CALLBACK
    );

    return $items;
}

function ctools_ajax_newsletter() {
    $path = drupal_get_path('module', 'my_module');
    drupal_add_library('system', 'ui.dialog', false);
    drupal_add_library('system', 'ui.draggable', false);
    drupal_add_js($path . '/my_module.js');

    $output = '';
    $webform_nid = 1; // nid for my webform submission
    $node = node_load($webform_nid);
    $submission = (object) array();
    $webform = drupal_get_form('webform_client_form_' . $webform_nid, $node, $submission);
    $output .= '<div id="popup">';
    $output .= '<h2>Get $25 off your order</h2>';
    $output .= '<span>Sign up we\'ll give you $25 off your first order.</span>';
    $output .= drupal_render($webform);
    $output .= '<span>Limited Time Offer, One use per household</span>';
    $output .= '</div>';

    return $output;
}

function my_module_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'webform_client_form_' . WEBFORM_NID) {
        dpm($form_state);
        $form_state['redirect'] = 'confirmation';
        $nid = $form['#node']->nid;
        $form['actions']['submit']['#ajax'] = array(
            'callback' => 'my_module_webform_js_submit',
            'wrapper' => 'webform-client-form' . $nid,
            'method' => 'replace',
            'effect' => 'fade'
        );
    }
}

function my_module_webform_js_submit($form, $form_state) {
    $sid = $form_state['values']['details']['sid'];

    if ($sid) {
        $node = node_load($form_state['values']['details']['nid']);
        $confirmation = array(
            '#type' => 'markup',
            '#markup' => check_markup($node->webform['confirmation'], $node->webform['confirmation_format'], 'apa aja ya disini', TRUE),
        );

        return $confirmation;
    } else {
        return $form;
    }
}

my_module.js

(function($) {
    $(document).ready(function() {
        $('#popup').dialog({
            height: 'auto',
            width: 700,
            autoOpen: false,
            modal: true,
            resizable: false
        });

        $('a').click(function() {
            var status = false;
            if (this.className !== 'lightbox-processed') {
                if (!getCookie('newsletter_popup')) {
                    setCookie('newsletter_popup', 'true', 1);
                    $('#popup').dialog('open');
                } else {
                    status = true;
                }
                if (this.className === 'ui-dialog-titlebar-close ui-corner-all ui-state-hover') {
                    $('#popup').dialog('close');
                    status = true;
                } else {
//                next_location = this.href;
                }
            }
            if (this.id === 'bottomNavClose') {
                $('#popup').dialog('close');
            }
            return status;
        });
    });
})(jQuery);

that's it, i really need everyone help. Thanks Yuanita.

1

1 Answers

0
votes

It is better to use already built module to do the job for you, instead of writing code. You may want to take a look into this,

Drupal:Display webform on a popup box