Drupal 7 - I can't pass post data in module function
Posted
by
user2603290
on Stack Overflow
See other posts from Stack Overflow
or by user2603290
Published on 2013-07-21T01:27:35Z
Indexed on
2014/06/07
3:27 UTC
Read the original article
Hit count: 109
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.
© Stack Overflow or respective owner