Skip to main content
edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Tweeted twitter.com/#!/StackDrupal/status/79069423150964736
deleted 123 characters in body; edited title
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Drupal 7: show Show the value of a node-form field inside an AJAX field added via hook_form_alter()

I am stuck with a probably very simple problem. I have created a simple content type "reservation" with an integer field called "field_km" and ofwith a default value of 50. 
On the node creation form, I want to have a markup field showing the value of the "field_km" field. So, I have created a simple myresa module, with the following code  :

/**
 * @file
 * Drupal Module : myresa
 * Adds custom code specific to this Drupal 7 site.
 */

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
  $form[‘box’] = array(
    ‘#type’ => ‘markup’,
    ‘#prefix’ => ‘’,
    ‘#suffix’ => ‘’,
    ‘#markup’ => ‘Price not computed',
  );

  $form[‘mybtn’] = array(
    ‘#type’ => ‘button’,
    ‘#id’ => ‘mybtn’,
    ‘#ajax’ => array(
      ‘callback’ => ‘myresa_callback’,
      ‘wrapper’ => ‘box’,
      ‘name’ => ‘mybtn1’,
    ),
    ‘#value’ => t(‘Compute the price’),
  );
}

function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#markup’] = «The price for » . $form_element[‘field_km’][‘#default_value’] . » km is 35 euros» ;
  return $element ;
}

The form shows the 'box' markup, the button, and when clicked the button activates the callback and shows the wanted text, but not the value of 'field_km'. It is probably a simple syntax mistake, but it beats me.

Thank you very much in advance for sparing 30 seconds to help ;-))

Drupal 7: show value of a node-form field inside an AJAX field added via hook_form_alter

I am stuck with a probably very simple problem. I have created a simple content type "reservation" with an integer field called "field_km" and of default value 50. On the node creation form, I want to have a markup field showing the value of the "field_km" field. So, I have created a simple myresa module, with the following code  :

/**
 * @file
 * Drupal Module : myresa
 * Adds custom code specific to this Drupal 7 site.
 */

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
  $form[‘box’] = array(
    ‘#type’ => ‘markup’,
    ‘#prefix’ => ‘’,
    ‘#suffix’ => ‘’,
    ‘#markup’ => ‘Price not computed',
  );

  $form[‘mybtn’] = array(
    ‘#type’ => ‘button’,
    ‘#id’ => ‘mybtn’,
    ‘#ajax’ => array(
      ‘callback’ => ‘myresa_callback’,
      ‘wrapper’ => ‘box’,
      ‘name’ => ‘mybtn1’,
    ),
    ‘#value’ => t(‘Compute the price’),
  );
}

function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#markup’] = «The price for » . $form_element[‘field_km’][‘#default_value’] . » km is 35 euros» ;
  return $element ;
}

The form shows the 'box' markup, the button, and when clicked the button activates the callback and shows the wanted text, but not the value of 'field_km'. It is probably a simple syntax mistake, but it beats me.

Thank you very much in advance for sparing 30 seconds to help ;-))

Show the value of a node-form field inside an AJAX field added via hook_form_alter()

I have created a simple content type "reservation" with an integer field called "field_km" with a default value of 50. 
On the node creation form, I want to have a markup field showing the value of the "field_km" field. So, I have created a simple module with the following code:

/**
 * @file
 * Drupal Module : myresa
 * Adds custom code specific to this Drupal 7 site.
 */

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
  $form[‘box’] = array(
    ‘#type’ => ‘markup’,
    ‘#prefix’ => ‘’,
    ‘#suffix’ => ‘’,
    ‘#markup’ => ‘Price not computed',
  );

  $form[‘mybtn’] = array(
    ‘#type’ => ‘button’,
    ‘#id’ => ‘mybtn’,
    ‘#ajax’ => array(
      ‘callback’ => ‘myresa_callback’,
      ‘wrapper’ => ‘box’,
      ‘name’ => ‘mybtn1’,
    ),
    ‘#value’ => t(‘Compute the price’),
  );
}

function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#markup’] = «The price for » . $form_element[‘field_km’][‘#default_value’] . » km is 35 euros» ;
  return $element ;
}

The form shows the 'box' markup, the button, and when clicked the button activates the callback and shows the wanted text, but not the value of 'field_km'. It is probably a simple syntax mistake, but it beats me.

