php - 通过 Ajax jQuery 改变背景颜色?

标签 php javascript jquery css ajax

场景:我的用户有自己的个人资料页面,具有不同的背景颜色和字体,我想使用 ajax 从某个用户那里检索颜色。即

$.ajax({ 
    type: "POST", 
    data: "id", 
    url: "ajax/css.php", 
    success: function (bg,font) { 
        $('#bg').css('background-color', 'bg');
        $('#font').css('font-color', 'font');
    } 

ajax/css.php页面

<?php

//retrieve the background and font data from database for the id(userID).

// this is the bit I'm stuck here, shall I echo the results or return them :~

?>

最佳答案

JSON 在这里可能是最简单的,就像这样:

$.ajax({ 
   type: "POST", 
   data: { id: someIDVariable }, 
   url: "ajax/css.php", 
   success: function (result) { 
     $('#bg').css('background-color', result.bg);
     $('#font').css('font-color', result.font);
   } 
});

或使用 $.getJSON() 的更短形式GET 是一个选项:

$.getJSON("ajax/css.php", { id: someID }, function (result) { 
  $('#bg').css('background-color', result.bg);
  $('#font').css('font-color', result.font);
});

然后在 PHP 中:

eacho json_encode(array('font'=>$font,'bg'=>$bg));
//which will echo this format: { "font": "Arial", "bg": "#000000" }

关于php - 通过 Ajax jQuery 改变背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4334600/

相关文章:

php - PHP 和 MySQL 中日期时间的区别

javascript - 使用 onsubmit 在 JS 中提交表单

javascript - FullCalendar - 将事件保存在 cookie 中

javascript - 防止 keydown() 被文档绑定(bind)捕获

jquery - 使用JQuery的clone()作为文本框的问题

javascript - Yii2:重复相关模型中的字段

php - 无法在 PHP 中连接到 MySQL

php - 对结果进行排序 mysql/php

javascript - 如何获得自动换色?

javascript - 如何在javascript中使用div id引用输入id