用ajax更新html代码

时间:2012-11-22 06:21:28

标签: php html ajax zend-framework

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

我用Zend框架写了一个表单然后在其中添加了一个subForm。取决于select子项的值,子表单更改,它将具有新元素。我写了一个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>

在我的更新页面中有一个元素名称子窗体[列],当我更改选择项时,子窗体被替换,但是当我得到子窗体列元素时它什么都没有显示

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

0 个答案:

没有答案