【问题标题】:CakePHP/jQuery Form ParsingCakePHP/jQuery 表单解析
【发布时间】: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


    【解决方案1】:

    如果您的输入具有 data[Model][0][field]data[Model][1][field] 之类的名称(对应于 $this-&gt;Form-&gt;input('Model.0.field') 等),那么在控制器中,您可以将 POST 数据提供给 saveMany (Cake 2.x) 或 @987654322 @ (Cake 1.x) 会保存多条记录。

    让 jQuery 创建与此方案相对应的新表单输入应该很简单。

    【讨论】:

      猜你喜欢
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多