drupal ajax create node

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-05-01T18:21:21Z Indexed on 2010/05/01 18:27 UTC
Read the original article Hit count: 439

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX