php - changing background color through Ajax jQuery? - Stack Overflow

scenario: my users have their own profile pages with different background colors and fonts, I want to r

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
Add a ment  | 

2 Answers 2

Reset to default 4

JSON 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

相关推荐

  • php - changing background color through Ajax jQuery? - Stack Overflow

    scenario: my users have their own profile pages with different background colors and fonts, I want to r

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信