drs3925 2016-06-01 06:20
浏览 138
已采纳

在复选框勾选中使用AJAX在MySQL数据库中插入数据

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>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php

    $updateCon = mysqli_connect("192.168.0.7", "uzer", "password", "remondb");
    if (!$updateCon) {
        die("not able to cconnect" . mysqli_error());
    }
    if(isset($_POST['firstOutputData'])){

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

        if(!$firstData){
            echo "nopes !";
        }

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

        if(!$updateValue){
            echo "nopes !";
        }

        mysqli_close($dbget);
        echo "Saved !";
        }
    else{
        echo " nothing happened ! ";
    }
    ?>
</body>

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>
</span> 

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";

 $.ajax({
       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 !

  • 写回答

1条回答 默认 最新

  • draw62188 2016-06-01 06:33
    关注

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

    if(isset($_POST['firstOutputData']))

    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.

    This:

    data : "firstOutputData"+firstOutputData,

    should be:

    data : {firstOutputData: firstOutputData}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