two jquery-slider variables via ajax in php
Hello,
I have two jquery scripts. These are two range slider which creates values between 1 and 25 (level) and 1-4. (qualityId)
I send the values of these two sliders via ajax in the php_file.php. I need the values of the sliders get changed
dynamically in a link since I need to create a link to a json from where I echo out some informations via php.
So everytime a slider gets moved I want the both values gets changed dynamically and independently in a link without a page reload.
The problem
At the moment if a person moves one slider everything is working, the value in the link gets changed dynamically but if the second slider gets moved then the value from the first slider disappears. So my questions is how can I have two sliders on the page and send both values back to php.
Here are two screenshots for a better understanding. For better testing I just echo out the link on the screens.
If I change the second slider (qualityId) the values of the first slider (level) disappeears. They should be both respected.
So in this case of the screenshots there should be the level=9 and the qualityId=4 appear in the link.
Here is the
html_file.php
- <script>
- $( function() {
- $( "#slider-range-max" ).slider({
- range: "max",
- min: 1,
- max: 25,
- value: 1,
- stop: function(event, ui) {
- $("#amount").val(ui.value);
- /* Here we send the slider value towards PHP */
- $.ajax({
- method: "POST",
- url: "php_file.php",
- data: { sliderVal: ui.value, sliderVal2: ui.value }
- })
- /* Here we receive the data back */
- .done(function(data) {
- /* Here you can do whatever you want with the data */
- $("#content").html(data);
- });
- }
- });
- $( "#amount" ).val( $("#slider-range-max").slider("value") );
- });
- $( function() {
- $( "#slider-range-max2" ).slider({
- range: "max",
- min: 1,
- max: 4,
- value: 1,
- stop: function(event, ui) {
- $("#amount2").val(ui.value);
- /* Here we send the slider value towards PHP */
- $.ajax({
- method: "POST",
- url: "php_file.php",
- data: { sliderVal: ui.value, sliderVal2: ui.value }
- })
- /* Here we receive the data back */
- .done(function(data2) {
- /* Here you can do whatever you want with the data */
- $("#content").html(data2);
- });
- }
- });
- });
- </script>
- <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
- <div id="slider-range-max"></div>
- <input type="text" id="amount2" readonly style="border:0; color:#f6931f; font-weight:bold;">
- <div id="slider-range-max2"></div>
- <div id='content'></div>
And this is my
php file.php
- <?php
- $str = $_POST['sliderVal'];
- $quality = $_POST['sliderVal2'];
- $str2 = file_get_contents('https://example.com/stats/258?level='. $str .'&qualityId='. $quality .'&locale=en_EN');
- $json = json_decode($str2, true); // decode the JSON into an associative array
- $n= $json['health'];
- echo '<div class="profile-font2">', $n, '</div>';
-
- ?>
I think the mistake could be in the ajax- data value. I just send one value in each function, but don´t know how to fix it without destroying the both sliders.
Thanks for help.