I have tried number solutions and cannot figure out what I am doing wrong.
I have the following input element:
<input type="checkbox" data-layout="fixed" class="pull-right" value="1">
just before my closing body tag I have the following jquery
<script>
$("input[type='checkbox']").on('click', function(){
var checked = $(this).attr('checked');
if(checked){
var value = $(this).val();
$.post('mysqlfunc.php', { value:value }, function(data){
if(data == 1){
}
});
}
});
</script>
In mysqlfunc.php file I have
<?php
include "../../inc/config.php";
include "../../inc/funcs.php";
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die( "Unable to select database");
include "../../inc/userauth.php";
if ($_POST && isset($_POST['value'])) {
$value = mysql_real_escape_string($_POST['value']);
$sql = "UPDATE members SET fixed = '".$value."' WHERE username='".$_SESSION['uid']."'";
} else {
}
?>
My expectation is when I click the input checkbox, a value "1" is saved into the "members" table in the "fixed" column where the username is equal to the session uid.
I can echo the uid, so that is working. Its not giving any errors, so its connecting to the database it seems. Yet checking the input textbox, does not change the value in the database.
I get no js errors at all, I get no php errors, just not saving.
What am I doing wrong? Example working code if at all possible would be very helpful.
This line in your script is wrong.
var checked = $(this).attr('checked');
change it to
var checked = $(this).is(':checked')
You forget to execute your query use mysql_query
$sql = "UPDATE members SET fixed = '".$value."' WHERE username='".$_SESSION['uid']."'";
mysql_query($sql);
What version of jquery you used ? And check if post request really done or not Try using .prop
:
$("input[type='checkbox']").on('click', function(){
var checked = $(this).prop('checked'); //<---- Here
if(checked){ // here returned true or false
var value = $(this).val();
$.post('mysqlfunc.php', { value:value }, function(data){
if(data == 1){
}
});
}
});
or....
$("input[type='checkbox']").on('click', function(){
if( this.checked ){ // <-- just checked directly
var value = $(this).val();
$.post('mysqlfunc.php', { value:value }, function(data){
if(data == 1){
}
});
}
});
And like @Saty said, put mysql_query($sql);
to execute those query.
Your JS code will look like.
$(document).ready(function(){
$("input[type='checkbox']").on('click', function(){
var checked = $(this).is(':checked');
if(checked){
var value = $(this).val();
$.post('mysqlfunc.php', { value:value }, function(data){
if(data == 1){
}
});
}
});
});
And You missed to execute SQL so, your PHP code will look like.
<?php
include "../../inc/config.php";
include "../../inc/funcs.php";
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die( "Unable to select database");
include "../../inc/userauth.php";
if ($_POST && isset($_POST['value'])) {
$value = mysql_real_escape_string($_POST['value']);
$sql = "UPDATE members SET fixed = '".$value."' WHERE username='".$_SESSION['uid']."'";
mysql_query($sql);
} else {
}
?>