J.Alan
J.Alan

Reputation: 85

Jquery Slider in Php / Laravel Blade

I need to use the variable $currentSliderCount, which holds the current slider value in the second statement of my for loop, instead of $chCount I want to pass $currentSliderCount how to accomplish this?

I use the jquery ui slider.

// filename = slider.blade.php

$currentSliderCount = $('#number_of_chapters').val();

for(var i = 0; i < $currentSliderCount; i++) {
    // php loop blade syntax

}

@for ($i = 0; $i < $chCount; $i++)
    $('#sliderAppendNumCh').append('...');
@endfor

I tried it with ajax like this:

    change : function(e, slider) {
        $.ajax({
            type: "POST",
            url: "/",
            data: slider.value,
            success: function() {

                // add chapters
                $('#sliderAppendNumCh').empty();
                for(var i = 0; i < $currentSliderCount; i++) {

                }

                @for ($i = 0; $i < $chCount; $i++)
                    $('#sliderAppendNumCh').append('...');
                @endfor
            }
        }
    });
    },

But I do not know how I can use that value from ajax.

Upvotes: 0

Views: 815

Answers (1)

Jesse de gans
Jesse de gans

Reputation: 1654

Server & client side

Web development is all about communication. In this case, communication between two (2) parties, over the HTTP protocol:

  • The Server - This party is responsible for serving pages. (PHP in your case)
  • The Client - This party requests pages from the Server, and displays them to the user. In most cases, the client is a web browser (html/ css / JavaScript).

Each side's programming, refers to code which runs at the specific machine, the server's or the client's.

So the code on your server(PHP) doesn't know about your variable on the client side.

As far as i can see you can solve your problem with just using Javascript as followed:

for(var i = 0; i < $currentSliderCount; i++) {
    for (var d = 0; d < $currentSliderCount; d++)
      {
        $('#sliderAppendNumCh').append('...');
      }
}

Tell us about the bigger picture if this solution doesn't work for you.

Edit

You could try passing the PHP variable to Javascript like so:

var chCount = <?php echo $chCount;?> ;

Edit 2 if you want to use AJAX

First you need to create a new laravel route which returns the ChCount variable. Then do a ajax post and execute code on success like so:

change : function(e, slider) {
        $.ajax({
            type: "POST",
            url: "/getChCount",
            data: slider.value,
            success: function(data) {
            // add chapters
            $('#sliderAppendNumCh').empty();
            for(var i = 0; i < $currentSliderCount; i++) {
                for (var d = 0; d < data; d++){
                    $('#sliderAppendNumCh').append('...');
                }
            }
        }
    }
});
},

Upvotes: 1

Related Questions