我正在从数据库字段创建一个表单,所以我拉出所有记录并循环并在php中的foreach循环中添加表单元素。问题是,当我提交表单时,元素未发布,我得到的唯一回报是提交按钮: -
stdClass Object
(
[submitbutton] => Submit
)
这是我创建元素的方式,这些都在屏幕上正确显示和完成,它只是在我提交时不发布,但是如果我在foreach循环中没有它们,则元素会发布,但我需要创建他们动态地从数据库,任何想法?
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 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" />
调查选择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>
答案 0 :(得分:0)
使用此代码,也可以在此表单中添加提交按钮
<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>
答案 1 :(得分:0)
解决了!我发现首次加载页面时我在URL中传递的额外参数以及首次创建表单时还需要在提交表单时存在,因此url上的额外参数搜索数据库记录,以及何时我提交了表单,页面被重新加载,所有函数再次被调用并失败,因为缺少参数并且找不到记录。希望这有助于其他人。