How can I set the default value based on the submitted values? - drupal-7

The purpose of the page is to show a list of jobs. The form at the top lets you filter the results. Once the form is submitted I want to show the form again as well as the values that were put in. How would I go about this in drupal. The form code is below.
function ac_resume_job_list_form($form, &$form_state)
{
$form['display_options'] = array(
'#type' => 'fieldset',
'#title' => 'Display Options',
'#attributes' => array("style" => "width:250px"),
);
$form['display_options']['limit'] = array(
'#type' => 'textfield',
'#title' => 'Limit',
'#size' => 2,
'#default_value' => 'the value of the submitted form',
);
$form['display_options']['submit'] = array(
'#type' => 'submit',
'#value' => 'Change Display',
);
return $form;
}

You'll want to set $form_state['rebuild'] = TRUE in the form submit handler, and basically you'll have access to all of the submitted values in the $form_state variable in the form when it's rebuilt:
$default_limit = isset($form_state['values']['limit']) ? $form_state['values']['limit'] : 0; // Or whatever default limit you want to have.
$form['display_options']['limit'] = array(
'#type' => 'textfield',
'#title' => 'Limit',
'#size' => 2,
'#default_value' => $default_limit
);
// Brief example for the submit form
function my_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}

Related

Not able to make submit button work in form theme in drupal 7

