I can't pass post data in my custom module.
filenames:
mymodule.info
mymodule.mod
.info
name = My Module
description = My custom module.
package = DEV
version = 1.0
core = 7.x
.module
<?php
function mymodule_menu() {
$items = array();
$items['getcountries'] = array(
'title' => 'Get Countries',
'page callback' => 'getcountries',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['getstates'] = array(
'title' => 'Get States',
'page callback' => 'getstates',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function getcountries() {
$result = db_query("select distinct(country) from region");
$jsonarray = Array();
foreach ($result as $record) {
$jsonarray[] = array(
'item' => $record->country,
'value' => $record->country
);
}
$json = json_encode($jsonarray);
echo $json;
}
function getstates() {
echo $_POST["test"];
}
Ajax call
$(document).ready(function(){
$.ajax({
url: '/getstates',
type: 'POST',
data: '{"test":"1"}',
success : function () {
alert('ok');
},
error : function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
});
The first item "getcountries" is working fine however the second one is not. I can browse to http://mysite.com/getstates ok but when I call this function using ajax it is not passing the value of "test" which is "1" to $_POST["test"].
I am new to Drupal so I am positive that I miss something here. I thought I need a new set of eyes.