Ajax not working in drupal 6 - php

in Form
$form['assignJob'] = array(
'#type' => 'checkbox',
'#title' => 'AssignJob',
'#default_value' => 1,
'#suffix'=>"<script>$js</script>"
)
in $js i defined the JS.
$js = <<<EOJS
Drupal.behaviors.checkboxrender = function(context) {
$('#edit-assignJob', context).change(function(event, ui) {
var method = $(this).val();
if(method){
$.get('/assignJob/',null,responseDetails);
}
});
var responseDetails=function(response){
alert(response);
}
};
EOJS;
In hook_menu in defined the menu
$items['assignJob'] = array(
'page callback' => 'assignee',
'type' => MENU_CALLBACK,
'access arguments' => array('access administration'),
);
return $items;
function assignee() {
$output='xxxx';
return drupal_json(array('status' => TRUE, 'data' => $output));
drupal_exit();
}
So whenever i click on checkbox it return me the blank value. Can anybody help in this.?
When i render this i got warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'assignee' not found or invalid function name in C:\Server\www\drupal-final\includes\menu.inc on line 350.

I'll try to help you . But all i know that what's responsible for Ajax call or javascript is the ahah property . have a read on drupal api http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/6#ahah
Also read this http://drupal.org/node/348475
By the way i never saw suffix that takes Script . i always use ahah property .
You can download this example http://www.kristen.org/content/drupal-ahah-form-examples
and read this article http://fuseinteractive.ca/blog/how-create-custom-drupal-ajax-module-0

Related

Create custom form display it in colorbox in each page in drupal 7?

I want to display custom form (which is custom module) in colorbox in each page.
I have created form in drupal 7 which is working fine if i run it by calling it in url but i need to call this module in colorbox.
my custom module(regform) code is
function regform_init() {
drupal_set_message('Our module is alive!');
}
function regform_menu() {
$items = array();
$items['regform'] = array(
'title' => 'Custom page',
'page callback' => 'drupal_get_form',
'page arguments' => array('regform_form'),
'access arguments' => array('access content'),
'access callback' => TRUE
);
return $items;
}
function regform_form($form, &$form_state) {
$form['#suffix'] = '<div id="test-ajax"></div>';
$form['email'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Enter Email',
'#size' => 50,
'#maxlength' => 50,
'#required' => TRUE, //make this field required
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#validate' => 'regform_form_validate',
'#ajax' => array(
'callback' => 'regform_form_ajax_callback',
'wrapper' => 'test-ajax'
),
'#submit' => array('regform_form_ajax_callback'),
);
return $form;
}
function regform_form_ajax_callback($form, &$form_state) {
/*Fire database query*/
/*Validation msg div block call here*/
return "<div id='test-ajax'></div>";
}
function regform_form_validate($form, &$form_state) {
if (trim($form_state['values']['email']) == ''){
form_set_error('email', t('Please Enter Email'));
}
if(!valid_email_address($form_state['values']['email'])){
form_set_error('email', t('Enter Valid Email'));
}
}
function regform_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['input'] = array();
}
This form is working fine.
Also I dont have any idea about how to use colorbox in drupal 7.
Any help will be appreciated.
Thanks in Advance.
I suggest to use chaos tools for modal popup form. Its easy to implement and integrate into Drupal 7.
A great example of Ctools modal form is given at here
For modal forms in Drupal, I highly suggest taking a look at chaos tools. It provides a great AJAX API (that can handle the form submission).
There is another module you can take a look at, modal forms. It's built on top of Ctools and provide an easy way to show forms in modal.

Using Drupal AJAX with Drupal Messages

