【发布时间】:2012-02-20 07:09:12
【问题描述】:
我有一个可以通过 javascript 在客户端浏览器上操作的表单(即可以添加、删除行等)。我想以某种方式将此表单数据发送回服务器,但我不确定最好的方法是什么。
我想到的一种方法是为添加到表单中的每一行动态生成 id,但是当我尝试在我的 CakePHP 控制器中解析 POST 数据时,我发现这变得非常混乱。
想到的另一种方法是解析表单客户端,生成更容易迭代的 JSON 或 XML 结构,然后将其发送回控制器。不过,我不确定如何实施这样的事情。
你们认为最好的解决方案是什么?
<?php echo $this->Html->script('jquery-1.7', FALSE); ?>
<h2>Assign Responses</h2>
<a id='addResponseLink' href='#'>Add a Response Action</a>
<?php
$this->Js->get('#addResponseLink');
$this->Js->event('click', "$('#responseActionsTable').append('<tr style=\'display:none\' id=\'row\'>".
"<td>".$this->Form->input('trigger_word')."</td>".
"<td>PROCEED"."</td>".
"<td>5"."</td>".
"<td><a id=\'removeLink\' href=\'#\'>Remove</a></td>".
"</tr>');".
"$('tr').last().fadeIn();".
"$('#removeLink').live('click', function() { $(this).closest('tr').fadeOut().delay(1500).queue(function() $(this).remove()); })");
?>
<?php
echo $this->Form->create(array('action' => 'assignResponsesSave'));
?>
<table id='responseActionsTable'>
<tr>
<th>Trigger Word</th>
<th>Action</th>
<th>Next Question</th>
<th>Remove</th>
</tr>
<?php foreach($responseActions as $responseAction): ?>
<tr>
<td><?php echo $this->Form->input('trigger_word', array('default' => $responseAction['responses_actions']['trigger_word'])); ?></td>
<td><?php echo($responseAction['responses_actions']['action']); ?></td> <!-- This and the next sibling will be inputs -->
<td><?php echo($responseAction['responses_actions']['next_question']); ?></td>
<td>Remove</td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->Form->submit('Save Changes'); ?>
<?php echo $this->Form->end(); ?>
<?php echo $this->Js->writeBuffer(); ?>
【问题讨论】:
标签: php javascript jquery cakephp