首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJAX错误Drupal-7

AJAX错误Drupal-7
EN

Stack Overflow用户
提问于 2011-09-12 13:27:47
回答 1查看 5.6K关注 0票数 0

我正在创建一个模块,在该模块中,组合框之间存在依赖关系。有三个组合框:国家、州和地区。如果我在国家组合框中选择印度,那么邦组合框将包含印度的所有邦,在地区情况下也是如此。

我是通过使用AJAX来做到这一点的。它在add表单中正常工作,但是当我要更新任何一个时,会发生以下错误:

代码语言:javascript
复制
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).

我的代码是:

代码语言:javascript
复制
////////////////////////////////////////////////////////
///////// 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;
}
代码语言:javascript
复制
////////////////////////////////////////////////////////
///////// 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'];
}
EN

回答 1

Stack Overflow用户

发布于 2011-09-12 14:31:54

您的代码行在这里:

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

返回字符串'ajax',使您的第三个SQL语句看起来如下:

代码语言:javascript
复制
SELECT district_name, country_id,state_id FROM {ajax_district} where id = ajax;

显然'ajax‘是错误的,我猜您想从URL中得到一个值吗?您可以使用arg()函数来提取它,这样您就不需要运行drupal_substr等等。

如果您的路径是ajax/12,那么arg(0)将返回'ajax',arg(1)将返回12等等。

希望这有帮助

更新

如果需要在表单中保存请求ID,请在表单函数中执行如下操作:

代码语言:javascript
复制
$form['request_id'] = array('#type' => 'value', '#value' => $request_id);

然后,当您进入ajax回调时,可以从$form_state['values']['request_id']$form['request_id']['#value']获得

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7388643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档