I need creating module that working with javascript, how can i get back the variable i passed from module file to js file ? I want to use it in module file but i cannot get it's value.

Comments

MathDoodler’s picture

Ok it seems you want to pass some javascript to some php. To do this, you can use javascript/ajax to send the information through a url / xmlhttp and then use php $_GET['var'] to grab the information. That is the only way I know of to pass javascript to php. Using cookies pro would work too. Use javascript to set the cookies and have PHP get it.

esperveras’s picture

Yes, that's the way i'm thinking for, but it's not working. I use POST method instead of GET method
this is my code in js file

            var fid = Drupal.settings.pdfread.key;
 			var count = parseInt(Drupal.settings.pdfread.key2)+1;
			jQuery.ajax({
				type:'POST',
				url: Drupal.settings.basePath+'pdfread/getvalue',
				data: 'count='+count,
				dataType: 'json',
				error: alert('error'),
			});

And this is my menu callback:

$items = array();
	$items['pdfread/getvalue'] = array(  
        'page callback' => 'set_download_count',
	'access callback' => TRUE,
	'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
	);
	return $items;
function set_download_count()
{
	if(isset($_POST['count']))
	{
	$count = (int)$_POST['count'];
	echo $count; 
        }
}

i don't know how it's not working :(
i attached it with click function but nothing happened when i clicked a button.

Jaypan’s picture

At a glance, it looks ok. A couple of questions:

1) Have you looked in either firebug on firefox, or the developer tools in Chrome or Safari to see if the request is actually being fired?
2) On that same note, are any javascript errors being seen in the consoles on either of those?

Also, what does your .click() function look like?