Vishal Vishal - 4 months ago 9x
MySQL Question

Insert data in MySQL Database using AJAX on checkbox tick

Before getting stuck here, I had a look at the implementation of question asked here

I tried implementing the code and wish to insert data at checkbox click inside MySQL database. It might have been done already if I was supposed to insert data on form submission but I have to individually insert 8 bit data by turning their flag ON/OFF at checkbox tick.

I tried doing it, but don't know where exactly I am going wrong.

Below are the code snippets :

PHP CODE insertIntoAbbaa.php :

<!DOCTYPE html>

<meta charset="UTF-8">

$updateCon = mysqli_connect("", "uzer", "password", "remondb");
if (!$updateCon) {
die("not able to cconnect" . mysqli_error());

$firstData = mysqli_escape_string($updateCon, $_POST['firstOutputData']);

echo "nopes !";

$updateValue = mysqli_query($updateCon, "INSERT INTO update_abbaa(rfu_id, upadate_index, update_value) values(1,'$firstData',1)");

echo "nopes !";

echo "Saved !";
echo " nothing happened ! ";

Below is the onclick event I am calling on checkbox tick :

HTML onclick code snippet :

<span class="toggle">
<input type="checkbox" onclick="onOffFunction1()" id="onoff1">
<label data-off="Off" data-on="On"></label>

Now, I am trying to call the above mentioned function in javascript as follows :

function onOffFunction1() {
var firstCb = document.getElementById("onoff1");

if (firstCb.checked) {
document.getElementById("firstBitData").innerHTML = "1";
document.getElementById("firstBitData").style.backgroundColor = "green";
var firstOutputData = "1";

url: "insertIntoAbbaa.php",
type : "POST",
data : "firstOutputData"+firstOutputData,
success: function(data)
alert("success !" + data);


The output shows me "nothing happened" message as I have written in PHP script

Please go easy on me I am a beginner !


The error message 'Nothing happened' is caused because this check:


is false. That means there is no value inside $_POST with index 'firstOutputData'. The first place you need to look for an error like that is your AJAX call and if you pass the parameter correctly, which you don't.


data : "firstOutputData"+firstOutputData,

should be:

data : {firstOutputData: firstOutputData}