2011-09-12 115 views
0

我正在创建一个组合框之间存在依赖关系的模块。有三个组合框:国家,州和地区。如果我在国家组合框中选择印度,那么状态组合框将包含印度的所有国家,并且在区域情况下也是如此。AJAX错误Drupal-7

我已经通过使用AJAX做到了这一点。它正常工作在加的形式,但是当我要更新任何一个,就会出现以下错误:

An AJAX HTTP error occurred. 
HTTP Result Code: 500 
Debugging information follows. 
Path: /drupal/?q=system/ajax 
StatusText: Service unavailable (with message) 
ResponseText: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ajax' in 'where clause': SELECT district_name, country_id,state_id FROM {ajax_district} where id = ajax; Array 
(
) 
in edit_district_form() (line 291 of /home/mansmu/www/drupal/sites/default/modules/ajaxtest/ajaxtest.district.inc). 

我的代码是:

//////////////////////////////////////////////////////// 
///////// FUNCTION FOR EDIT 
//////////////////////////////////////////////////////// 
function edit_district_form($form, &$form_state) { 
$request_url = request_uri(); 
// $request_id to get id of edit district. 
$request_id = drupal_substr(strrchr($request_url, '/'), 1); 

//// FOR country 
$rows = array(); 
$result = db_query("SELECT id,country_name from {ajax_country} order by country_name"); 
while ($data = $result->fetchObject()) { 
$id = $data->id; 
$rows[$id] = $data->country_name; 
} 

//// FOR state 
$state = array(); 
$result = db_query("SELECT id,state_name from {ajax_state} order by state_name"); 
while ($data = $result->fetchObject()) { 
$id = $data->id; 
$state[$id] = $data->state_name; 
} 

// $district_name varible to get name from database for a requested id. 
$district_name = db_query("SELECT district_name, country_id,state_id FROM {ajax_district} where id = ".$request_id); 
// Loop For show vehicle district name in text field to be update. 
foreach ($district_name as $district) {*/ 
$form['values']['edit_country_name'] = array(
'#type' => 'select', 
// '#default_value' => "{$district->country_id}", 
'#required' => TRUE, 
'#options' => $rows, 
'#ajax' => array(
'effect' => 'fade', 
'progress' => array('type' => 'none'), 
'callback' => 'state_callback_edit', 
'wrapper' => 'replace_edit_state', 
), 
); 

$form['values']['edit_state_name'] = array(
'#type' => 'select', 
// '#default_value' => "{$district->state_id}", 
'#required' => TRUE, 
'#options' => array(), 
// The prefix/suffix provide the div that we're replacing, named by #ajax['wrapper'] above. 
'#prefix' => '', 
'#suffix' => '', 
); 

$form['values']['edit_district_name'] = array(
'#type' => 'textfield', 
'#size' => 30, 
//'#default_value' => "{$district->district_name}", 
'#maxlength' => 80, 
'#required' => TRUE, 
); 
} 

$form['edit_district_submit'] = array(
'#type' => 'submit', 
'#value' => t('Update'), 
); 
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), 'district'), 
); 

return $form; 
} 
//////////////////////////////////////////////////////// 
///////// FUNCTION FOR AJAX CALL BACK 
//////////////////////////////////////////////////////// 
function state_callback_edit($form, &$form_state) { 
// An AJAX request calls the form builder function for every change. 
// We can change how we build the form based on $form_state. 
if (!empty($form_state['values']['edit_country_name'])) { 
$country_id = $form_state['values']['edit_country_name']; 
$rows1 = array(); 
$result = db_query("SELECT id, state_name from {ajax_state} where country_id = $country_id order by state_name"); 
while ($data = $result->fetchObject()) { 
$id = $data->id; 
$rows1[$id] = $data->state_name; 
} 
$form['edit_state_name']['#options'] = $rows1; 
} 
return $form['values']['edit_state_name']; 
} 

回答

1

你行的代码在这里:

$request_id = drupal_substr(strrchr($request_url, '/'), 1);

是RETURNI纳克字符串“AJAX”,使你的第三个SQL语句是这样的:

SELECT district_name, country_id,state_id FROM {ajax_district} where id = ajax; 

显然“AJAX”是错在那里,我想你想从URL中的价值?您可以使用arg() function提取该这样你就不会需要运行drupal_substr

如果你的路径是ajax/12然后arg(0)将返回“AJAX”,arg(1)将返回12等。

希望帮助

UPDATE

如果您需要保存请求ID的形式做这样的事情在你的表单功能:

$form['request_id'] = array('#type' => 'value', '#value' => $request_id); 

然后,当你进入你的ajax回调你可以从$form_state['values']['request_id']$form['request_id']['#value']

+0

感谢您的回复。是的,我想要一个来自url的值,但是当我调用ajax时,url是系统/ ajax,否则编辑/ 12,其中12是我要更新的分区的id。 M无法在ajax调用中发送此ID,多数民众赞成在这种类型的问题即将到来, –

+0

难题,代码更新以上 – Clive

+0

亲爱的克莱夫你的更新代码没有工作。 –