当选择框onchange时更新mysql表中的值

编程入门 行业动态 更新时间:2024-10-24 08:27:28
本文介绍了当选择框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表中的值

本文发布于:2023-10-30 19:20:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:onchange   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!