CakePHP/jQuery 表单解析
我有一个可以通过 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(); ?>
I have a form that can be manipulated on the client's browser via javascript (i.e. rows can be added, deleted, etc). I would like to somehow send this form data back to the server, but I'm not sure what the best approach would be.
One approach I thought of would be to dynamically generate ids for each row that gets added to the form, but I see this getting really messy when I try to parse the POST data back in my CakePHP controller.
Another approach that came to mind would be to parse the form clientside, generate a JSON or XML structure that can be more easily iterated over, and send that back to the controller. I'm not sure how to go about implementing something like this though.
What do you guys think the best solution for this is?
<?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(); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的输入的名称类似于
data[Model][0][field]
、data[Model][1][field]
(对应于$this- >Form->input('Model.0.field')
等),然后在控制器中您可以将 POST 数据提供给 saveMany (Cake 2.x) 或 saveAll (Cake 1.x) 它将保存多条记录。让 jQuery 创建与此方案相对应的新表单输入应该很简单。
If your inputs have names like
data[Model][0][field]
,data[Model][1][field]
(corresponding to$this->Form->input('Model.0.field')
etc), then in the controller you can give the POST'd data to saveMany (Cake 2.x) or saveAll (Cake 1.x) and it will save multiple records.It should be straightforward to have jQuery create new form inputs corresponding to this scheme.