8bitrebellion
8bitrebellion

Reputation: 180

how to retrive value from jquery ui slider and store them into database?

Hi im new to javascript and ajax. my problem is when i move slider to any value,the value should be stored to database and a response should be back that value is stored. I have searched many questions previously asked here but still cant figure it out yet. So any help will be great.

here is my html code

<head>
<script>
$(function() {
    $( "#slider-range-max" ).slider({
        range: "max",
        min: 1,
        max: 10,
        value: 2,
        slide: function( event, ui ) {
            $( "#amount" ).val( ui.value );
        }
    });
    $( "#amount" ).val( $( "#slider-range-max" ).slider( "value" ) );

    $.ajax({
        type:"POST",
        url:"myslider.php",
        data:"vote="+$( "#slider-range-max" ).slider( "value" ),
        success:function(response) {
            alert(voted);
        } 
    })
});

</script>
</head>

and this is my myslider.php with connection of database,

<?php
require 'connection.php';
$vote="";

if (isset($_POST['slider'])) {
    $vote=$_POST['slider'];     
    mysqli_query("INSERT INTO slider (value) VALUES ('$vote')");
}
?>

Upvotes: 0

Views: 998

Answers (2)

hugofcampos
hugofcampos

Reputation: 695

You need to use the change callback for the slider. Then you can get the correct value and post it by jQuery Ajax.

$( ".selector" ).slider({
   change: function( event, ui ) {
      //here you can post data to PHP
   }
});

And Omikron43 is right. You need to check the parameter that you are expecting in php, because they do not match with what you are sending.

Upvotes: 2

fehnomenal
fehnomenal

Reputation: 509

You have to send the post request when your slider changes, i.e. in the slide function.

Also I think you are posting the wrong data to your script. I believe you are sending something like this vote=34 but are expecting something like this: slider=34.

Upvotes: 0

Related Questions