Search code examples
phpjqueryjsonjquery-ui-slider

jQuery initialize Slider with data from JSON request (returned from php script)


at this moment i'm trying to initialize a jQuery Slider with data i've got from a PHP-SQL query encoded to JSON format.

PHP side is working, but i'm struggling with parsing the value (in the future there are more values to parse) to a variable and to push it as an initial value to the Slider.

Here are the functions and things i've done so far:

JS:

function schieber(){   

var handle = $("#custom-handle");

$("#slider-aktualisierungsrate").slider({
  min: 1,
  max: 60,        
  create: function() {
    handle.text( $( this ).slider( "value" ) );
  },
  slide: function( event, ui ) {
    handle.text( ui.value );
  }  
});}

function getSettings(){

var settings = [];

$.getJSON("/php/settings.php", function(data){

    $.each(data, function(index, value) {
       settings.push(parseInt(data.Aktualisierungsrate)); 
    });
    //settings = data;
    $("#slider-aktualisierungsrate").slider("value", settings[0]);
});}

This doesnt work, i call getSettings after Initializing the slider. Maybe there's a method to parse the JSON-Data inside the create-function of the slider.

Further I do not just want to initialize the slider, I want to push updated values back to the SQL database with another php script.


Solution

  • Got it working with this solution:

    function schieber(){
    
    
    var init = [];
    var handle = $("#custom-handle");
    
    $.getJSON("/php/settings.php", function(data){
    
        $.each(data, function(index, value) {
           init.push(parseInt(value.test)); 
        });
    
        $("#slider-aktualisierungsrate").slider({
          min: 1,
          max: 60,
          value: init,
          create: function() {
                handle.text( $( this ).slider( "value" ) );    
          },
          slide: function( event, ui ) {
            handle.text( ui.value );
          }  
        });
    
    });}