0

I am dynamically showing the radio box values from database on a form.

<input id="edit-info-prog-dates-0" class="form-radio" type="radio" value="0" name="info[prog_dates]">
<label for="edit-info-prog-dates-0" class="option"><div class="test-cost-date">Aug 13, 2012 - Sep 24, 2012</div><div class="currency-w1"><span class="currency-symbol">$</span><span class="currency-cost">350.00</span> <span class="currency-code">CAD</span></div> </label>

I am using the following code to generate the above HTML.

 $form['info']['prog_dates'] = array(
    '#type' => 'radios',
    '#title' => t('Select Dates'),
    '#required' => TRUE,
    '#options' => $dates,
    '#prefix' => '<div id="radios-programdates">',
    '#suffix' => '</div>'
);

I want to save the value for the cost in database. If I write the following code, I am only getting the input value '0'.

$form_state['info']['prog_dates']

How do I fetch the cost value, which is inside the <span> and <label> in HTML?

2 Answers 2

1

From the HTML you show, I get that the partial content of $dates is the following.

$dates = array(
  0 => '<div class="test-cost-date">Aug 13, 2012 - Sep 24, 2012</div><div class="currency-w1"><span class="currency-symbol">$</span><span class="currency-cost">350.00</span> <span class="currency-code">CAD</span></div>'
);

What you can do to get the 350.0 value is changing $dates to the following.

$dates = array(
  '350.0' => '<div class="test-cost-date">Aug 13, 2012 - Sep 24, 2012</div><div class="currency-w1"><span class="currency-symbol">$</span><span class="currency-cost">350.00</span> <span class="currency-code">CAD</span></div>',
  // ...
);

In that way, $form_state['values']['info']['prog_dates'] would contain '350.0', if the users select the radio button I shown.

From the HTML you shown, I get that in the form you set #tree to TRUE. Differently, the HTML name assigned to the radio form field would not be info[prog_dates] as in your case, but prog_dates. If I am wrong about that, it means you need to check the value of $form_state['values']['prog_dates'], instead of $form_state['values']['info']['prog_dates'].

0

If [info] is a fieldset then to get the value of "prog_dates" you only need:

$form_state['values']['prog_dates']
1
  • It could be also $form_state['values']['prog_dates'] if the form has #tree set to TRUE.
    – avpaderno
    Commented Nov 18, 2012 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.