admin管理员组文章数量:1418134
scenario: my users have their own profile pages with different background colors and fonts, I want to retrieve the colors for example from a certain user using ajax. i.e.
$.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 page
<?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 :~
?>
scenario: my users have their own profile pages with different background colors and fonts, I want to retrieve the colors for example from a certain user using ajax. i.e.
$.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 page
<?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 :~
?>
Share
Improve this question
edited Jun 26, 2018 at 15:52
Jason Aller
3,65228 gold badges41 silver badges39 bronze badges
asked Dec 2, 2010 at 11:45
getawaygetaway
9,00023 gold badges66 silver badges96 bronze badges
2 Answers
Reset to default 4JSON would probably be easiest here, like this:
$.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);
}
});
Or a shorter form using $.getJSON()
is GET is an option:
$.getJSON("ajax/css.php", { id: someID }, function (result) {
$('#bg').css('background-color', result.bg);
$('#font').css('font-color', result.font);
});
Then in PHP:
eacho json_encode(array('font'=>$font,'bg'=>$bg));
//which will echo this format: { "font": "Arial", "bg": "#000000" }
Just make an action returning a valid JSON with the data you need. For instance if it returns:
{ color: "red", font:"arial"}
You can do:
$.post("user_css_info.json",{id:1234}, function(data){
alert("Color is" + data.color);
});
本文标签: phpchanging background color through Ajax jQueryStack Overflow
版权声明:本文标题:php - changing background color through Ajax jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745281477a2651446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论