[HELP] How to pass a value in a form, from Javascript (jQuery) to Drupal (PHP)
ardnet - March 27, 2008 - 13:52
How can i pass a value from a form, say.... from a select form. An event handle is apply in to that select form which is onChange (we apply a jQuery code on it). Then i want to grab that value using...i dunno...whether i use $_REQUEST['var'] or anything else, when i choose something to that select form.
How am i supposed to that in my php (Drupal) code and also in jQuery?
I got a similar code that might be can help my problem, which is in here: http://f5sitedesign.com/drupalcamp
But it seems doesnt work in a menu that says "Add two numbers with AJAX" and "Watch a dropdown wtih AJAX".
Anybody know why it doesnt work?
Thanx in advance.
Sory for my bad english

Look into the jQuery.ajax()
Look into the jQuery.ajax() function. It's a little difficult to understand at first, but once you get the hang of it, you'll find it does what you're looking for quite well. In particular, look at the options under the "options" tab.
On the PHP side, you can use drupal_to_js() to serialize a PHP variable into a JSON string which the jQuery.ajax() function will then easily convert into a JavaScript object for you to use back on the client side. Just set the "dataType" parameter of the object you pass to .ajax() to "json".
Hope this helps.
I still don't get it...
I'm sorry Garret, is it me being stupid or i am a real stupid :P
I still don't get it how to set up my code in jQuery and also in PHP (Drupal).
In jQuery i did something like this:
-------------------------------------------------------------------------
$('div#select_cat select').change(function(){
$.ajax({
url:'node/add/cl-ad',
dataType:'json',
type:'POST',
data: {'foo' : this.value}
});
});
-------------------------------------------------------------------------
And in PHP (Drupal), i did something like this:
-------------------------------------------------------------------------------------------------
function cl_ad_menu($may_cache) {
$items = array();
if (!$may_cache) {
$items[] = array(
'path' => 'node/add/cl-ad',
'title' => t('Advertisement'),
'access' => user_access('create advertisement')
);
return $items;
}
function cl_ad_form($node, $name = NULL){
if(isset($_GET['foo']))
{
$form['step1']['subcategories'] = array(
'#type' => 'select',
'#title' => t('Select a Sub Category'),
'#required' => TRUE,
'#options' => $options2,
'#weight' => 0,
'#prefix' => '',
'#suffix' => ''
);
}
}
------------------------------------------------------------
All I wanna do just grab a value from "foo" in php (Drupal) that were pass from a jQuery, that's all.
Thx in advance, again.
With this example, the
With this example, the contents of foo will be in $_POST['foo'], not $_GET['foo'].
Also, make sure your Drupal code is going to pass something back to jQuery that it will be able to handle.
Help me please...
anyone... anybody...
Help me please...
anyone... anybody...?
Drupal.settings
Try JavaScript, AJAX, AHAH API, the section on Drupal.settings. and http://api.drupal.org/api/function/drupal_add_js
hth
Frank