0

I want to save a background color of an element in a variable which I sent with an AJAX call, so I can use it in a PHP SESSION, but for some reason I cannot save this css in the variable.

var color_rgb   = $( "#config-steps #selected-color" ).css( 'background-color' );

My AJAX call works fine, I have tested to put a test string in color_rgb and this works fine.

$.ajax( {
    type: "POST",
    url: sbg.ajaxurl,
    data: {
        color_rgb: color_rgb,
        action: 'sbg_config'
    }

});

Has anyone an idea how to fix this?

Andresch Serj
  • 35,217
  • 15
  • 59
  • 101
Robbert
  • 1,270
  • 6
  • 30
  • 62

2 Answers2

1

Try this,

function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    return '#' + parts.join('');
}
var color_rgb = $("#config-steps #selected-color").css( 'background-color' );
$.ajax( {
    type: "POST",
    url: sbg.ajaxurl,
    data: {
        // set default black, if not color found, else convert it by hex function
        color_rgb: color_rgb ? hexc(color_rgb) : '#000000',
        action: 'sbg_config'
    }
});

Check the id of your selector. Is it exists or not.

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

Cast a variable to String in Ajax Call like this :

$.ajax( {
    type: "POST",
    url: sbg.ajaxurl,
    data: {
        color_rgb: String(color_rgb),
        action: 'sbg_config'
    }
});
theHarsh
  • 660
  • 5
  • 8