weixin_33720956 2017-11-25 18:06 采纳率: 0%
浏览 51

Ajax选择列表

I want to retrive the result of this kind of data list with CakePHP 3

<?= $this->Form->select('notif_message', 
    [ 'oui' => 'oui', 'non' => 'non'], array('id' => 'notifmess')); ?>
<?= $this->Form->hidden('notifmessage', ['value' => $notif_message]) ;?>

The goal is when a user chosse a value, an Ajax call to this controller be done

public function notifmessage() // mise à jour des paramètres de notifications 0  = non, 1 = oui
{

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

    $notifmessage = $this->request->data('notifmessage');

    if($notifmessage == 'oui')
    {
        $new_notif_message = 'non';
    }
    else
    {
         $new_notif_message = 'oui';
    }

    $query = $this->Settings->query()
                        ->update()
                        ->set(['notif_message' => $new_notif_message])
                        ->where(['user_id' => $this->Auth->user('username') ])                            
                        ->execute();

    $this->response->body($new_notif_message);
    return $this->response;

    }
}

And i would like to do this call in Ajax without reloading , i have this script

<script type="text/javascript">
$(document).ready(function() {
    $('.notif_message').change(function(){
        $.ajax({
            type: 'POST',
            url: '/settings-notif_message',
            data: 'select.notif_message' + val,
            success: function(data) {
                alert('ok');
            },
            error: function(data) {
                alert('fail');
            }
        });
    });
});
</script>

he doesn't work, nothing happend but i don't know why, i don't have any message in log, i can't debug without indication what doesn't not work

Thanks

  • 写回答

2条回答 默认 最新

  • 七度&光 2017-11-25 18:25
    关注

    In yout javascript you should use $('#notifmess').change(… or $('[notif_message]').change(… instead of $('.notif_message').change(….
    In CakePHP the first argument of the select method will be used as the name attribute of the select tag.

    Update: In your controller you are retrieving the value of $_POST['notifmessage'], which is the name of the hidden input field. To get the user's choice you either should use $this->request->data('notif_message'); in the controller, or setting up the ajax request to send the data with notifmessage like so:

    $('[name="notif_message"]').change(function(){
        $.ajax({
            type: 'POST',
            url: '/settings-notif_message',
            data: {'notifmessage' : this.value},
            success: function(data) {
                // To change selected value to the one got from the server
                $('#notifmess').val(data);
    
                alert('ok');
            },
            error: function(data) {
                alert('fail');
            }
        });
    });
    

    (Where in this case this is referring to <select> tag.)

    评论

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题