Esempio Drupal con ajax
Salve,
ho trascritto da uno dei libri di Drupal 7 che ho acquistato il seguente esempio per testare le funzionalità ajax:
Creazione del link:
<?php
$items['Vis'] = array(
'title' => 'Vis1',
'access callback' => array('permessi'),
'page callback' => 'drupal_get_form',
'page arguments' => array('hello_world_simple_form_example'),
);
?>Funzione di callback:
<?php
function hello_world_simple_form_example($form, &$form_state) {
$form = array();
$form['hello_city'] = array(
'#title' => t("Choose a city"),
'#type' => 'select',
'#options' => array(
t('World') => t('World'),
t('Chicago') => t('Chicago'),
t('New York') => t('New York'),
t('Los Angelas') => t('Los Angelas'),
),
'#ajax' => array(
'callback' => 'hello_world_simple_form_callback',
'wrapper' => 'ajax_markup_div',
),
);
$form['ajax_markup'] = array(
'#prefix' => '<div id="ajax_markup_div">',
'#suffix' => '</div>',
'#markup' => t('Hello World'),
);
if (!empty($form_state['values']['hello_city'])) {
$form['ajax_markup']['#markup'] = t("Hello !city", array('!city' => $form_state['values']['hello_city']));
}
return $form;
}
function hello_world_simple_form_callback($form, $form_state) {
return $form['ajax_markup'];
}
?>La funzione che controlla gli accessi non l'ho postata perchè funziona.
Selezionando una città dal menù a tendina, parte la chiamata ajax e mi visualizza l'errore che ho allegato come file immagine.
Qualcuno può aiutarmi?
Grazie per la risposta.
Ps: Test effettuato su browser Firefox e Chrome, senza risultato.
| Allegato | Dimensione |
|---|---|
| Immagine.png | 80.74 KB |

Risposte
Ciao, ho testato il seguente
Ciao, ho testato il seguente codice (è uguale a quello postato tranne per i nomi e la funzione per il controllo dell'accesso):
<?php
function mgi_menu(){
$items['Vis'] = array(
'title' => 'Vis1',
'access arguments' => array('access content'),
'page callback' => 'drupal_get_form',
'page arguments' => array('mgi_form_example'),
);
return $items;
}
function mgi_form_example($form, &$form_state) {
$form = array();
$form['hello_city'] = array(
'#title' => t("Choose a city"),
'#type' => 'select',
'#options' => array(
t('World') => t('World'),
t('Chicago') => t('Chicago'),
t('New York') => t('New York'),
t('Los Angelas') => t('Los Angelas'),
),
'#ajax' => array(
'callback' => 'mgi_form_callback',
'wrapper' => 'ajax_markup_div',
),
);
$form['ajax_markup'] = array(
'#prefix' => '<div id="ajax_markup_div">',
'#suffix' => '</div>',
'#markup' => t('Hello World'),
);
if (!empty($form_state['values']['hello_city'])) {
$form['ajax_markup']['#markup'] = t("Hello !city", array('!city' => $form_state['values']['hello_city']));
}
return $form;
}
function mgi_form_callback($form, $form_state) {
return $form['ajax_markup'];
}
?>
e funziona correttamente. Strano perché sembra che l'output del server sia corretto dall'immagine che hai inviato, ma non viene interpretato correttamente. Infatti analizzando le richieste/risposte tra client e server questa è la risposta che ottengo ad ogni richiesta Ajax:
Date Sun, 27 Nov 2011 20:09:11 GMT
Server Apache/2.2.14 (Ubuntu)
X-Powered-By PHP/5.3.2-1ubuntu4.10
Expires Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified Sun, 27 Nov 2011 20:09:11 +0000
Cache-Control no-cache, must-revalidate, post-check=0, pre-check=0
Etag "1322424551"
Content-Language en
Content-Length 1010
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Content-Type application/json; charset=utf-8
[{"command":"settings","settings":{"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"bartik","theme_token":"LY7DmS3G17ZxVSeZZAyOkvjGmBLsgLAurvYFG3mc95E","css":[]},"overlay":{"paths":{"admin":"node\/*\/edit\nnode\/*\/delete\nnode\/*\/revisions\nnode\/*\/revisions\/*\/revert\nnode\/*\/revisions\/*\/delete\nnode\/add\nnode\/add\/*\noverlay\/dismiss-message\nuser\/*\/shortcuts\nadmin\nadmin\/*\nbatch\ntaxonomy\/term\/*\/edit\nuser\/*\/cancel\nuser\/*\/edit\nuser\/*\/edit\/*\ndevel\/*\nnode\/*\/devel\nnode\/*\/devel\/*\ncomment\/*\/devel\ncomment\/*\/devel\/*\nuser\/*\/devel\nuser\/*\/devel\/*\ntaxonomy\/term\/*\/devel\ntaxonomy\/term\/*\/devel\/*","non_admin":"admin\/structure\/block\/demo\/*\nadmin\/reports\/status\/php"},"ajaxCallback":"overlay-ajax"}},"merge":true},{"command":"insert","method":null,"selector":null,"data":"\u003cdiv id=\"ajax_markup_div\"\u003eHello Chicago\u003c\/div\u003e","settings":null},{"command":"insert","method":"prepend","selector":null,"data":"","settings":null}]
Spero ti sia utile.
Mi dispiace, ma l'errore è
Mi dispiace, ma l'errore è sempre quello. Non so cosa pensare..
Ti ringrazio comunque per avermi risposto.