当选择框onchange时更新mysql表中的值 [英] Update value in mysql table when select box onchange

查看:77
本文介绍了当选择框onchange时更新mysql表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html表,其中包含我的mysql数据库表中的值。客户要求对我的html表中的数据进行前端编辑。因此,我单击时将td转换为选择框,用户将在其中选择X和O注释。

I have a html table that contains values in my mysql db table. Client ask for a front-end editing of data in my html table. So i make td transform to selection box when click, where the user will choose X and O remarks.

这是我的脚本:

 $(document).on('click', 'td', function() { ////---make td transform to dropdown list box when click---///
          if($(this).find('select').length == 0) {
              $(this).empty();  //clears out current text in the table
              $(this).append('<select onchange="myFunction()" id="Remarks" name="Remarks"><option value=""></option><option <?php if ($Remarks=='X') echo 'selected';?> value="X" style="font-size:20px;font-weight:bold;">X<option style="font-size:20px;color:green;font-weight:bold;" <?php if ($Remarks=='O') echo 'selected';?> value="O">O</select>');
          }
    });


    $(document).on('focusout', 'td select', function(){ 
        var myValue = $(this).val();
        var $parent = $(this).parent();
        $(this).remove();
        $parent.append(myValue);        
    });

我需要根据用户从选择中选择的内容来更新td的值

What I Need is to make update for the value of td based on what the user choose from the selection box.

这是我尝试过的方法:对选择框进行更改

This is what I have tried : Making an onchange for the selection box

function myFunction(){
        var emp_name = document.getElementById('employeeName').value;
        var r = document.getElementById('Remarks').value;
                $.ajax({
                    type: 'post',
                    url: 'update_data.php',
                    data: {
                        'employeeName' :emp_name,
                        'DAY1' : r
                    },
                    success: function(data){
                        $("#content").html(data)
                        $(".loader").fadeOut("veryslow");               
                        $("#content").hide().fadeIn(500)  
                        //alert(r); 
                        //alert(emp_name);
                    },
                    error:function(data){
                        alert('Failed');
                    }
                })

    };

这是我的update_data.php:

and this is my update_data.php :

<?php
    $employeeName = $_REQUEST["employeeName"];
    $Remarks = $_REQUEST["Remarks"];
    //$id = $_REQUEST["id"];

    try {
        $pdo = new PDO('mysql:host=localhost:***;dbname=******;', '*****', '*****' );
        $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $pdo->query( 'SET NAMES UTF8' );
        $stmt = $pdo->prepare(
            "UPDATE `mbwa`
            SET
                `DAY1` = :Remarks
            WHERE
                `employeeName` = :employeeName
            "
        );
        $stmt->bindValue(':employeeName',$employeeName,PDO::PARAM_STR);
        $stmt->bindValue(':Remarks',$Remarks,PDO::PARAM_STR);
        //$stmt->bindValue(':id',$id,PDO::PARAM_STR);
        $stmt->execute();
        header('location:./');
    } catch ( PDOException $e ) {
        var_dump( $e->getMessage() );   
    }

    $pdo = null;
    ?>

它更新数据库,但是它给出了我更改的td值的空值。我认为它在更新查询中没有获得':Remarks'的值。

it updates the database but it gives a value of null in value of the td that i make change. I think it doesn't get the value of ':Remarks' in my update query.

有帮助吗?

推荐答案

检查 $ _ REQUEST [备注]; 应该为 $ _ REQUEST [ DAY1];

因为在Ajax中发送的数据是这样的:

Because your data in Ajax are sended like this:

data: {
       'employeeName' :emp_name,
       'DAY1' : r
},

因此,在 PHP 中,您必须将其更改为:

So, in the PHP you have to change it to this:

$employeeName = $_REQUEST["employeeName"];
$Remarks = $_REQUEST["DAY1"];

这篇关于当选择框onchange时更新mysql表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