I have a custom module right now in order to text AJAX functionality with Drupal Messages. The module has the following code (test_error.module):
<?php
function test_error_user_view_alter(&$build) {
//_assassinations_player_profile($build);
}
function test_error_form_alter(&$form, &$form_state, $form_id) {
$form['test_error'] = array(
'#type' => 'button',
'#name' => 'Error',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_error',
'wrapper' => 'the-wrapper-div-field',
),
);
$form['test_warning'] = array(
'#type' => 'button',
'#name' => 'Error',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_warning',
'wrapper' => 'the-wrapper-div-field',
),
);
return $form;
}
function test_error_error() {
drupal_set_message("Header error", 'error');
}
function test_error_warning() {
drupal_set_message("Header warning", 'warning');
}
Then, in page.tpl.php, I have the following to output $messages:
<div id="messages-wrap"><?php print $messages; ?></div>
The AJAX function happens when the button is clicked - setting the message - while the message only displays after the page reloads. I tried to hack my way through the return AJAX call like so:
jQuery(document).ready( function(){
jQuery('.ajax-processed').each( function(){
jQuery(this).unbind();
});
jQuery('#edit-test-warning, #edit-test-error').click( function() {
var the_form = jQuery(this).closest('form');
var form_action = the_form.attr('action');
var details = the_form.serialize();
jQuery.ajax({
type: "POST",
url: form_action,
data: details
}).done( function(){
jQuery('#messages-wrap').load("/ #messages-wrap");
});
});
});
The binding of a click event on the #edit-test-x buttons never occurs because of Drupal's native ajax.js preventing it by adding its ajax-x classes.
It's driving me crazy that this problem is so persistent and so difficult to solve when the goal is such a simple thing. What am I missing? Google searching and StackOverflow browsing has come up surprisingly fruitless.
Thanks!
At first, here is some explanations:
1. So you trying to render drupal messages as part of the form, you must to render drupal messages manually.
2. With using '#ajax' key, there is no additional (custom) JS required.
3. On the button click the form is rebuilded, so, all you need is to place message rendering code inside the form logic.
Code example for the simple case:
function test_error_form(&$form, &$form_state) {
$form['test_error'] = array(
'#type' => 'button',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_error',
'wrapper' => 'the-error-container',
),
);
$form['test_warning'] = array(
'#type' => 'button',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_warning',
'wrapper' => 'the-error-container',
),
);
return $form;
}
function test_error_error($form) {
drupal_set_message("Header error", 'error');
return array(
'#type' => 'markup',
'#markup' => theme('status_messages'),
);
}
function test_error_warning($form) {
drupal_set_message("Header warning", 'warning');
return array(
'#type' => 'markup',
'#markup' => theme('status_messages'),
);
}
Note, that you must have div with id="the-error-container" on the page.
If you want to render new drupal messages on the standard place, try to use ajax commands, like this:
$commands = array();
$commands[] = ajax_command_replace($selector, theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
where $selector - is a css/jquery selector of your message area.
Please ask, if there is some unclear.

drupal 7 form, simple check on hook_submit

I'm trying to understand drupal forms by trial and error (not getting along famously with Drupal Documentation), and I'm a bit confused as why doesn't this work:
function sform() {
$form['password'] = array(
'#type' => 'password',
'#title' => 'enter 1234'
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => '1234 and then click',
);
$form['#method'] = 'post';
return $form;
}
function sform_submit($form_id, $form) {
if (isset($form['values']['password']) && $form['values]']['password'] == "1234") {
drupal_set_message(t('Success;'), '');
}
else {
drupal_set_message("({$form['values']['password']})", 'warning');
drupal_set_message(t('Failure'), 'error');
}
}
I know it all extremely basic and the idea isn't for it to stay in any way like this. As I said I'm just prodding the API to see what works and how.
But the thing is, the basic check is that the "password" field exists and is equal to "1234". And it fails every time. In the failure branch I spit out what value I've got from "password", and I see that it was "1234" nonetheless...
Is there some very obvious reason why the comparison between $form['values']['password'] and "1234" shouldn't be working as I intend here???
You have a typo on your if test:
$form['values]']['password']
//should be
$form['values']['password']
Also it looks like you have your functions setup incorrectly, take a look here: https://api.drupal.org/api/drupal/includes!form.inc/group/form_api/7
You should be passing $form and $form_state (by reference) when using forms.
Taken from link above:
function my_module_example_form($form, &$form_state) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function my_module_example_form_validate($form, &$form_state) {
// Validation logic.
}
function my_module_example_form_submit($form, &$form_state) {
// Submission logic.
}
Plus your menu hook should call drupal_get_form as the callback and your function name as the page arguements.
$items['sform'] = array(
'title' => 'My Form',
'page callback' => 'drupal_get_form',
'page arguments' => array('sform'),
'access arguments' => array('some rule'),
);

drupal 6: using ahah for forms with markup types

function MyModule_menu() {
$items['blah'] = array(
'title' = 'blah',
'page callback' => 'blah_page',
'type' => MENU_NORMAL_ITEM
);
$items['clickPath'] = array(
'title' => 'A title',
'page callback' => 'clickPath_page',
'type' => MENU_CALLBACK,
);
return $items;
}
function blah_page() {
$output = drupal_get_form(MyModule_form);
return $output;
}
function clickPath_page() {
return ('you clicked me!');
}
function MyModule_form($form,&$form_state) {
$output = '<div id="clickDiv">Click me</div>';
$form['blah'] = array(
'#type' => 'markup',
'#value' => $output,
'#ahah' => array(
'event' => 'click',
'path' => 'clickPath',
'wrapper' => 'clickDiv',
),
);
return $form;
}
Why won't the above work? Is it not possible to use ahah and events on form types of 'markup'? Do I have to use my own custom javascript?
You can stop reading here! I would like to end my sentences and question here, but stackoverflow is forcing me to input a minimum amount of characters. Apologies in advance!!!!
If you look at the Form API under "Special Elements" you can see that the #ahah attribute is not supported for the markup form type.
So I'm afraid you will have to roll your own JS in this case, or convert the markup element into a normal form element (which it doesn't look like will work for your purposes).

Drupal dynamic internal redirect

What I want is pretty simple. I have registered a path
function spotlight_menu() {
$items = array();
$items['congres'] = array(
'title' => 'Congres',
'title arguments' => array(2),
'page callback' => 'taxonomy_term_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
When this menu item is triggered, I want to redirect (without changing the url) to a taxonomy page, of which the term is chosen in a function that runs when this function is called.
How can I do this (especcially without changing the url) ?
You can't call taxonomy_term_page directly as your page callback as you'd need to provide a load function to load the term, which is just going to be too difficult with the setup you've got.
Instead, define your own page callback as an intermediary and just return the output from taxonomy_term_page directly:
function spotlight_menu() {
$items = array();
$items['congres'] = array(
'title' => 'Congres',
'page callback' => 'spotlight_taxonomy_term_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function spotlight_taxonomy_term_page() {
// Get your term ID in whatever way you need
$term_id = my_function_to_get_term_id();
// Load the term
$term = taxonomy_term_load($term_id);
// Make sure taxonomy_term_page() is available
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
// Return the page output normally provided at taxonomy/term/ID
return taxonomy_term_page($term);
}

Categories