必须按两次提交按钮才能继续


Have to press submit button twice in order to continue

在我的网页上,有用户可以编辑的记录。当他们编辑时。他们点击相关记录旁边的"编辑"按钮,就会进入编辑页面。在这里,他们可以编辑不同的字段,然后点击"提交"按钮提交编辑后的帖子。他们点击提交。什么也不会发生。他们再次点击提交,然后更改被保存,他们被重定向回索引。每个帖子都是这样。

这是我编辑视图的控制器。

    function edit ($id = NULL) {
    $this->loadModel('RecordDrugUnit');
    $this->loadModel('Drug');
    $this->loadModel('Unit');
    $this->loadModel('Route');
    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }
    $record=$this->RecordDrugUnit->findByrecordId($id);
    if(!$record) {
        throw new NotFoundException(__('Invalid post'));
    }
    /*----
    * Divide dose by
    * associated conversion
    -----*/
    $record['RecordDrugUnit']['dose'] = $record['RecordDrugUnit']['dose'] / $record['Unit']['conversion'];
    if($this->request->is('post')) {
        $this->request->data['Record']['id'] = $id;
        $this->request->data['RecordDrugUnit']['record_id'] = $id;
        $this->request->data['RecordDrugUnit']['id'] = $record['RecordDrugUnit']['id'];
        $this->request->data['Record']['user_id'] = $this->Auth->user('id');
        /*----
        * Multiply dose by
        * associated conversion
        -----*/
        $conv_val = $this->Unit->find('first',
        array(
            'conditions' => array(
                'id' => $this->request->data['RecordDrugUnit']['unit_id']),
            'fields' => array('conversion')
            ));
        $this->request->data['RecordDrugUnit']['dose'] = (float)$this->request->data['RecordDrugUnit']['dose'] * (float)$conv_val['Unit']['conversion'];
        if ($this->RecordDrugUnit->saveAssociated($this->request->data, array('deep' => TRUE))) {
            $this->Session->setFlash('Your log has been updated.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to update your post.');
        }
    }
    if(!$this->request->data) {
        $this->request->data = $record;
    }

}

和view

  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
        $(function() {
            $( "#datepicker" ).datepicker({ dateFormat: 'yy-m-dd' });
        });
    </script>
    <?php echo $this->Session->flash('auth'); ?>
    <?php echo $this->Form->create('Record'); ?>
        <header id='topHead'>
            <div class="frame headWrap">
                <div class="bit-2">
                    <div class ='padding background_color'>
                        <?php
                            echo $this->Form->input('Record.dose_date', array('placeholder' => 'Date of Dose', 'label' => false,'type' => 'text','id' =>'datepicker'));    
                            echo $this->Form->input('RecordDrugUnit.drug_id', array('placeholder' => 'Substance', 'options'=>$drugList,'label' => false,'type' => 'select')); 
                            echo $this->Form->input('RecordDrugUnit.dose', array('placeholder' => 'Dose', 'label' => false, 'class' => 'colLarge left')); 
                            echo $this->Form->input('RecordDrugUnit.unit_id', array('placeholder'=>'Unit', 'options'=>$unitList, 'label' => false,'type'=>'select', 'class' => 'colSmall right')); 
                            echo $this->Form->submit('EDIT', array('class' => 'button')); 
                        ?>
                    </div>
                </div>
                <div class="bit-2">
                    <div class ='padding background_color'>
                        <span class='smallSuper'>these fields are optional</span>
                        <br>
                        <span class='smallx colSmall left'>ROA: </span>
                        <?php
                            echo $this->Form->input('RecordDrugUnit.route_id', array('placeholder'=>'Unit', 'options'=>$routeList, 'label' => false,'type'=>'select', 'class' => 'colLarge right'));
                            echo $this->Form->input('Record.title', array('placeholder' => 'Title', 'label' => false,'type' => 'text'));
                            echo $this->Form->input('Record.report', array('placeholder' => 'Your Report','label' => false,'type' => 'textarea')); 
                        ?>
                    </div>
                </div>
            </div>
        </header>
    <?php echo $this->Form->end(); ?>

找到这个问题的解决方法了。

错误是我的编辑控制器正在发送一个put请求,而我的代码只检查post请求。

换行

if($this->request->is('post')) {

if($this->request->is('post') || $this->request->is('put')) {