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.)

    评论

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败