来自javascript的CakePHP表单
我可以从PHP生成我的表单字段:来自javascript的CakePHP表单,php,javascript,jquery,cakephp,Php,Javascript,Jquery,Cakephp,我可以从PHP生成我的表单字段: <?php echo $this->Form->input('Model.0.name', array( 'label' => __('Name') ) ); ?> 但是,需要“动态”创建字段,例如当用户单击“新名称字段”时。我不知道如何开始..我不知道cakephp如何工作,但假设它与控制器->函数(site.com/controller/function)一起工作,您
<?php
echo $this->Form->input('Model.0.name', array(
'label' => __('Name')
)
);
?>
但是,需要“动态”创建字段,例如当用户单击“新名称字段”时。我不知道如何开始..我不知道cakephp如何工作,但假设它与控制器->函数(site.com/controller/function)一起工作,您可以这样做:
<?
class FieldCreator extends Controller{
public function input()
{
echo $this->Form->input('Model.0.name', array(
'label' => __('%name%')
)
);
}
}
<html>
<head>
<!-- import jquery framewrok -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
</head>
<body>
<!-- This link will act like a button -->
<a href="#" class="new_input">New name field<a>
<form id="my_form">
</form>
<!-- Prepare the script once the page is ready -->
<script type="text/javascript">
$(document).ready(function(){
var content = '';
var index = 1;
// Use click event in the link above to trigger the request
$('.new_input').click(function(){
if (content == '')
// Do the request to site.com/FieldCreator/input
$.get('site.com/FieldCreator/input', function(data){
content = data;
});
//create the input
createInput('name_' + index);
});
});
function createInput(name)
{
var input = content.replace('%name%', name);
$('#my_form').append(input);
index ++;
}
</script>
</body>
</html>
但是,如果要重新利用GET结果,可以:
'label'=>\uuuuu('Name')
更改为'label'=>\uuuuuuu('%Name%')
有几种方法可以实现这一点,包括客户端的javascript/jquery解决方案。问题是,你的最终目的是什么?什么会受到“动态”的影响changes@SpYk3HH我有一个有很多字段的表单。一组字段构成客户地址(主地址、账单地址等)。我需要显示
Model.0.field
字段,当单击“添加更多”时,为更多地址添加dinamic字段。下面是一个示例。只需将字段的名称/ID从nameX
修改为Model.X.field
@Ross我知道如何创建jQuery脚本,但需要与CakePHP集成..视情况而定,您可以将结果存储在变量中并重用它。