0

So, this is a simple form, and what the ajax call back does here is that it returns the value of a field that's being changed.

The problem is that, only the value of the first change is returned. If i change the value a second time, the returned value is the same as first. I'm not sure what is happening here, it needs a better pair of eyes.

Here's the code

<?php

    //Create raffle form
    function raffle_create_form($form, &$form_state)
    {
        dpm($form_state);
        //Basic raffle info
        $form['raf_raffle_name'] = array(
            '#type' => "textfield",
            "#prefix" => "<h5>Raffle information</h5><hr>",
            "#title" => "Raffle Name:",
            "#required" => TRUE
        );

        $form['raf_allowed_number'] = array(
            '#type' => "textfield",
            "#title" => "Number of allowed members",
            "#required" => TRUE,
            '#element_validate' => array('element_validate_number')
        );

        $form['raf_start_now'] = array(
            "#type" => "checkbox",
            "#title" => "Start raffle now?"
        );

        $form['raf_end_date'] = array(
            "#type" => "date",
            "#title" => "When to close this raffle?",
            "#required" => TRUE
        );

        $form['raf_prize_type'] = array(
            '#type' => "select",
            "#options" => array('gift' => 'Steam Gift', 'hardware' => 'Gaming peripherals'),
            "#title" => "Prize type",
            "#required" => TRUE,
            "#ajax" => array(
                'callback' => 'raffle_create_form_type_steam',
                'wrapper' => 'steam_options',
                'event' => 'change',
                'method' => 'replace',
            ),
        );

        $form['raf_prize_name'] = array(
            '#type' => "textfield",
            "#title" => "Prize name",
            "#prefix" => "<div id='steam_options'></div>",
            "#required" => TRUE
        );

        $form['raf_prize_about'] = array(
            '#type' => "textarea",
            "#resizeable" => true,
            "#title" => "Prize description"
        );

        $form['raf_prize_thumb'] = array(
            '#type' => "file",
            "#title" => "Prize thumbnail"
        );

        $form['raf_prize_link'] = array(
            '#type' => 'textfield',
            '#title' => "Link to product"
        );

        $form['raf_raffle_create'] = array(
            '#type' => 'submit',
            '#value' => "Create Raffle",
        );

        $form['#submit'][] = 'raffle_create_form_submit';

        return $form;
    }

    function raffle_create_form_validate($form, &$form_state)
    {
        dpm($form_state);
    }

    function raffle_create_form_submit($form, &$form_state)
    {

    }

    function raffle_create_form_type_steam($form, &$form_state)
    {
        return $form_state['values']['raf_prize_type'];
    }**
  • It could help form framework – Paul Bönisch Jan 15 '16 at 14:30
  • sorry, that's not what im lookin for – Haider Ali Jan 15 '16 at 14:41
  • why are you not using #suffix key? – Shabir A. Jan 15 '16 at 14:42
  • @ShabirA. Because, i need to add a new input field and not replace an existing one. – Haider Ali Jan 15 '16 at 14:43
0

Whatever data is returned must be encapsulated in a div element of the same id as the wrapper.

This fixed the problem for me :)

function raffle_create_form_type_steam($form, &$form_state)
{
    if($form_state['values']['raf_prize_type'] == 'hardware')
    {
        return "<div id='steam_options'></div>";
    }
    else
    {
        return  $form['raf_steam_option_game_append'] = array(
                '#type' => 'checkbox',
                '#title' => 'Check if Steamer has game in library',
                '#description' => 'Does not allow gamer to join raffle if said gift already exists within gamer\'s steam library',
                "#prefix" => "<div id='steam_options'>",
                "#suffix" => "</div>"
            );
    }

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.