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);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745281477a4620301.html
评论列表(0条)