I am relatively new to Drupal (7) and PHP so this may be a bit of a basic question through I have not managed to find out what I needed in the questions so far, hence the post.
I have a drupal site that will connect to a simple CRUD REST webservice(.net 4) to retreive and post data.
I have been finding it difficult to determine how to load the data obtained from the website into a simple drupal form. The data will be transfered via JSON format but I an not sure how to retreive the data from the response array and populate the fields of the form with the data obtained from the website. Assume the JSON fields returned match the form field names.
In my example I have the http_request in the submit function through really it should be in a page load function.
So the questions are : how to you populate the fields of the form from the retreived data ? What is the syntax to get the data out of the json_array? The data needs to be retrieved when the page loads, what is the best way of acheiveing this ?
Here is the example code , I have been playing around in the submit. I have put a rebuilt in , is this the best way to persist the data on the screen ? I have tried to reset the 'Surname' field to a new value but no joy..
<?php
/**
* @file
* Test activation form
*/
/**
* Implements hook_menu()
*/
function activation_menu() {
$items['activation'] = array(
'title' => 'Registration form',
'page callback' => 'drupal_get_form',
'page arguments' => array('activation_nameform'),
'access callback' => TRUE,
// 'access_callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Define the form
*/
function activation_nameform() {
$form['Firstname'] = array(
'#Firstname' => t('Firstname'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your First name.'),
);
$form['Surname'] = array(
'#Surname' => t('Surname'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Surname.'),
);
$form['Username'] = array(
'#Username' => t('Username'),
'#type' => 'textfield',
'#maxlength'=> 100,
'#description' => t('Please enter your Username.'),
);
$form['Password'] = array(
'#Password' => t('Password'),
'#type' => 'textfield',
'#maxlength'=> 8,
'#description' => t('Please enter your Surname.'),
);
$form['Organisation'] = array(
'#Organisation' => t('Organisation'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Organisation.'),
);
$form['Address1'] = array(
'#Address1' => t('Address1'),
'#type' => 'textfield',
'#maxlength'=> 150,
'#description' => t('Please enter your first full line of Address.'),
);
$form['Address2'] = array(
'#Address2' => t('Address'),
'#type' => 'textfield',
'#maxlength'=> 150,
'#description' => t('Please enter your second full line of Address.'),
);
$form['Town_city'] = array(
'#Town_city' => t('Town_city'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Town or City.'),
);
$form['Region'] = array(
'#Region' => t('Region'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Region.'),
);
$form['PostCode'] = array(
'#PostCode' => t('PostCode'),
'#type' => 'textfield',
'#maxlength'=> 10,
'#description' => t('Please enter your PostCode.'),
);
$form['Postcode'] = array(
'#Postcode' => t('Postcode'),
'#type' => 'textfield',
'#maxlength'=> 15,
'#description' => t('Please enter your Postcode or ZIP code.'),
);
$form['Country_options'] = array(
'#type' => 'value',
'#value'=> array(t('UK'),t('Europe'),t('USA')),
);
$form['Country']['favourite_country'] = array(
'#title' => t('Favourite Country'),
'#type' => 'select',
'#value'=> array(t('UK'),t('Europe'),t('USA')),
'#description' => t('Please enter your Country'),
'#options' => $form['Country_options']['#value']
);
$form['Status'] = array(
'#Status' => t('Status'),
'#type' => 'textfield',
'#maxlength'=> 150,
'#description' => t('Please enter your first full line of Address.'),
);
$form['Add_credits'] = array(
'#Add_credits' => t('Add_credits'),
'#type' => 'textfield',
'#maxlength'=> 15,
'#description' => t('Please enter additional credits'),
);
$form['TandC'] = array(
'#TandC' => t('TandC'),
'#type' => 'textfield',
'#maxlength'=> 5,
'#description' => t('TandC'),
'#value'=> 30
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}
/**
* Handle post - validation form submission
*/
function activation_nameform_submit($form,&$form_state) {
$name = $form_state['values']['Firstname'];
drupal_set_message(t('Thanks ,%name',
array('%name' => $name)));
$request = drupal_http_request('http://somewebsite.com/Customers/?ID=10');
$jsonarray =drupal_json_decode($request->data);
$form_state['rebuild']=TRUE;
$form['Surname']['#value']='Hello mother';
}
Continuation of answer by using
echo '<pre>';var_dump($request); echo '</pre>'
I can now see the request information:
["data"]=>
string(346) "?{"CustomerID":10,"FirstName":"Howard Philip","LastName":"Lovecraft","Organisation":"superglue","AddressID":null,"Status":0,"Credits":0,"Type":0,"ExpiryDate":"0001-01-01T00:00:00","UserName":"xxx@hotmail.com","Password":"","CustomerIconURL":"","CountryCode":"","LastUpdated":null,"DateCreated":"2013-04-18T07:43:10.123","ApplicationID":null}"
["protocol"]=>
string(8) "HTTP/1.1"
["status_message"]=>
string(2) "OK"
["headers"]=>
array(6) {
["content-length"]=>
string(3) "346"
["content-type"]=>
string(31) "application/json; charset=utf-8"
["server"]=>
string(17) "Microsoft-IIS/7.5"
["x-powered-by"]=>
string(7) "ASP.NET"
["date"]=>
string(29) "Fri, 14 Jun 2013 12:00:35 GMT"
["connection"]=>
string(5) "close"
}
["code"]=>
string(3) "200"
}
So will now implement the suggestion below ...