duangang4940 2017-09-05 15:14
浏览 36
已采纳

如何使用AJAX将DropDown保存到数据库中

I've looked really hard on this but I can't get my head around AJAX working with PHP.

This is what I have and when a user clicks on the dropdown I would like it to save into my database

<select>
  <?php $taskStatus = "SELECT * FROM task_status WHERE used = 1 ORDER BY id ASC ";
   $taskresults = $conn->query($taskStatus) or die(mysqli_error($conn));
   while($taskStatusRow = mysqli_fetch_assoc($taskresults)) {
     echo " <option  value= ". $taskStatusRow['name'] ." >". $taskStatusRow['name'] ." </option>";
   }
  ?>
</select>

And this is the query i'd like to run:

INSERT INTO snagging (taskstatus, updated_at) 
WHERE ID = 1234 
VALUES taskStatusRow['name'], $now);
  • 写回答

1条回答 默认 最新

  • doulu8341 2017-09-05 15:44
    关注

    I'll give you a overall flow of AJAX here. I tried to provide comments so as to show the control flow.

    <select id="selectOption">  //******* Assign an ID
        <?php $taskStatus = "SELECT * FROM task_status WHERE used = 1 ORDER BY id ASC ";
        $taskresults = $conn->query($taskStatus) or die(mysqli_error($conn));
        while($taskStatusRow = mysqli_fetch_assoc($taskresults)) {
    
            echo " <option  value= ". $taskStatusRow['name'] ." >". $taskStatusRow['name'] ." </option>";
    
        }
        ?>
    </select>
    

    jQuery + AJAX

    $(document).ready(function() {
        $("#selectOption").change(function(){ //** on selecting an option based on ID you assigned
            var optionVal = $("#selectOption option:selected").val(); //** get the selected option's value
    
            $.ajax({
                type: "POST", //**how data is send
                url: "MYPROCESSPAGE.php", //** where to send the option data so that it can be saved in DB
                data: {optionVal: optionVal }, //** send the selected option's value to above page
                dataType: "json",
                success: function(data){
                    //** what should do after value is saved to DB and returned from above URL page.
                }
            });
        }); 
    });
    

    Inside your MYPROCESSPAGE.php, you can access the data passed via AJAX like:

    <?php
    
    $selectedOptionVal = $_POST['optionVal'];
    
    //DB CONNECTION STEPS
    .
    .
    .
    // You are trying to "UPDATE" a table data based on some ID and not inserting. Included both operations
    
    // If you are INSERTING A new table entry, use below code.
    //INSERT INTO snagging (taskstatus, updated_at) VALUES ('$selectedOptionVal', 'Now()');
    
    // If you are UPDATING an existing table entry, use below code.
    //UPDATE snagging SET taskstatus = '$selectedOptionVal', updated_at = 'Now()' WHERE ID = 1234;
    
    ?>
    

    Hope it's helpful.

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

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题