不在php文件中运行JavaScript

时间:2016-12-28 06:04:55

标签: javascript php cakephp

我创建的代码在单击时,单选按钮状态将更改为挂起或完成,并在cakephp中更改数据库中的值。

但这有时不起作用。

这是我的代码:
运行JavaScript代码的.ctp文件

<div class="my-acc">
    <div class="my-acc-one">

        <div class="gap-top">&nbsp;</div>
        <?php echo $this->Form->create('ServiceSearch', array('id' => 'ServiceSearchForm', 'class' => 'regVerify', 'autocomplete' => 'off', 'novalidate' => 'novalidate', 'inputDefaults' => array('class' => '', 'div' => array('style' => 'width: 87%;margin-top: -22px;margin-left: 101px;')))); ?>
        <div>
            <div class="gap-top">&nbsp;</div>

            <div>
                <div class="gap-bottom gap-top"></div>
                <!-- -->
                <div class="sape">
                    <table class="table table-hover table-bordered" cellspacing="0" cellpadding="0">
                        <thead>
                            <tr>
                                <th width='20%'><?php echo $this->Paginator->sort('ExpertUser.expert_id', __('Expert')); ?></th>
                                <th width='20%'><?php echo $this->Paginator->sort('created', __('Created On')); ?></th>
                                <th width='15%'><?php echo $this->Paginator->sort('Consultation_Status', __('Status')); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($history)) { ?>
                            <tr>
                                <td colspan='6' class='text-warning'><?php echo __('No history found.') ?></td>
                            </tr>
                            <?php } else { ?>
                            <?php foreach ($history as $service):
                                if(!$service['Service']['price']) continue;
                                if($service['Service']['service_mode'] == 'email'){ ?>
                            <tr>
                                <td>
                                    <?php
                                        echo $this->Html->link($service['ExpertUser']['expert_id'], Router::url(array('controller' => 'experts', 'action' => 'profile', $service['ExpertUser']['expert_id']), true), array('escape' => false, 'target' => '_blank'));
                                        ?>
                                </td>
                                <td><?php echo showdatetime($service['Service']['created']); ?>&nbsp;</td>

                                <td>
                                    <div class="gap-bottom"></div>

                                    <p style="word-spacing: 50px;">
                                        <?php
                                            if ($service['Service']['status'] == 'pending' || $service['Service']['status'] == 'cancelled') {
                                            $selected = "pending";
                                            } else {
                                            $selected = "complete";
                                            }
                                            $options = array('pending' => 'pending', 'complete' => 'complete');
                                            $attributes = array('legend' => false, 'value' => $selected, 'class' => 'statusmy');
                                            echo $this->Form->create('ServiceStatus', array('id' => 'ServiceSatusForm','url' => array('controller' => 'experts', 'action' => "change_servicestatus", encrypt($service['Service']['id'])),'novalidate' => 'novalidate','inputDefaults' => array('class' => '', 'div' => array('class' => 'form-group'))));
                                            echo $this->Form->radio('status', $options, $attributes);
                                            echo $this->Form->end();
                                            ?>
                                    </p>
                                    <div class="gap-bottom">&nbsp;/div>
                                </td>
                            </tr> <?php } ?>
                            <?php endforeach; ?>
                        <?php } ?>
                        </tbody>
                    </table>
                    <!-- -->
                </div><!-- sape -->
                <div class="gap-bottom"></div>
                <div class="box-footer clearfix" align='right'>
                    <?php
                        echo $this->element('pagination');
                    ?><div class='clean'></div> <div class="gap-bottom"></div>
                </div>
            </div>
            <div class="gap-bottom"></div>
            <!-- -->
        </div>
    </div>
    <script type="text/javascript">
        $('input[type=radio][class=statusmy]').on('change', function () {
            $( "#ServiceSatusForm" ).submit();

        });
    </script>
   <style>
   </style>

现在在这个JavaScript的控制器中使用了这个方法

public function change_servicestatus($serviceId = null) {
    $this->layout = "ajax";
    if (isset($this->request->data['ServiceStatus']['status'])) {
        if ($this->request->data['ServiceStatus']['status'] == "pending") {
            $paid_status = "pending";
            $expert_status = 1 ;
        } else {
            $paid_status = "complete";
            $expert_status = 2 ;
        }
        $expert_id = $this->Session->read('Auth.ExpertData.Expert.id');
        $serviceId = decrypt($serviceId);
        $this->loadModel('Service');

        if ($serviceId) {
            $this->Service->id = $serviceId;

            $this->Service->recursive = -1;

            $service = $this->Service->findById($serviceId);
            if ($this->Service->saveField('status', $paid_status)) {
                $this->Message->setSuccess(__('Your status successfully change to ') . ucfirst($this->request->data['ServiceStatus']['status']) . '.', null, array('flashTitle' => 'Status Change successfully!'));
            } else {
                $this->Message->setWarning(__('Try again later!.'), null, array('flashTitle' => 'Try again later!'));
            }
        }
    } else {
        $this->Message->setWarning(__('Invalid Request!'), null);
    }
    $this->redirect($this->referer());
}

1 个答案:

答案 0 :(得分:0)

您可以使用所需输入的类或ID作为onclick:

$('.statusmy').on('change', function () {
    $( "#ServiceSatusForm" ).submit();
});

甚至为输入提供ID。