更新:私は問題を解決しましたが、それでも解決策は本当に一時的なものです。どうやら送信ボタンは送信と...の値を保持しているので、サブフォームを新しい要素に変更しても送信は変更されません。そこで、メインフォームから送信ボタンを削除してサブフォームに追加し、更新ページに独自の送信ボタンを追加して問題を解決しました。しかし、正直なところ、私はこの解決策が好きではありませんでした。
Zendフレームワークを使用してフォームを作成し、その中にサブフォームを追加しました。選択したアイテムの値に応じてサブフォームが変更され、新しい要素が追加されます。私はそれを行うためにajaxを書きました。ただし、反対側では、getParam / Postメソッドは新しい要素の値を取得できません(つまり、ajaxでフォームを変更しても、htmlコードは同じままです)forms要素を更新するにはどうすればよいですか?
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]があり、select Itemを変更するとサブフォームが置き換えられましたが、subformcolumn要素を取得すると何も表示されません
public function insertRuleAction()
{
$arr=$this->_request->getParam('subform');
echo 'arr:'.$arr['column'];