Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax更新html代码_Php_Html_Ajax_Zend Framework - Fatal编程技术网

Php 使用ajax更新html代码

Php 使用ajax更新html代码,php,html,ajax,zend-framework,Php,Html,Ajax,Zend Framework,更新:我解决了这个问题,但解决方案仍然是暂时的。显然,submit按钮包含submit和。。。因此,当我将子表单更改为新元素时,提交不会更改。所以我从主窗体中删除了submit按钮并将其添加到子窗体,在更新页面中添加了它自己的submit按钮,问题就解决了。但说实话,我不喜欢这个解决方案 我用Zend framework编写了一个表单,然后在其中添加了一个子表单。根据选择项的值,子窗体将发生更改,并将包含新元素。我写了一个ajax来实现它。然而,另一方面,getParam/Post方法无法获取新

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

我用Zend framework编写了一个表单,然后在其中添加了一个子表单。根据选择项的值,子窗体将发生更改,并将包含新元素。我写了一个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>

你试了什么。请添加一些它将帮助我们回答一些示例代码??如果您在表单中正确地放置元素,即使是通过javascript,它也会得到POST
 public function insertRuleAction()
{
    $arr=$this->_request->getParam('subform');
    echo 'arr:'.$arr['column'];