2

I'm a newbie with drupal.

I was trying to send data from drupal to node.js function and save the data from node into mongo.

I wrote a function in php

function sample_working_submit($form, &$form_state) { //Function to connect with node and save into Mongo using node
  $Name = check_plain($form_state['values']['Name']);
  $Age = check_plain($form_state['values']['Age']);
  $request_url = "http://10.20.5.112:3001/save/$Name/$Age ";
  $response = drupal_http_request($request_url);
}

This is working as long as there is no 'space' between the names(input). How can save the input with spaces.Why does this issue came?

How can i rewrite the function as post?

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
Okky
  • 10,338
  • 15
  • 75
  • 122

3 Answers3

2
<?php
$url = http://localhost:8080/myservlet;

$data = array (
    'name' => check_plain($form_state['values']['Name']),
    'age' => check_plain($form_state['values']['Age'])
    );

$response = drupal_http_request($url, $headers, 'POST', json_encode($data));
?>

This would POST data to URL. Note that you need to change logic in server side as well to receive POST data instead of GET data.

Refer here for more info

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
  • I was trying this but I'm getting error Warning: http_build_query(): Parameter 1 expected to be Array or Object. I included $headers = array(); – Okky Jun 04 '13 at 04:20
  • $options = array( 'method' => 'POST', 'data' => $jsonData, 'timeout' => 15, 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'), ); $result = drupal_http_request($request_url, $options); – Okky Jun 04 '13 at 04:38
1

If the space between the names is the issue, try using urlencode().

The code will be something like:

$Name = urlencode(check_plain($form_state['values']['Name']));
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
0

For POST requests I use SimpleBrowser instead of drupal_http_request(). It's easy to use and you'll be able to pass strings in any form.

carmel
  • 902
  • 7
  • 24