0

更新:我解决了这个问题,但解决方案仍然是暂时的。显然,提交按钮保存了提交和...的值,所以当我将子表单更改为新元素时,提交不会改变。所以我从主窗体中删除了提交按钮并将其添加到子窗体中,在更新页面中我添加了它自己的提交按钮,问题就解决了。但老实说,我不喜欢这种解决方案。

我用 Zend 框架编写了一个表单,然后在其中添加了一个子表单。取决于选择项的值,子表单会发生变化,并且它将具有新元素。我写了一个ajax来做到这一点。但是另一方面,getParam/Post 方法无法获取新元素值(我的意思是当我使用 ajax 更改表单时,html 代码保持不变)如何更新表单元素?

public function init() {
    //echo "in init";
    $this -> setMethod('post');
    $this->setAction('insert-rule');
    //echo "after set metho";
    $ruleName = new Zend_Form_Element_Text('rname');
    $ruleName -> setLabel('Rule Name:') -> setRequired('true');
    $address = new Zend_Form_Element_Text('address');
    $address -> setLabel('Address') -> setRequired('true');
    $port = new Zend_Form_Element_Text('port');
    $port -> setLabel('Port') -> setRequired('true');
    $submit = new Zend_Form_Element_Submit('submit');
    $submit -> setValue('Submit');
    $this -> addElements(array($ruleName, $address, $port));
    $action = new Zend_Form_Element_Select('action');
    $action -> addMultiOption(0, "allow");
    $action -> addMultiOption(1, "warning");
    $action -> addMultiOption(2, "block");
    $command = new Zend_Form_Element_Select('command');
    $command -> addMultiOption(0, "select");
    $command -> addMultiOption(1, "update");
    $command->setAttrib('onChange', 'changeform()');
    //TODO will add more command
    //echo "after def";
    $this -> addElements(array($action, $command));
    //echo "after add";
    $subForm = $this -> mySubForm();
    //echo "after myform";
    $this ->addSubForm($subForm,'subform');
    //echo "add subform";
    $this->addElement($submit);
    $subForm -> setElementDecorators(array('ViewHelper',
     array( array('data' => 'HtmlTag'),
      array('tag' => 'td', 'class' => 'subform')), array('Label', array('tag' => 'td')), 
      array( array('row' => 'HtmlTag'), array('tag' => 'tr'))));
               $subForm->addDecorator('htmlTag', array('tag' => 'div','id'=>'sub'));
}

public function mySubForm() {
    ///echo "my subform";
    $subForm = new Zend_Form_SubForm();
    //echo "new ing";
    $table=new Zend_Form_Element_Text('table');
    $table -> setLabel('On') -> setRequired('true');
    $table->setValue('table name');
    $user=new Zend_Form_Element_Text('user');
    $user -> setLabel('for') -> setRequired('true');
    $user->setValue('user name');
    // echo "subform".$table->getName;
    $condition=new Zend_Form_Element_Text('condition');
    $condition -> setLabel('where') -> setRequired('true');
    $subForm -> addElements(array($table,$user,$condition));
    // $subForm -> setElementDecorators(array('ViewHelper',
     // array( array('data' => 'HtmlTag'),
      // array('tag' => 'td', 'class' => 'subform')), array('Label', array('tag' => 'td')), 
      // array( array('row' => 'HtmlTag'), array('tag' => 'tr'))));
      return $subForm;
}

这是我的表单代码,这是我的 ajax 代码

    <script>
        function changeform() {
            var sel=document.getElementById("command");
             var chosenoption=sel.options[sel.selectedIndex];
                 // window.alert(chosenoption.innerHTML);
            var xmlhttp;
            if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                // window.alert(xmlhttp.status);
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    // window.alert('response');
                    document.getElementById("sub").innerHTML = xmlhttp.responseText;
                }
            }

            if(chosenoption.innerHTML=='update') {

                xmlhttp.open("Post", "http://localhost/dbfirewall/public/add-rule/update", true);
                xmlhttp.send();
            }
        }
    </script>

在我的更新页面中有一个元素名称 subform[column] 并且当我更改选择项时子表单被替换但是当我得到子表单列元素时它什么也不显示

 public function insertRuleAction()
{
    $arr=$this->_request->getParam('subform');
    echo 'arr:'.$arr['column'];
4

0 回答 0