I am having three fields: First name, Last name and Year of birth. In code I am easily inserting into database when I press submit button, so I want to update these fields when I want to click on Edit button. Please anyone help me to code this update to put the inserted data in a database or please post code.
<?php
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => 'My form',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_my_form'),
'access arguments' => array('access content'),
'description' => 'My form',
'type' => MENU_CALLBACK,
);
$items['my_module/edit'] = array(
'title' => t('Edit Name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_edit'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_my_form($form_state) {
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE, // Added
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE, // Added
);
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => ('Year of birth'),
'#description' => 'Format is "YYYY"',
);
// Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
function my_module_edit($form, &$form_submit){
$form['Edit'] = array(
'#type' => 'Edit',
'#value' => 'Edit',
);
return $form;
}
function my_module_my_form_submit($form, &$form_state){
$firstName = $form_state['values']['fname'];
$lastName=$form_state['value']['lname'];
$yearofbirth = $form_state['values']['yearOfbirth'];
$query ="INSERT INTO `slideshow`.`mymoduledb`(`first_name`,`last_name`,`year_of_birth`) VALUES ('{$firstName}','{$lastName}','{$yearofbirth}')";
//$result=db_query($query);
if ($success = db_query($query)) {
// Tell the user that the employee has been saved.
drupal_set_message(t(' has been saved.'));
}else{ // If there's an error, $success will evaluate to FALSE, and the following code will execute.
drupal_set_message(t('There was an error saving your data. Please try again.'));
}
}
}
Inorder to edit form field you can reuse the form and set the default value field '#default_value'
storing the form submitted data fetched from database using the respective uniques ids.Refer the link for sample code.
https://www.drupal.org/node/2069383
Related
I created custom form in drupal 7 and data is saved in a table called "person" in database when i am trying to retrieve data in custom url data is displaying but when loading the form page "page not found" message is displaying not getting that form, if the code for retrieve data in custom url is commented then i get form page.
this is the code which i used:
<?php
/**
* #file
* Provides a custom form, data are saved in database and can retrieve data in table format in custom url.
*/
//Implementation of hook_menu()
function form_test_menu() {
$items['formtest'] = array(
'title' => 'Form Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_form'),
'access callback' => TRUE,
);
$items = array();
$items['results'] = array( // change to the url you want
'title' => 'results',
'type' => MENU_CALLBACK,
'page callback' => 'results',
'access arguments' => array('access content'),
);
return $items;
}
//function to describe field in the form
function form_test_form($form,&$form_submit) {
$form['name'] = array(
'#title' => t('name'),
'#type' => 'textfield',
'#size' => 20,
'#maxlength' => 20,
'#required' => TRUE,
'#default_value' => 'Enter Your Name',//make this field required
);
$form['address']['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#required' => TRUE,
'#maxlength' => 255,
);
$form['price'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'What is Your Price?',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['attending'] = array(
'#type' => 'radios',
'#title' => t('Will you be attending?'),
'#options' => array(
'1' => t('Yes'),
'0' => t('No'),
),
'#required' => TRUE,
'#default_value' => isset($attending) ? $attending : NULL,
);
$form['submit'] = array(
'#value' => 'Submit',
'#type' => 'submit',
);
return $form;
}
//validation code for integer
function form_test_form_validate($form, &$form_state) {
if (!($form_state['values']['price'] > 0)){
form_set_error('price', t('Price must be a positive number.'));
}
}
//inserting data into database
drupal_write_record('person', $data);
function form_test_form_submit($form, &$form_state) {
$id = db_insert('person')
->fields(array(
'name' => $form_state['values']['name'],
'email' => $form_state['values']['email'],
'price' => $form_state['values']['price'],
'attending' => $form_state['values']['attending'],
// 'ios' => $form_state['values']['ios'],
))
->execute();
drupal_set_message(t('data saved successfully.'));
}
//Retrieving data from database
function results() {
print "CUSTOM TABLE CONTENT";
print "<br /> ";
print "<br /> ";
$query = db_select('person', 'u');
$query->fields('u', array('name')); //mention the field that you want to display
$query->fields('u', array('email'));
$query->fields('u', array('price'));
$query->fields('u', array('attending'));
$result = $query->execute();
while($record = $result->fetchAssoc()) { //while loop mention how the data want to display
echo "<li>";
print_r($record['name']);
print " ";
print_r($record['email']);
print " ";
print_r($record['price']);
print " ";
print_r($record['attending']);
echo "</li>";
}
}
$items = array();
this code should be come under the main function
function form_test_menu() {
the code should be like this
function form_test_menu() {
$items = array();
no other modification it will work.
I am developing a custom module by name 'form' in drupal 7.
I have placed my module folder in sites\all\modules\form.
And i have 2 files in 'form' folder. 'form.info' and 'form.module'.
'form.info'
name = Form
description = Form creation.
core = 7.x
And 'form.module' contains
<?php
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
)
return $form;
}
I am getting only blank white page when i click on the link.
Thanks in advance.
I works for me try this code for mymodule.module
<?php
// $Id$
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
When you create mymodule.info file
name = Form
description = Form creation.
core = 7.x
package = Form
Add package to it so you have your module in his own box. It is easier to find it betwen all of those modules.
Rename your module to form1, as it is conflicting with Drupal internal form module, located in /includes.
You forgot a semicolon here:
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
); //<- semicolon missing
I am a newbie to drupal. I am working to create a custom module in the name "course". I have read about the list of hooks to create a custom module. So, what's my problem is that i cant able to see the edit form for the module configure but i can do add management and its working fine.
I've used Existing contact modules as a reference.
Below are my code:
In course.module
function course_menu() {
$items['admin/structure/course'] = array(
'title' => 'Academy\'s courses',
'description' => 'Create a system contact form and set up categories for the form to use.',
'page callback' => 'course_list',
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
$items['admin/structure/course/add'] = array(
'title' => 'Add Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form'),
'access arguments' => array('administer contact forms'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'course.admin.inc',
);
$items['admin/structure/course/edit/%course'] = array(
'title' => 'Edit Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form',4),
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
return $items;
}
and in course.admin.inc:
function course_edit_form($form, &$form_state, array $course = array()) {
// If this is a new course, add the default values.
$course += array(
'name' => '',
'course_id' => NULL,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Course Name'),
'#maxlength' => 255,
'#default_value' => $course['name'],
'#description' => t("Example: 'Available Course Names'."),
'#required' => TRUE,
);
$form['course_id'] = array(
'#type' => 'value',
'#value' => $course['course_id'],
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function course_edit_form_submit($form, &$form_state) {
if (empty($form_state['values']['course_id'])) {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->execute();
//drupal_write_record('courses', $form_state['values']);
}
else {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->condition('course_id', $form_state['values']['course_id'])
->execute();
//drupal_write_record('courses', $form_state['values'], array('course_id'));
}
drupal_set_message(t('Course %name has been saved.', array('%name' => $form_state['values']['name'])));
watchdog('contact', 'Course %name has been saved.', array('%course' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/course/edit/' . $form_state['values']['course_id']));
$form_state['redirect'] = 'admin/structure/course';
}
I can't able to find the exact problem, why add is working but not edit & delete? Help me on this.
Below image tells you what exactly i am asking for(Red mark).
If anything additional needed in this please let me know.
At A guess I'd say the edit route is not matching because of an issue with the course load function (not listed). Try dumping the result of that method. Is the course entity defined in your module? If so, then the convention is to prefix the variable name with the module name, and implement the appropriately names hook.
There's a better description of how this should work here: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7
Jump to the "Auto-Loader Wildcards" section.
Try to clear cache /admin/config/development/performance
I'd like to make a drupal page with a form. Something like the following, which doesn't render:
function score_table_menu() {
$items['table'] = array(
'title' => t('name'),
'page arguments' => array('table_page'),
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
return $items;
}
function table_page(){
$output .= t('Complicated Hello');
$header = stuff;
$rows = stuff;
$output .= theme_table($header, $rows);
return $output;
}
function table_form(){
$stuff_array = array (values);
$form['choice']= array(
'#type' => 'select',
'#title' => t('Select Stuff'),
'#options' => $stuff_array,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function table_form_validate(){}
function table_form_submit(){
drupal_set_message(t('Submitted'));
}
Is it possible to have half the module render a page and the other half a form? I've written out two page arguments. I don't see any difference between page argument and callback. Also might need to use drupal_get_form() to render the form on the page.
page callback
is the function that you want this URI to trigger.
page arguments
are any arguments that you want to sent to that function when the callback is called.
Your menu item should look like this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'table_page',
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
Or this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
In your table_form, you can use other form types to display markup,
$form['table'] = array(
'#type' => 'markup',
'#markup' => theme_table($header, $rows),
);
Take a look in the API for a better understanding of hook_menu: http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_menu/6
I am setting up a configuration page for a new module and I am having trouble getting my textfield to remember data I previously entered.
I am setting #default_value by calling variable_get but the value never gets reset in the textfield. It is however correctly stored as a variable and visible on the variable editor page.
Below is a code fragment:
$form = array();
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
return system_settings_form($form);
How can I fix this?
Updated code
The submit function is as follows:
function mymodule_settings_submit($form, &$form_state){
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
variable_set( 'username', $form_state['values']['username'] );
}
Updated code #2
The full code:
function my_module_init(){
}
function my_module_menu(){
$items = array();
$items['admin/settings/helper'] = array(
'title' => 'helper',
'page callback' => 'drupal_get_form',
'page arguments' => array('helper_setup'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function helper_setup(){
$node_types = node_get_types('names');
$form = array();
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
$form['#submit'][] = 'my_module_settings_submit';
return system_settings_form($form);
}
function my_module_validate($form, &$form_state){
die('validation area responding!');
}
function my_module_settings_submit($form, &$form_state){
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
variable_set( 'username', $form_state['values']['username'] );
}
in your form submit function you should insert variable_set('variableName' , 'value') variables is not inserted automatically..
UPDATE:
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
$form['#submit'][] = '_myform_submit_function' ;
return system_settings_form($form);
now lets build your submit function
function _myform_submit_function($form,&$form_state ) {
variable_set('username', $form_state['values']['username']) ;
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
}
now clear the cache and tell me if its working or not