This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Dynamic Radio Button Value


  1. msfbiz
    Member

    I'd like to choose the value of a radio button according to the value of a PHP variable defined in the page where the Machform form is embedded.

    How do I need to modify view-functions.php file?

    Thanks!
    Mike

    Posted 13 years ago #
  2. yuniar

    Are you using the advanced form code or the standard (iframe)?
    Also, is the PHP variable being passed as URL parameter?


    MachForm Founder

    Posted 13 years ago #
  3. msfbiz
    Member

    I'm using the advanced form code.

    No, the PHP variable is NOT passed as a URL parameter, it's defined in the PHP code of the page at the top.

    Posted 13 years ago #
  4. yuniar

    Ok, so let say you have a radio button like this:

    <input id="element_2_1" name="element_2" class="element radio" type="radio" value="1" />
    <label class="choice" for="element_2_1">First option</label>
    <input id="element_2_2" name="element_2" class="element radio" type="radio" value="2" />
    <label class="choice" for="element_2_2">Second option</label>
    <input id="element_2_3" name="element_2" class="element radio" type="radio" value="3" />

    and your form code + variable is like this:

    <?php
    
    	$my_radio = 2;
    
    	require("/home/machform/machform.php");
    	$mf_param['form_id'] = 159;
    	$mf_param['base_path'] = 'http://example.com/machform/';
    	display_machform($mf_param);
    
    ?>

    and our goal is to choose the second radio button (id = 2) when the value of $my_radio variable is 2 as well.

    So, all you need to do is to edit your "includes/view-functions.php" file.
    Search around line 493 for this code:

    foreach ($element->options as $option){

    exactly below that code, add this:

    if($option->id == 2 && $GLOBALS['my_radio'] == 1){
    	$option->is_default = true;
    }

    that should do it.


    MachForm Founder

    Posted 13 years ago #
  5. msfbiz
    Member

    Great, thanks!!!

    Posted 13 years ago #
  6. msfbiz
    Member

    I have tried the code, but it's not working.

    Also shouldn't it be

    if($option->id == 2 && $GLOBALS['my_radio'] == 2){
    	$option->is_default = true;
    }

    since we want the radio value 2 to be selected?

    In any case, I've tried with both == 1 and ==2 but couldn't get it to work.

    Posted 13 years ago #
  7. yuniar

    Can you send me your file and the URL to your form please?
    Send to: customer.service [at] appnitro.com

    I'll check it.


    MachForm Founder

    Posted 13 years ago #

RSS feed for this topic

Reply