Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to be able to save a node when certain events happen in the browser. I.E the user changes tab. Is it possible to save a node in drupal using javascript?

Is there a javascript api tfor drupal? If not are there any contrib modules that add this? If it is custom code is needed is there an web service that can be called or would I need to role my own?

share|improve this question
Welcome to Drupal Answers :) As per the instructions you will have had to read through to post this question, it's important to include as much relevant detail as possible in the question. You're expected to show the effort you've put into researching the problem, what you've tried, what didn't work, etc. Be sure to include samples of the code you've tried, as proof that you're not just trying to get us to write your code for you. As it is, the answer to your question is simply "Yes, it's possible" – Clive Nov 22 '12 at 13:44
Question updated. Hopefully a little better. I looked in the api and found node_save but that is in php not javascrpt. – Rodney Nov 22 '12 at 14:14
Here's a link about creating a node using javascript, it's for d6 but perhaps it will give you something to play with: stackoverflow.com/questions/960343/… ie I guess you could send a nid as the parameter instead and then load the node, populate it and the save it using node_save() – Cyclone Nov 22 '12 at 14:21
up vote 4 down vote accepted

In order to achieve this on Drupal 7, you need to develop a custom module that does the following:

Register a menu hook that will act as the endpoint where your javascript will send the data for the node to be created.

Create a function that will be the callback of the hook and handle the actual creating and saving of the node. It can be something like this as mentioned in the answer above:

$data = json_decode($_REQUEST['title']); 
$node = new stdClass;
$node->type = 'article';
$node->title = check_plain($data->title);
echo json_encode(array('Status' => 'OK')); 

Notice drupal_exit() is needed otherwise you get the whole page rendering. In order to set other properties for example the body, you need to know the structure, it is usually something like

$node->fieldName[language][delta]['value'] eg, $node->body['und'][0]['value']

That's all it needs on the php side, now on the javascript side you will be needing to create an array containing the values for the node and submit it via ajax. Using jQuery it can be done like this:

    url:  Drupal.settings.basePath + queryPath,  
type: 'POST',
dataType: 'json',
data:  {'data': JSON.stringify(queryParameters)},  
error: function (XMLHttpRequest, textStatus, errorThrown) 
success: function (data) 

Data can contain anything returned from php...

Have fun


A more general and concrete solution is available through the use of the Services module that allows you to define a REST api over the various content types you create. This way you don't need to write any custom code on the php side,

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.