I am trying to develop a module where in my form needs to be rendered in a tabular form. Everything is working fine except the submit button. It is neither calling default _validation or _submit functions nor _form_alter functions. I don't know where I am missing.
Here is the code for my .module file
<?php
function mytheme_menu(){
$items = array();
$items['enqform']=array(
'title' => 'Enquiry Form',
'page callback' => 'mytheme_function',
'access arguments'=>array('access content'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function mytheme_function(){
return theme('enquiry')
}
function mytheme_theme() {
return array(
'enquiry' => array(
'render element' => 'form',
'template' => 'custompage',
),
);
}
function enquiry_form($form, &$form_state){
$form['efname'] = array(
'#type' => 'textfield',
'#title' => 'First Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE,
);
$form['elname'] = array(
'#type' => 'textfield',
'#title' => 'Last Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function enquiry_form_validate($form, &$form_state){
}
function enquiry_form_submit($form, &$form_state) {
db_insert('form_example')->fields(array(
'efname'=>$form_state['values']['efname'],
'elname'=>$form_state['values']['elname'],
'active' => 1,
)
)->execute();
}
and my .tpl.php page looks like this
<?php$form = drupal_get_form('enquiry_form');?>
<h2>Please enter details here</h2>
<?php print render(drupal_render($form['efname']));?>
<?php print render(drupal_render($form['submit']));?>
<?php print render(drupal_render_children($form));?>
When I use 'drupal_get_form' as page callback in my hook menu, submit is working fine, but the purpose of template is of no use. It would be a day saver if someone can guide me where and what am I missing in this code.
I think that you should not render the drupal_render_children, you should just print drupal_render_children by itself.
<?php print drupal_render_children($form); ?>

Drupal7 Radio button value to datasabe is not working

Adding textbox ,radiobuttons and submit button is working fine..
enter code here:
' $form = array();
$form['name'] = array(
'#title' => 'Your Name',
'#type' => 'textfield',
'#size' => '30',
);
$active = array(0 => t('Blog-Url'), 1 => t('Twitter-Name'));
$form['settings']['active'] = array(
'#type' => 'radios',
'#title' => t('Enter nature of the name'),
'#default_value' => isset($node->active) ? $node->active : 1,
'#options' => $active,
'#description' => t('Enter nature of the name, and characteritics.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;'
// When Submit button is clicked the action take place and insert into database not working..
` function sform_add_form_submit($form,$form_state){
$name = trim($form_state['values']['name']);
$formwem= $form_state['values']['settings']['active'];
$query = "INSERT INTO twittername (`name`,'Blogurt','Twitter_name') VALUES ('%s','%s','%s')";
$result = db_query($query, $name, $formwem ); `
Please help me ....
Try re-writing your query to:
$name = trim($form_state['values']['name']);
$formwem = $form_state['values']['settings']['active'];
$result = db_query("INSERT INTO twittername (name, Blogurt, Twitter_name) VALUES (':name',':blogurt',':twitter_name')", array(':name' => $name, ':blogurt' => $formwem, ':twitter_name' => $formwem));
Though I'm not sure which form field populates the two columns Blogurt and Twitter_name. Can you please explain?

Add form Drupal Error Page

I am working on Drupal 7. I want to display a form on "Error! The website encountered an unexpected error. Please try again later." page.
I have copied the maintenance-page.tpl.php file to my theme folder. and rendered a form in that template. Up until now the form is showing but when I submit the form, drupal is not getting into _validate and _submit functions.
Here is the code
Template File
<div class="complaint-form-message">
<h5>
We’re sorry you’re having trouble with myCCS!
Tell us about the error you’ve experienced and we’ll
get right back to you!
</h5>
</div>
<div class="complaint-form">
<?php echo drupal_render(drupal_get_form('complaint_form_form')); ?>
</div>
Form Function
function complaint_form_form($form, &$form_state){
$form['name'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => 'First Name & Last Name',
);
$form['email'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => 'Email Address',
);
$form['browser'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => 'What browser you are using?',
);
$form['tell_us_about_error'] = array(
'#type' => 'textarea',
'#required' => TRUE,
'#title' => 'Tell us about error you experienced?',
);
$form['attachment'] = array(
'#type' => 'file',
'#title' => 'Attachment',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => 'complaint_form_submit',
);
return $form;
}
function complaint_form_validate($form, $form_state){
echo 'PPPPP<pre>'; print_r($form_state); die;
$mail = $form_state['values']['submitted_tree']['email'];
if (!valid_email_address($mail))
form_set_error('[submitted][email_address]', t('The email
address appears to be invalid.'));
}
function complaint_form_submit($form, $form_state){
echo 'DDDDD<pre>'; print_r($form_state); die;
$values = $form_state['values'];
}
Your #submit is an array you have put is as string,
So change your submit entry to the following.
and place $form['submit'] to $form['actions']['submit'];
$form['actions']['submit']=array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('complaint_form_submit'),
);
Note: **'#submit' is an array**
to add validations
$form['#validate'][] = 'complaint_form_validate';
Note: '#validate' is an array

drupal 7 ajax form rebuild form to change form_state value

I have one drupal form, i want it works as "add one more": there is one button at the end of the form, when click it, another form will append to it. Here is part of my code:
function add_passenger_form($form, &$form_state){
if(!isset($form_state['num_names'])){
$form_state['num_names']=2;
}
$form['add_button']= array(
'#type' => 'button',
'#value'=> 'Add more',
'#ajax' => array(
'callback' => 'ajax_add_passenger_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'last_field',
),
);
$form['form_number']=array(
'#prefix'=>'<div class="form_number">',
'#suffix'=>'</div>',
'#type'=>'hidden',
'#value'=>2,
);
}
function ajax_add_passenger_callback($form, &$form_state){
$form_state['num_names']++;
$form_state['rebuild']=TRUE;
$form['form_number']['#value']=$form_state['num_names'];
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace(".form_number", render($form['form_number'])),
)
);
}
I want the $form['num_names']['#value'] increase one when i click the add one button. Now it only works once (to 3).
Change your code like this.. this will create button "Add more" to add new passenger and one field (name) for each passenger. You can replace that "Name" field for example with fieldset and add more fields for one passenger.
function add_passenger_form($form, &$form_state){
if(!isset($form_state['num_names'])){
$form_state['num_names']=2;
}
// Create wrapper for ajax
$form['#prefix'] = '<div id="form-wrapper">';
$form['#suffix'] = '</div>';
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['name_' . $i] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => true
);
}
$form['add_button']= array(
'#type' => 'button',
'#value'=> 'Add more',
'#ajax' => array(
'callback' => 'ajax_add_passenger_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'form-wrapper',
),
);
}
function ajax_add_passenger_callback($form, &$form_state){
$form_state['num_names']++;
// rebuild whole form with new values
$form_state['rebuild'] = true;
}
Also check this link for more info about ajax:
https://api.drupal.org/api/examples/ajax_example!ajax_example_graceful_degradation.inc/7
function add_passenger_form($form, &$form_state){
$form['#tree'] = TRUE;
if(empty($form_state['num_names'])){
$form_state['num_names'] = 2;
}
$form['passenger_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('List of Passengers'),
'#prefix' => '<div id="passenger-form-wrapper">',
'#suffix' => '</div>',
);
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['passenger_fieldset']['passenger_name'][$i] = array(
'#type' => 'textfield',
'#title' => t('Passenger Name'),
'#required' => false,
);
}
$form['passenger_fieldset']['add_button']= array(
'#type' => 'submit',
'#value'=> t('Add passenger'),
'#submit' => array('add_passenger_form_add_one'),
'#ajax' => array(
'callback' => 'add_passenger_form_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'passenger-form-wrapper',
),
);
// Remove passenger
if ($form_state['num_names'] > 2) {
$form['passenger_fieldset']['remove_button'] = array(
'#type' => 'submit',
'#value' => t('Remove Passenger'),
'#submit' => array('remove_passenger_form_remove_one'),
'#ajax' => array(
'callback' => 'add_passenger_form_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'passenger-form-wrapper',
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/*
* add a textfield one more
*/
function add_passenger_form_add_one($form, &$form_state) {
$form_state['num_names']++;
// rebuild whole form with new values
$form_state['rebuild'] = true;
}
function remove_passenger_form_remove_one($form, &$form_state) {
if ($form_state['num_names'] > 2) {
$form_state['num_names']--;
}
$form_state['rebuild'] = TRUE;
}
function add_passenger_form_callback($form, $form_state){
return $form['passenger_fieldset'];
}

Drupal 7 Forms API - AJAX Forms error: An illegal choice has been detected. Please contact the site administrator

I have a form set up where a users selects an item from a drop down. Once that item is selected another drop down is populated. Then depending on the value that is selected from the second drop down a fieldset may or may not be shown. If the field set is shown there is a field and a button. By clicking the button you add another copy of the same field. Once there is more than one a remove one button shows up as well. I got the basis for the code from here:
http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7
The problem is when I click 'Add Another Survey Question' the first time it works fine and add a field. When I click it a second time or when I click remove now that there are two, I get this error: 'An illegal choice has been detected. Please contact the site administrator.'
What am I doing wrong?
Here's my code:
function touchpoints_addmetric_form($form, &$form_state, $tp_id) {
$selectedType = isset($form_state['values']['type']) ? $form_state['values']['type'] : FALSE;
$types = db_query('SELECT * FROM {touchpoints_metric_types}') -> fetchAllKeyed(0, 1);
$form['#tree'] = TRUE;
$form['type'] = array(
'#type' => 'select',
'#title' => t('Metric Type'),
'#options' => $types,
'#required'=>TRUE,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'method-wrapper',
'callback' => 'touchpoints_method_callback'
)
);
$form['measurementmethod'] = array(
'#type' => 'select',
'#title' => t('Measurement Method'),
'#required'=>TRUE,
'#prefix' => '<div id="method-wrapper">',
'#suffix' => '</div>',
'#options' => _get_methods($selectedType)
);
$form['survey'] = array('
#type' => 'fieldset',
'#collapsible' => FALSE,
'#states' => array(
'visible' => array(
array(
array(':input[name="measurementmethod"]' => array('value' => '5')),
'xor',
array(':input[name="measurementmethod"]' => array('value' => '6')),
'xor',
array(':input[name="measurementmethod"]' => array('value' => '7'))
)
)
)
);
$form['survey']['contents'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#prefix' => '<div id="survey-div">',
'#suffix' => '</div>',
);
if (empty($form_state['num_surveys'])) {
$form_state['num_surveys'] = 1;
}
for ($i = 1; $i <= $form_state['num_surveys']; $i++) {
$form['survey']['contents']['survey_question'][$i] = array(
'#type' => 'textfield',
'#title' => t('Survey Question ' . $i),
'#size' => 70, '#maxlength' => 100,
);
}
$form['survey']['contents']['addsurvey'] = array(
'#type' => 'submit',
'#value' => t('Add Another Survey Question'),
'#submit' => array('touchpoints_metrics_survey_add_one'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'touchpoints_metrics_survey_callback',
'wrapper' => 'survey-div',
),
);
if ($form_state['num_surveys'] > 1) {
$form['survey']['contents']['removesurvey'] = array(
'#type' => 'submit',
'#value' => t('Remove A Survey Question'),
'#submit' => array('touchpoints_metrics_survey_remove_one'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'touchpoints_metrics_survey_callback',
'wrapper' => 'survey-div',
),
);
}
return $form;
}
function _get_methods($selected) {
if ($selected) {
$methods = db_query("SELECT * FROM {touchpoints_m_methods} WHERE mt_id=$selected") -> fetchAllKeyed(0, 2);
} else {
$methods = array();
}
return $methods;
}
function touchpoints_method_callback($form, &$form_state) {
return $form['measurementmethod'];
}
function touchpoints_metrics_survey_add_one($form, &$form_state) {
$form_state['num_surveys']++;
$form_state['rebuild'] = TRUE;
}
function touchpoints_metrics_survey_remove_one($form, &$form_state) {
if ($form_state['num_surveys'] > 1) {
$form_state['num_surveys']--;
}
$form_state['rebuild'] = TRUE;
}
function touchpoints_metrics_survey_callback($form, &$form_state) {
return $form['survey']['contents'];
}
I have encountered this error a few times, and this is what I found:
This is Drupal's way of saying "Hey, you tried to submit this form with a , checkbox or radio button option that wasn't included in the original form definition! That's not allowed." - See more at: http://proofgroup.com/blog/2008/jul/debugging_mysterious_illegal_choice_has_been_detected_please_contact_site_administrato#sthash.vDNmqslL.dpuf
Instead of custom code, maybe you should consider module Conditional Fields
Or a work around: '#validate' => TRUE source
Actually, it's '#validated' => TRUE.

Resources