DeAztek DeAztek - 3 months ago 57
Javascript Question

jQuery Knob and POST request from JavaScript

How to form a standard

POST
request with a parameter with JavaScript and jQuery Knob?

Here is what I got:

index.html
:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="jquery.knob.min.js"></script>
</head>

<body>
<input type="text" data-angleoffset=-125 data-anglearc=250 data-fgcolor="#66EE66" value="50" class="dial">

<script>
$(".dial").knob({
'release' : function (sendpostresp) {
$.ajax({
url: "publish.php", //the page containing php script
type: "POST", //request type
success: function (result) {
alert(result);
}
});
}
});
</script>

</body>
</html>


I need to save the value of the knob on release. The PHP part waits for the
POST
and saves the value to a database, and it works.

In my opinion, my code should send the current value of knob to the PHP script with
POST
. But I see no parameters in console, just an empty
POST
response.

Unfortunately, the official jQuery Knob docs don’t provide sufficient instructions. Please, help me with code examples, how to send the current value from the jQuery knob through
POST
?

Answer

You just need to add a data property to your ajax request. It could look like this:

$(".dial").knob({
    'release' : function (sendpostresp) {
        $.ajax({
            url: "publish.php", //the page containing php script
            type: "POST", //request type
            data: { 
                foo: sendpostresp 
            },
            success: function (result) {
                alert(result);
            }
        });
    }
});

According to Knob docs, the parameter in the 'release' callback function is the current value of the dial. Simply stick that into your data object on the ajax request and it will be sent to your server-side code that handles the request