Question

I am making a form from database fields, so I pull all the records and loop through and add the form elements in a foreach loop in php. The problem is when I submit the form the elements are not posted, the only return I get is the submit button: -

stdClass Object
(
    [submitbutton] => Submit
)

This is how I create the elements, these all display and fucntion correctly onscreen, it just does not post when I submit, but the elements do post if I don't have them in the foreach loop, but I need to create them dynamically from the database, any ideas?

foreach($records as $log){
    $inc++;

    if($log->type == 0){ 

        $mform->addElement('html', '<p>'.$log->leadin.'</p>');

        $attributes = array();
        $distractors = explode(',', $log->distractors);
        $radioarray=array();
        $count = 0;

        foreach($distractors as $dis){
            $count++;
            $radioarray[] =& $mform->createElement('radio', 'radio', '', $dis, $count, array());
        }

        $mform->addGroup($radioarray, 'radioar'.$inc, '', array(' '), false);
    }
    else if($log->type == 1){

        $mform->addElement('html', '<div>'.$log->leadin.'</div>');

        $distractors = explode(',', $log->distractors);
        $count = 0;

        foreach($distractors as $dis){
            $count++;
            $mform->addElement('checkbox', 'check'.$count, $dis);
        }
    }}

Here is an image of the output of the above code, all displays and functions correctly.

enter image description here

<form autocomplete="off" action="blocks/training_plan/student/survey.php" method="post" accept-charset="utf-8" id="mform1" class="mform">
<div style="display: none;"><input name="sesskey" type="hidden" value="MJS9xXm2SA" />

Survey Choice 1?

<fieldset class="hidden"><div>
    <div id="fgroup_id_radioar1" class="fitem fitem_fgroup femptylabel"><div class="fitemtitle"><div class="fgrouplabel"><label> </label></div></div><fieldset class="felement fgroup"><span><input name="radio" value="1" type="radio" id="id_radio_1" /><label for="id_radio_1">1</label></span> <span><input name="radio" value="2" type="radio" id="id_radio_2" /><label for="id_radio_2">2</label></span> <span><input name="radio" value="3" type="radio" id="id_radio_3" /><label for="id_radio_3">3</label></span> <span><input name="radio" value="4" type="radio" id="id_radio_4" /><label for="id_radio_4">4</label></span></fieldset></div><div>Survey choice 2?</div>
    <div id="fitem_id_check1" class="fitem fitem_fcheckbox "><div class="fitemtitle"><label for="id_check1">1 </label></div><div class="felement fcheckbox"><span><input name="check1" type="checkbox" value="1" id="id_check1" /></span></div></div>
    <div id="fitem_id_check2" class="fitem fitem_fcheckbox "><div class="fitemtitle"><label for="id_check2">2 </label></div><div class="felement fcheckbox"><span><input name="check2" type="checkbox" value="1" id="id_check2" /></span></div></div>
    <div id="fitem_id_check3" class="fitem fitem_fcheckbox "><div class="fitemtitle"><label for="id_check3">3 </label></div><div class="felement fcheckbox"><span><input name="check3" type="checkbox" value="1" id="id_check3" /></span></div></div>
    <div id="fitem_id_check4" class="fitem fitem_fcheckbox "><div class="fitemtitle"><label for="id_check4">4 </label></div><div class="felement fcheckbox"><span><input name="check4" type="checkbox" value="1" id="id_check4" /></span></div></div>
    <div id="fitem_id_answer3" class="fitem fitem_ftextarea "><div class="fitemtitle"><label for="id_answer3">Survey choice 3? </label></div><div class="felement ftextarea"><textarea rows="10" cols="80" name="answer3" id="id_answer3"></textarea></div></div>
    </div></fieldset>
<fieldset class="hidden"><div>
    <div id="fgroup_id_buttonar" class="fitem fitem_actionbuttons fitem_fgroup"><div class="felement fgroup"><input name="submitbutton" value="Submit" type="submit" id="id_submitbutton" /> <input name="cancel" value="Cancel" type="submit" onclick="skipClientValidation = true; return true;" class=" btn-cancel" id="id_cancel" /></div></div>
    </div></fieldset>

Was it helpful?

Solution 2

SOLVED! I found that the extra parameter I was passing in the URL when the page is first loaded and the form is first created also needed to be present when the form is submitted, so the extra parameter on the url searched for a database record, and when I submitted the form, the page gets reloaded and all the functions get called again and fails because the parameter is missing and the records are not found. Hope this helps someone else.

OTHER TIPS

use this code, also add submit buttion this this form

<form autocomplete="off" action="blocks/training_plan/student/survey.php" method="post" accept-charset="utf-8" id="mform1" class="mform">
    <?php
        foreach($records as $log){
        $inc++;

        if($log->type == 0){ 

            $mform->addElement('html', '<p>'.$log->leadin.'</p>');

            $attributes = array();
            $distractors = explode(',', $log->distractors);
            $radioarray=array();
            $count = 0;

            foreach($distractors as $dis){
                $count++;
                $radioarray[] =& $mform->createElement('radio', 'radio', '', $dis, $count, array());
            }

            $mform->addGroup($radioarray, 'radioar'.$inc, '', array(' '), false);
        }
        else if($log->type == 1){

            $mform->addElement('html', '<div>'.$log->leadin.'</div>');

            $distractors = explode(',', $log->distractors);
            $count = 0;

            foreach($distractors as $dis){
                $count++;
                $mform->addElement('checkbox', 'check'.$count, $dis);
            }
        }}
    ?>
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top