javascript - JQuery:在 cakePHP 中验证失败后如何保存动态添加的表单输入

标签 javascript php jquery validation cakephp

我的注册表有问题。因此,当用户单击链接时,我有一些字段被生成,如果验证失败,在我按下表单提交按钮后,这些字段就会丢失。如果验证通过,则一切正确,数据已保存到数据库中。

这些就是这些字段,以及添加新字段的脚本:

<script> 
  $(document).ready(function() {

    var MaxInputs       = 8; //maximum input boxes allowed
    var FacebookInputsWrapper   = $("#FacebookInputsWrapper"); //Input boxes wrapper ID
    var AddButton       = $("#FacebookAddMoreFileBox"); //Add button ID

    var x = FacebookInputsWrapper.length; //initlal text box count
    var FieldCount=0; //to keep track of text box added

    $(AddButton).click(function (e)  //on add input button click
    {
        if(x <= MaxInputs) //max input box allowed
        {

        FieldCount++; //text box added increment
        //add input box
        $(FacebookInputsWrapper).append('<div style="margin-top:10px;"><input id="SocialMediaLink' + FieldCount + 'Link" type="hidden" name="data[SocialMediaLink][' + FieldCount + '][type]" value="fb" /><input id="SocialMediaLink' + FieldCount + 'Link" type="text" name="data[SocialMediaLink][' + FieldCount + '][link]" class="input-xlarge" placeholder="Facebook link" /><a style="background:0;color:black;" href="#" class="facebookremoveclass">&times;</a></div>');
        x++; //text box increment
        }
    return false;
    });

    $("body").on("click",".facebookremoveclass", function(e){ //user click on remove text
        if( x > 1 ) {
            $(this).parent('div').remove(); //remove text box
            x--; //decrement textbox
        }
    return false;
    }) 

  });
  </script>

HTML

<li><label>Facebook</label>
<div >
  <div style="float:right; margin-top:10px;" id="FacebookInputsWrapper"><?php echo $this->Form->input('SocialMediaLink.0.type',array('type'=>'hidden','value'=>'fb')); ?><?php echo $this->Form->input('SocialMediaLink.0.link',array('type'=>'text','class'=>'input-xlarge','label'=>false,'div'=>false,'placeholder'=>'Facebook link', 'error' => array(
    'attributes' => array('class' => 'inputerror')
))); ?><label  style="color:#FF0000;"><?php echo @$valid_social; ?></label>
  <a style="margin-left:10px;background : 0;color:black;" href="#" id="FacebookAddMoreFileBox">Add another Facebook account</a></div>

</div>

所以我的问题是:是否可以在验证失败后保存动态生成的表单字段及其值以及如何保存?

谢谢

最佳答案

如果我正确理解了这个问题,您需要在验证失败后在 View 中显示那些动态创建的字段。这样做,检查这些字段的 $this->request->data :

            <?if (!empty($this->request->data['SocialMediaLink'])):?>
                <?foreach($this->request->data['SocialMediaLink'] as $i => $item):?>
                    <div style="margin-top:10px;">
                        <?=$this->Form->hidden('SocialMediaLink.' . $i . '.type', array('value' => 'fb'))?>
                        <?=$this->Form->input('SocialMediaLink.' . $i . '.link')?>
                    </div>
                <?endforeach;?>
            <?endif;?>

或者,您可以根据 $this->request->data['SocialMediaLink 中的元素数量,在页面加载时触发 AddButton 上的点击事件适当的次数']

关于javascript - JQuery:在 cakePHP 中验证失败后如何保存动态添加的表单输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23597813/

相关文章:

javascript - jQuery 折叠选项卡(如果选择相同)

php - PHP 中 UNIX 的 ATOM 日期格式

javascript - 如何在页面不自动滚动回顶部的情况下显示固定的 div

jquery - 按钮不显示

JavaScript appendChild() 带有 href 会使浏览器崩溃

javascript - 如何在 jquery 中对规则进行分组

javascript - Jquery 仅适用于首页加载(ajax 分页)

php - 使用 PHPDoc 自动添加 Git 版本

php - 将 Id 数组传递给数据库查询 - Codeigniter

jquery - 您如何为用于 jquery 编码的菜单栏正确编写 CSS 代码?