Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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
3  
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
2  
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() –  Krister Andersson Nov 22 '12 at 14:21
add comment

1 Answer 1

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);
node_save($node);
echo json_encode(array('Status' => 'OK')); 
drupal_exit() 

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:

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

Data can contain anything returned from php...

Have fun

Update:

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
add comment

Your Answer

 
discard

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.