jquery - 发布动态字段数据返回到 Cakephp 中查看

标签 jquery ajax validation cakephp dynamic

我正在为学校的非营利组织开发一个系统,如果存在验证错误,我需要将动态字段中的数据发布回 View 。下面是创建动态字段的代码。

-这是jquery函数

var lastRow=0;

function addPerson() {
    lastRow++;
    $("#victimsTable tbody>tr:#victim0").clone(true).attr('id','victim'+lastRow).removeAttr('style').insertBefore("#victimsTable tbody>tr:#trAdd");
    $("#victim"+lastRow+" button").attr('onclick','removePerson('+lastRow+')');
    $("#victim"+lastRow+" input:eq(0)").attr('name','data[Victim]['+lastRow+'][victimId]').attr('id','victimVictimId'+lastRow);
    $("#victim"+lastRow+" input:eq(1)").attr('name','data[Victim]['+lastRow+'][firstName]').attr('id','victimFirstName'+lastRow);
    $("#victim"+lastRow+" input:eq(2)").attr('name','data[Victim]['+lastRow+'][lastName]').attr('id','victimLastName'+lastRow);
    $("#victim"+lastRow+" input:eq(3)").attr('name','data[Victim]['+lastRow+'][dateOfBirth]').attr('id','victimDateOfBirth'+lastRow);
    $("#victim"+lastRow+" select.g").attr('name','data[Victim]['+lastRow+'][gender]').attr('id','victimGender'+lastRow);
    $("#victim"+lastRow+" select.r").attr('name','data[Victim]['+lastRow+'][race]').attr('id','victimRace'+lastRow);
    $("#victim"+lastRow+" input:eq(4)").attr('name','data[Victim]['+lastRow+'][streetAddress]').attr('id','victimStreetAddress'+lastRow);
    $("#victim"+lastRow+" input:eq(5)").attr('name','data[Victim]['+lastRow+'][zipCode]').attr('id','victimZipCode'+lastRow);
    $("#victim"+lastRow+" input:eq(6)").attr('name','data[Victim]['+lastRow+'][city]').attr('id','victimCity'+lastRow);
    $("#victim"+lastRow+" input:eq(7)").attr('name','data[Victim]['+lastRow+'][state]').attr('id','victimState'+lastRow);
    $("#victim"+lastRow+" input:eq(8)").attr('name','data[Victim]['+lastRow+'][email]').attr('id','victimEmail'+lastRow);
    $("#victim"+lastRow+" input:eq(9)").attr('name','data[Victim]['+lastRow+'][phoneOne]').attr('id','victimPhoneOne'+lastRow);
    $("#victim"+lastRow+" select.p1t").attr('name','data[Victim]['+lastRow+'][phoneOneType]').attr('id','victimPhoneOneType'+lastRow);
    $("#victim"+lastRow+" input:eq(10)").attr('name','data[Victim]['+lastRow+'][phoneTwo]').attr('id','victimPhoneTwo'+lastRow);
    $("#victim"+lastRow+" select.p2t").attr('name','data[Victim]['+lastRow+'][phoneTwoType]').attr('id','victimPhoneTwoType'+lastRow);
    AddVictimCalScript(lastRow);
    AddVictimChosenScript(lastRow);
    AddVictimChosenRaceScript(lastRow);
}
function removePerson(x) {
    $("#victim"+x).remove();
}

-这些是字段的输入

<h3>Add Victims</h3>
<div class="table-responsive">
<table class="table" id="victimsTable">
<tr>
<tr id="victim0" style="display:none;">
    <td><?php echo $this->Form->button('&nbsp;Remove this Victim&nbsp;',array('type'=>'button','title'=>'Click Here to remove this victim','onclick'=>'removePerson()')); ?>

    <?php echo $this->Form->input('unused.victimId',array('type'=>'text','id'=>'resultField2')); ?>

    <?php echo $this->Form->input('unused.firstName',array('label'=>'First Name')); ?>
    <?php echo $this->Form->input('unused.lastName',array('label'=>'Last Name')); ?>
    <?php echo $this->Form->input('unused.dateOfBirth',array('type'=>'text')); ?>
    <?php echo $this->Form->input('unused.gender',array('class'=>'g','label'=>'Gender','type'=>'select','options'=>array('Unassigned'=>'----','Male' =>'Male','Female'=>'Female'))); ?>
    <?php echo $this->Form->input('unused.race',array('class'=>'r','label'=>'Race','type'=>'select','options'=>$race)); ?>
    <?php echo $this->Form->input('unused.streetAddress',array('label'=>'Street Address')); ?>
    <?php echo $this->Form->input('unused.zipCode',array('label'=>'Zip Code')); ?>
    <?php echo $this->Form->input('unused.city',array('label'=>'City')); ?>
    <?php echo $this->Form->input('unused.state',array('label'=>'State')); ?>
    <?php echo $this->Form->input('unused.email',array('label'=>'Email')); ?>
    <?php echo $this->Form->input('unused.phoneOneType',array('class'=>'p1t','label'=>'Phone One Type','type'=>'select','options'=>$phonetype)); ?>
    <?php echo $this->Form->input('unused.phoneOne',array('label'=>'Phone One')); ?>
    <?php echo $this->Form->input('unused.phoneTwoType',array('class'=>'p2t','label'=>'Phone Two Type','type'=>'select','options'=>$phonetype)); ?>
    <?php echo $this->Form->input('unused.phoneTwo',array('label'=>'Phone Two')); ?>
    </td>
</tr>
<tr id="trAdd"> </tr>
<tfoot>
    <tr>
    <td id="trAdd"> <?php echo $this->Form->button('Add Victim(s)',array('type'=>'button','label'=>'Click Here to add another victim','onclick'=>'addPerson()')); ?> </td>
    <tr>
</tfoot>
</table>
</div>

我的问题是,如果存在验证错误,如何将数据发回查看?代码保存得很好,但我对 cakephp、ajax 和 jquery 编程还很陌生。任何帮助将不胜感激

最佳答案

你可以试试这个:

$this->Model->save( $data, false);

但如果您必须忽略验证,则意味着您的受孕失败。您应该更改验证标准或更改数据库中的方法。

另一种可能性是您的应用程序中有安全组件。它阻止在不使用 jQuery 包装器的情况下使用客户端生成的动态表单。她是如何做到的。

http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html

关于jquery - 发布动态字段数据返回到 Cakephp 中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467863/

相关文章:

javascript - API Ajax 结构化

php - 如何将 blob url 发送到 ajax

javascript - 为什么这个 XMLHttpRequest POST 甚至无法连接到服务器?

jQuery 与 Scriptaculous 冲突

javascript - jQuery 自动完成建议所有选项,无论输入条目如何

javascript - $(document).ready 中的 jquery 的 removeAttr 函数不起作用

jquery-plugins - 仅当使用 jquery 验证插件选中复选框时才将 minLength 设置为字段

javascript - 检查号码在国家/地区是否是移动电话或固定电话,两者格式相同

php - 包罗万象的MySQL数据验证

javascript - jQuery 失去了添加的类