added 37 characters in body
Source Link
googletorp
  • 35.4k
  • 10
  • 69
  • 109

I am stuck with a probably very simple problem. I have created a simple content type "reservation" with an integer field called "field_km" and of default value 50. On the node creation form, I want to have a markup field showing the value of the "field_km" field. So, I have created a simple myresa module, with the following code :

< ?php
// $Id$

/**
 * @file
 * Drupal Module : myresa
 * Adds custom code specific to this Drupal 7 site.
 */

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
  $form[‘box’] = array(
    ‘#type’ => ‘markup’,
    ‘#prefix’ => ‘’,
    ‘#suffix’ => ‘’,
    ‘#markup’ => ‘Price not computed',
)  );

  $form[‘mybtn’] = array(
    ‘#type’ => ‘button’,
    ‘#id’ => ‘mybtn’,
    ‘#ajax’ => array(
      ‘callback’ => ‘myresa_callback’,
      ‘wrapper’ => ‘box’,
      ‘name’ => ‘mybtn1’,
    ),
    ‘#value’ => t(‘Compute the price’),
)  );
}

function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#markup’] = «The price for » . $form_element[‘field_km’][‘#default_value’] . » km is 35 euros» ;
  return $element ;
}

The form shows the 'box' markup, the button, and when clicked the button activates the callback and shows the wanted text, but not the value of 'field_km'. It is probably a simple syntax mistake, but it beats me.

Thank you very much in advance for sparing 30 seconds to help ;-))

I am stuck with a probably very simple problem. I have created a simple content type "reservation" with an integer field called "field_km" and of default value 50. On the node creation form, I want to have a markup field showing the value of the "field_km" field. So, I have created a simple myresa module, with the following code :

< ?php
// $Id$

/**
* @file
* Drupal Module : myresa
* Adds custom code specific to this Drupal 7 site.
*/

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
$form[‘box’] = array(
‘#type’ => ‘markup’,
‘#prefix’ => ‘’,
‘#suffix’ => ‘’,
‘#markup’ => ‘Price not computed',
) ;

$form[‘mybtn’] = array(
‘#type’ => ‘button’,
‘#id’ => ‘mybtn’,
‘#ajax’ => array(
‘callback’ => ‘myresa_callback’,
‘wrapper’ => ‘box’,
‘name’ => ‘mybtn1’,
),
‘#value’ => t(‘Compute the price’),
) ;
}

function myresa_callback($form, $form_state) {
$element = $form[‘box’] ;
$element[‘#markup’] = «The price for » . $form_element[‘field_km’][‘#default_value’] . » km is 35 euros» ;
return $element ;
}

The form shows the 'box' markup, the button, and when clicked the button activates the callback and shows the wanted text, but not the value of 'field_km'. It is probably a simple syntax mistake, but it beats me.

Thank you very much in advance for sparing 30 seconds to help ;-))

I am stuck with a probably very simple problem. I have created a simple content type "reservation" with an integer field called "field_km" and of default value 50. On the node creation form, I want to have a markup field showing the value of the "field_km" field. So, I have created a simple myresa module, with the following code :

/**
 * @file
 * Drupal Module : myresa
 * Adds custom code specific to this Drupal 7 site.
 */

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
  $form[‘box’] = array(
    ‘#type’ => ‘markup’,
    ‘#prefix’ => ‘’,
    ‘#suffix’ => ‘’,
    ‘#markup’ => ‘Price not computed',
  );

  $form[‘mybtn’] = array(
    ‘#type’ => ‘button’,
    ‘#id’ => ‘mybtn’,
    ‘#ajax’ => array(
      ‘callback’ => ‘myresa_callback’,
      ‘wrapper’ => ‘box’,
      ‘name’ => ‘mybtn1’,
    ),
    ‘#value’ => t(‘Compute the price’),
  );
}

function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#markup’] = «The price for » . $form_element[‘field_km’][‘#default_value’] . » km is 35 euros» ;
  return $element ;
}

The form shows the 'box' markup, the button, and when clicked the button activates the callback and shows the wanted text, but not the value of 'field_km'. It is probably a simple syntax mistake, but it beats me.

Thank you very much in advance for sparing 30 seconds to help ;-))

added 138 characters in body
Source Link
Berdir
  • 83.5k
  • 6
  • 161
  • 207
Loading
Source Link
Loading