drupal ajax create node
- by Mark
I need to create a drupal node via ajax. I'm looking for some instructions as in: http://stackoverflow.com/questions/960343/creating-a-drupal-node-with-javascript
But with more detail, and including the js steps.
The JS will probably look something like this:
var title = 'Demo Node Title'
$('button').click(function () {
$.post('demo/js', {"title" : title}, function(data) {
var json = eval("(" + data + ")");
if (json['status'] == "error") {
alert(json['message']);
}
else if (json['status'] == "success") {
alert(json['message']);
// Need to return the nid of the new node here. Anyone know how to do this?
}
});
});
The PHP (copied from the other question, but I don't understand it much, how do I set the title of the node? also as the comments says, how do I set an input filter?):
<?php
/**
* Implementation of hook_menu().
*/
function demo_menu() {
$items = array();
$items['demo/js'] = array(
'title' => 'Demo page',
'page callback' => 'demo_js_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback that saves a node.
*/
function demo_js_page() {
if (isset($_REQUEST['title'])) {
$node = new stdClass;
$node->type = 'blog';
$node->title = check_plain($_REQUEST['title']);
node_save($node);
drupal_set_message(t('Created node %title', array('%title' => $_REQUEST['title'])));
}
return t('Thanks for visiting');
}
Thanks in advance.