我想以檢索這種數據列表的結果與CakePHP的3阿賈克斯選擇列表
<?= $this->Form->select('notif_message',
[ 'oui' => 'oui', 'non' => 'non'], array('id' => 'notifmess')); ?>
<?= $this->Form->hidden('notifmessage', ['value' => $notif_message]) ;?>
的目標是當用戶chosse值,Ajax調用這個控制器來完成
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;
}
}
,我想這樣做在阿賈克斯這一呼籲沒有重裝,我有這個腳本
<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>
他不工作,沒有什麼^ h追加,但我不知道爲什麼,我沒有在日誌中的任何消息,我不能沒有指示調試什麼不不行
感謝
什麼是您的select元素生成的值,因爲這些值是您需要使用的值。 – jeff