开发者

Drupal - Create a form

开发者 https://www.devze.com 2023-02-24 08:52 出处:网络
How do I create a form in drupal with about 5 text fields, allowing the user to enter in data, then click the submit button a开发者_JAVA百科nd get it to save all the data into a mysql database table?I

How do I create a form in drupal with about 5 text fields, allowing the user to enter in data, then click the submit button a开发者_JAVA百科nd get it to save all the data into a mysql database table?


I strongly invite you to read this page about the form API, which is a powerful tool of Drupal to manage forms.


Well, as you can see from the other responses to this question, there's a lot of different ways to do this. The question becomes, what do you want to do with the data once it's in mysql.

1) If you want to have the data stored somehow in mysql, and the data is going to be reviewed by someone but doesn't need to be displayed to users in general, then webform is probably the way to go. It allows you to easily create a form which people can enter data into, and which a privileged user can then review the results either online or by downloading the responses in a file. Webform is particularly good for setting up surveys and other forms where you only want each user to fill it out once.

2) If you want people to answer the questions in the form, and then display the results as data on your website, and especially if you want to display the results in views, then creating a node type with cck, and allowing folks to create nodes with their answers. It's a little more tricky making sure each user creates a node once, but once they are created they can be displayed in a number of different ways with existing tools.

3) If you need the form output to go into a specific table that you've created or which has been given to you, then it's probably best to buckle down and learn the Form API and build the code yourself. Webform and CCK create tables for their own use which doesn't work if you're using an existing system.


You can use Drupal webform module.

If you want to make a custom form you can create your own module. This link will help you.

http://drupallearn.blogspot.com/p/forms-in-drupal.html


You may try the webform module also. It pretty much serves your need


if you just need a few fields you can use the cck module and create content that has these fields, set permissions for all users create the content. With the views module can create a list of all creation.

cck

Views


Although a lot of the answers suggest using the webform module, coding your own forms is not hard and saves you installing a mkodule you don't need. (Less code and fewer tables in your DB is a good thing.)

To create a form in drupal with about 5 text fields, allowing the user to enter in data, then click the submit button and get it to save all the data into a mysql database table:

Assuming mymodule is the name of your module ...

// shows the form
mymodule_myform($form, &$form_state) {
    for ($i = 0; $i < 5; $i++) {
        $form["field_$i"] = array(
            '#type' => 'textfield',
            '#title' => t('textfield @i', array('@i' => $i))
        );
    }

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

// validate input
mymodule_myform_validate($form, &$form_state) {
}

// save the input to the database
mymodule_myform_submit($form, &$form_state) {
    $values = array();
    for ($i = 0; $i < 5; $i++) {
        $values("field_$i_value" = strip_tags($form_state['values']['field_$i']));
    }
    flat_val = implode(',', $values);
    $sql = "INSERT INTO {table} (field_0_value, field_1_value, field_2_value, field_3_value, field_4_value) VALUES (%s)"
    $res = db_query($sql, $flat_val);
    drupal_set_message("Thank you for your submission.", 'status');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消