How to pass jquery variable to drupal (ajax)

I want to pass

var x = 10; to the drupal / php $ x variable without refreshing the page.

Can anyone suggest?


Thank you so much for your reply. In fact, I am developing a custom module for an e-commerce site (Drupal 6). On the product page, I have 4 attributes in the form of a text field (width, width, height, height). Performing some calculations, I got a new price for this product. I made this calculation in j-request. Now I change "uc_add_to_cart_form" and add 1 hidden field and get a new jquery price in this hidden field using $ _SESSION. Up to this point, everything is working fine.

I want to keep this new price in the product node. I cannot set this new price to the price of $ node →. I am using uc_ajax_cart for the add to cart button.

Can anyone suggest me how can I continue?

+7
source share
2 answers

Passing variable data from JavaScript to Drupal / PHP is one thing. Passing information from Drupal / PHP to JavaScript is another thing. I think you mean the first in your question. Therefore, I will continue and respond to this.

Passing variables from JavaScript to Drupal / PHP

1) Enter the menu item . You can do this on an existing added or new custom module, it depends on what you are doing. It is fairly easy and straightforward, it is also well documented on the Drupal website.

  $items = array(); $items['my_custom_callback/%'] = array( 'title' => 'My Custom Callback', 'description' => 'Listing of blogs.', 'page callback' => 'my_custom_php_function', 'page arguments' => array(1), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); 

Break the above example.

$items['my_custom_callback/%']

$ items is just an array and can be called anything. If my site URL is www.alexanderallen.name, then http://www.alexanderallen.name/my_custom_callback will be the resource that I would call from my JavaScript application.

In the array key, the percent symbol after the word my_custom_callback is a placeholder. Here you will transfer your data from JS to PHP. Therefore, if the value of x is 10, the call from your jQuery will look like this:

http://www.alexanderallen.name/my_custom_callback/10

When you load this URL, Drupal will find your menu hook and call the function defined in the page callback array key. In this case, it will be:

 <?php /** * Function that gets called from JQuery asynchronously. */ function my_custom_php_function($argument) { // Do something with $argument... echo $argument; } ?> 

There you can do whatever you want with the value of the variable x, for example, store it in a database.

If you were to place a menu hook in a user module and your module name was example_module, the menu hook and PHP user callback would look like this:

 <?php /** * @file example_module.module Handles fancy AJAX functionality. * */ /** * Implementation of hook_menu * * @see http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_menu/6 */ function example_module_menu() { $items = array(); $items['my_custom_callback/%'] = array( 'title' => 'My Custom Callback', 'description' => 'Listing of blogs.', 'page callback' => 'my_custom_php_function', 'page arguments' => array(1), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items; } /** * Function that gets called from JQuery asynchronously. */ function my_custom_php_function($argument) { // Do something with $argument... echo $argument; } ?> 

If you want to pass multiple variables from jQuery to Drupal / PHP, you can consider JSONifying them in JavaScript before passing it to PHP. Remember that if the payload is very large, you should consider jQuery.post () instead of .get (). PHP functions for decoding JSON here at php.net .

Remember to return $ items at the end of the example_module_menu () function. If you want to pass two or more arguments from JavaScript to PHP, then the menu item will look something like this:

 function example_module_menu() { $items = array(); $items['my_custom_callback/%/%/%'] = array( 'title' => 'My Custom Callback', 'description' => 'Listing of blogs.', 'page callback' => 'my_custom_php_function', 'page arguments' => array(1, 2, 3), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items; } function my_custom_php_function($arg1, $arg2, $arg3) { // Do something ... $sum = $arg2 + $arg3; echo "Hi". $arg1 .", X + Z = ". $sum; } ?> 

In this case, I pass three arguments to PHP. 'page arguments' indicates the portion of the URL that you want PHP to pass. If you must write 'page arguments' => array(0), , then the argument to your PHP function is my_custom_callback.


2) Call the menu from your JS code using jQuery. JQuery provides various AJAX methods. Which one you will use will depend on the format of the data you are dealing with and the size, among other things.

Most likely, you will use one of the two jQuery methods. . post () or . get () , bearing remember that these mail requests will get a larger data payload than receiving requests. Therefore, if the size of the variable is x = 10, you might want to stick with .get (). Both .post () and .get () methods are extension or method based . Ajax () . The .ajax () method is more flexible because it provides you with more options, for example, the ability to specify a data format (JSON or plain text), send a username and password along with your request, choose between synchronous or asynchronous requests, and whether the browser should cache or do not fulfill the request.

This is the method signature for the JQuery.get () method:

jQuery.get( url, [ data ], [ callback(data, textStatus, XMLHttpRequest) ], [ dataType ] )

If you are going to send your data using GET to Drupal / PHP using the above example, you can do something like this:

  <script type='text/javascript'> var name = 'alexander'; var x = 10; var z = 20; $.get( // Callback URL. "http://www.alexanderallen.name/my_custom_callback/"+ name +"/"+ x +"/"+ z ); </script> 

Note that using the data argument in the .get () method is optional, and in my example I skipped it, but achieved the same effect by combining the data that Drupal / PHP needs to pass with the rest of the URL.

If I were to run this, the HTTP GET request would look like this:

http://www.alexanderallen.name/my_custom_callback/alexander/10/20

and the answer will look like this:

Hi Alexander, X + Z = 30

Passing data from Drupal to JavaScript

For this, then you will use Behaviors, I will not go into details, because I think this is not your question, but you can go to the official documentation for the behavior of Drupal 6.

+35
source

You need to know:

Menu API:
http://api.drupal.org/api/drupal/includes--menu.inc/group/menu/7
Some JSON Drupal Features (Possible)
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_json_encode/7
variable_set ()
http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variable_set/7
And jQuery ajax API:
http://api.jquery.com/category/ajax/

Essentially, you need to create a menu item that delegates a callback (be careful with your permissions), and then call it with $ .ajax (). If you need to do something with the answer, see json_encode Functionality.

You might want to take a look at some sample menus:
http://drupal.org/project/examples

I think there is a Drupal interface for the jQuery ajax API. Perhaps someone can comment if they know a better way.

+4
source

All Articles