0
votes

I am looking for a way to save the data collected by a form (this form is generated from the module i'm working on). I would like to save the data into a content type I also created. Is this possible? I have been looking for a way to do this and have not found any simple ways of doing it. I am currently working on drupal 7.

Here is my code so far :

function drupalform_menu() {
$items['drupalform/form1'] = array(
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
    'page callback' => 'drupal_get_form',
    'page arguments'=>array('drupalform_form1'));

  return $items;
}

function drupalform_form1() {
$form = array();


$form['Civilite']=array(
    '#type'=>'select',
    '#title'=>t('Entrez votre civilite'),
    '#options'=>array('M','Mr','Mme')
  );
$form['nom']=array(
    '#type'=>'textfield',
    '#title'=>t('Entez votre nom'),
  );

 $form['email']=array(
    '#type'=>'textfield',
    '#title'=>t('Enter votre e-mail'),
  );

$form['submit']=array(
    '#type'=>'submit',
    '#value'=>t('Submit')
  );

  return $form;
}

function drupalform_form1_validate($form, $form_state) {
  if(empty($form_state['values']['nom']))
     form_set_error('nom','Veuillez saisir votre nom');
  else if(filter_var($form_state['values']['email'], FILTER_VALIDATE_EMAIL) == false)
    form_set_error('email','Mail non valide');
}

function drupalform_form1_submit($form, $form_state) {
   drupal_set_message("Le formulaire est envoyé");
   drupal_goto();
}
1

1 Answers

0
votes

You can create node by assigning values like below,

global $user;
$newNode = new stdClass();
$newNode->type = 'NODE_TYPE';
$newNode->title = 'NODE_TITLE'
$newNode->uid = $use->uid;
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;

// add CCK field data
$newNode->field_YOUR_CUSTOM_FIELD_NAME_1[0]['value'] = 'FORM_VALUE_1';
$newNode->field_YOUR_CUSTOM_FIELD_NAME_2[0]['value'] = 'FORM_VALUE_2';
// save node
node_save($newNode);

You can give this in your form submit.