I am ready to pull my hair out! My form displays perfectly fine and I can enter data into the form and when I submit, I don't get any error messages at all. However, I use phpmyadmin to view the table to verify that the data from my form was in fact written to the table. The table remains EmPTY!!! This happens for both IE browser and Firefox browsers which do have javascript enabled. Is my db_insert() function not correct???? If it is something with my table, where do I look to debug???
<?php
/**
*CREATE NEW People RECORD
* This function defines the URL to the page created etc.
*/
function trp_people_menu() {
$items = array();
$items['trp_people/form'] = array(
'title' => t('TRP-PMS Create New Record in People Module'),
'page callback' => 'trp_people_form',
'access arguments' => array('access content'),
'description' => t('Create New People Record'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* This function gets called in the browser address bar for:
* "http://yourhost/new_people_db/form" or
* "http://yourhost/?q=new_people_db/form". It will generate
* a page with this form on it.
*/
function trp_people_form() {
// This form calls the form builder function via the
// drupal_get_form() function which takes the name of this form builder
// function as an argument. It returns the results to display the form.
return drupal_get_form('trp_people_record_form');
}
/**
* This function is called the "form builder". It builds the form.
* Notice, it takes one argument, the $form_state
*/
function trp_people_record_form($form_state) {
// This is the first form element. It's a textfield with a label, "Role"
$form['role'] = array(
'#type' => 'select',
'#title' => 'Role',
'#options' => array(0 => 'Select One', 1 => 'Appraiser', 2 => 'Customer', 3 => 'General Contractor', 4 => 'HomeOwner', 5 => 'Project Owner', 6 => 'Regional Manager'),
'#default_value' => 0,
'#required' => TRUE,
);
$form['company_name'] = array(
'#type' => 'textfield',
'#title' => t('Company Name'),
'#maxlength' => 30,
'#required' => TRUE,
'#size' => 30,
'#states' => array(
'visible' => array(
':input[name="role"]' => array('value' => 1),
),
),
);
$form['address'] = array(
'#type' => 'textfield',
'#title' => t('Street Address'),
'#maxlength' => 40,
'#required' => TRUE,
);
$form['address2'] = array(
'#type' => 'textfield',
'#title' => t('Street Address2'),
'#maxlength' => 40,
'#required' => FALSE,
);
$form['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
'#required' => TRUE,
);
$form['state'] = array(
'#type' => 'select',
'#title' => 'State',
'#options' => array(0 => 'Select', 1 => 'Alabama', 2 => 'Alaska', 3 => 'Arkansas', 4 => 'California'),
'#default_value' => 0,
'#required' => TRUE,
);
$form['zipcode'] = array(
'#type' => 'textfield',
'#title' => t('ZipCode'),
'#maxlength' => 8,
'#size' => 8,
'#required' => TRUE,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Email Address'),
'#maxlength' => 20,
'#size' => 20,
);
$form['telephone'] = array(
'#type' => 'textfield',
'#title' => t('Company Telephone Number'),
'#maxlength' => 10,
'#size' => 10,
'#required' => TRUE,
'#states' => array(
'visible' => array(
':input[name="role"]' => array('value' => 1),
),
),
);
$form['fax'] = array(
'#type' => 'textfield',
'#title' => t('FAX Number'),
'#maxlength' => 10,
'#size' => 10,
'#states' => array(
'visible' => array(
':input[name="role"]' => array('value' => 1),
),
),
);
$form['contactname'] = array(
'#type' => 'textfield',
'#title' => t('Contact Name'),
'#maxlength' => 32,
'#size' => 40,
'#states' => array(
'visible' => array(
':input[name="role"]' => array('value' => 1),
),
),
);
$form['contactphone'] = array(
'#type' => 'textfield',
'#title' => t('Contact Telephone Number'),
'#maxlength' => 10,
'#size' => 10,
);
$form['ContactMobilephone'] = array(
'#type' => 'textfield',
'#title' => t('Company Mobile phone Number'),
'#maxlength' => 10,
'#size' => 10,
);
$form['contactemail'] = array(
'#type' => 'textfield',
'#title' => t('Contact Email Address'),
'#maxlength' => 20,
'#size' => 20,
);
$form['contact2name'] = array(
'#type' => 'textfield',
'#title' => t('Second Contact Name'),
'#maxlength' => 32,
'#required' => TRUE,
'#size' => 40,
);
$form['contact2phone'] = array(
'#type' => 'textfield',
'#title' => t('Second Contact Telephone Number'),
'#maxlength' => 10,
'#size' => 10,
);
$form['region'] = array(
'#type' => 'select',
'#options' => array(0 => 'Choose', 1 => 'West', 2 => 'East', 3 => 'South', 4 => 'North'),
'#default_value' => 0,
'#title' => t('Region'),
'#required' => TRUE,
// This #states rule says that the "region" fieldset should only
// be shown if the "role" form element is set to "Appraiser".
'#states' => array(
'visible' => array(
':input[name="role"]' => array('value' => 1),
),
),
);
$form['organization'] = array(
'#type' => 'select',
'#options' => array(0 => 'Choose', 1 => 'Commercial', 2 => 'Independent'),
'#default_value' => 0,
'#title' => t('Organization'),
// This #states rule says that the "organization" fieldset should only
// be shown if the "role" form element is set to "General Contractor".
'#states' => array(
'visible' => array(
':input[name="role"]' => array('value' => 3),
),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function trp_people_form_submit($form, &$form_state) {
// Save the data from the $form_state into variables to make the database insert query simpler
$role =$form_state['values']['role']; //$roles is the variable name; 'role' is form field name from the function above.
$company = $form_state['values']['company_name']; //
$contactname = $form_state['values']['contactname']; // $contactname is the variable name; 'contactname' is form field name from the form function above.
$contactphone =$form_state['values']['contactphone']; //
$contactMobilephone = $form_state['values']['contactMobilephone']; //
$email = $form_state['values']['email']; //
$address = $form_state['values']['address']; //
$address2 = $form_state['values']['address2']; //
$city = $form_state['values']['city']; //
$state = $form_state['values']['state']; //
$zipcode = $form_state['values']['zipcode']; //
$contact2name = $form_state['values']['contact2name']; //
$contactemail = $form_state['values']['contactemail']; //
$telephone = $form_state['values']['telephone']; //
$fax = $form_state['values']['fax']; //
// Insert Data into Tables Drupal 7 version
$index = db_insert('pms_appraisers')
->fields(array(
'cname' => $company,
'address' => $address,
'address2' => $address2,
'city' => $city,
'state' => $state,
'zip' => $zipcode,
'fax' => $fax,
'contactMobilephone' => $contactMobilephone,
'contactphone' => $telephone,
'email' => $email,
'contactEmail' => $contactemail,
'contact2name' => $contact2name,
'contact2phone' => $contactphone,
))
->execute();
}
Comments
Did you manage to come right?
I'm trying a similar thing on the submit of my custom form, trying to save the data into the D7 db.
BUT NO LUCK!
HOWEVER, If I use a basic db insert:
$nid = db_insert('node')
->fields(array(
'title' => 'Example Event',
'uid' => 1,
'created' => REQUEST_TIME,
))
->execute();
The form will create a new node titled "Example Event"...
BUT... I want my own content type, eg. "calendar_event" instead of "node", I get no love, or joy or anything...
ANY IDEAS? :- )
---
Almost there!