0

I'm new to Drupal. What I'd like to do is create a form on a new page, having full control over the css/html of the form, and post the form to PHP for processing. The PHP should do some custom things to the form data, dump some stuff to the DB, return some stuff from the DB, and either redirect to a new page or return an error. No need to go into details about the PHP/DB stuff - just need to figure out how to go about creating a new page, and where to post the form to for processing. Should be able to figure everything else out from there!

  • Welcome to Drupal Answers. This is not the way Drupal actually works. You cannot code an HTML form into a content type. Would you edit and explain what you actually trying to achieve with this form? – Елин Й. Aug 28 '13 at 20:37
  • Hi Елин Й., I've tried to explain my general goal. – valen Aug 28 '13 at 20:46
1

To create a form on a page you need to use Drupal's Form API. It seems a bit complex at the first sight, but it isn't as it seems.

The steps you need to follow:

  1. Create a custom module
  2. Implement hook_menu() in your module and set a path for your page which should render the form.
  3. A form builder function that builds a form using the Form API.

An Example for hook_menu() implementation:

function YOURMODULE_menu() {
  $items['some/path'] = array(
    'title' => 'My page title',
    'description' => 'An example page',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('YOURMODULE_example_form'),
  );
  return $items;
}

This code registers a paths as http://example.com/some/path and calls drupal_get_form() function, which gets a form with the given id, which in this example is YOURMODULE_example_form.

An example for the form builder function:

function YOURMODULE_example_form($form, &$form_state) {
  $form['some_text_field'] = array(
    '#title' => t('Enter some text'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );

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

  return $form;
}
| improve this answer | |
  • In terms of rendering the form - the form needs to have very custom HTML & CSS. Is this possible? – valen Aug 28 '13 at 21:16
  • Indeed you may have to implement some theme functions as well to get an exact HTML structure of the form. But the actual HTML of Drupal forms are not known as the cleanest ones. – Елин Й. Aug 28 '13 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.