<?php
/*product.module*/
function product_menu() {
$items = array();
$items['product'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('product'),
'access arguments' => TRUE,
'type' => MENU_CALLBACK,
);
$items['product/check_name'] = array(
'page callback' => 'check_name',
'access arguments' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function product() {
$path = drupal_get_path('module', 'product');
drupal_add_js($path . '/product.js', 'module');
$form['product_name'] = array(
'#title' => t('Product Name'),
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 30,
'#description' => t('Please enter product name.'),
);
$form['check_name'] = array(
'#type' => 'markup',
'#value' => "" . t('Check Product Name') . " ",
);
$form['status'] = array(
'#type' => 'markup',
'#value' => " ",
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['cancel'] = array(
'#type' => 'markup',
'#value' => l(t('Cancel'), 'product_mgmt'),
);
return $form;
}
function check_name() {
$name = strtolower($_GET['name']);
$query = "SELECT COUNT(*) AS total FROM {product} WHERE LOWER(product_name) LIKE ('%s')";
$rs = db_query($query, $name);
$info = db_fetch_object($rs);
$total = $info->total;
if ($total) {
echo "$('#status').html('This product is available.');";
}
else {
echo "$('#status').html('This product is not available.');";
}
}
?>
Don’t return anything in “check_name()” function otherwise it will return the whole page when we access “product/check_name” path through AJAX. |