weixin_33736649 2016-08-17 03:59 采纳率: 0%
浏览 83

更新数据记录onBlur

I am trying to update a record in a database onBlur but it does not work. Any ideas on how I can achieve this? my code looks like below.

JS

$(function () {
$("textarea").blur(function() {

       var meta_value =   $(this).val();
                var meta_key   =   $(this).attr("id");

             alert(meta_value + meta_key);


    $.get('script-url', { key: meta_Key, value: meta_value}, function(data) {

    });
});
});

PHP

<?php


if(isset($_POST['key']))
{
   $meta_value = $_REQUEST['value'];
   $meta_key = $_REQUEST['key'];


$servername = "hhh";
$username = "hhh";
$password = "hhh";
$dbname = "hhh";


 global $wpdb;
 $user_ID = get_current_user_id();  



// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$sql = "UPDATE wp_usermeta SET meta_value = $meta_value WHERE meta_key = $meta_key AND user_id= $user_ID";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}


}
?>
  • 写回答

2条回答 默认 最新

  • DragonWar% 2016-08-17 05:08
    关注

    Try this ;)

    You are sending key & value to server not meta_key;

    So update this condition

    if(isset($_POST['meta_key'])){
    

    to

    if(isset($_POST['key'])){
    
    评论
  • weixin_33716557 2016-08-17 12:20
    关注

    issue? So really you never get results trying get POST values from a GET request maybe use

    $.on('ready', function() {
      $("textarea").on('blur', (function() {
    
        var meta_value = $(this).val();
        var meta_key = $(this).attr("id");
    
        alert(meta_value + meta_key);
    
        $.post('script-url', {
          key: meta_Key,
          value: meta_value
        }, function(data) {
    
        });
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 存储过程或函数中的结果集类型变量如何使用。
  • ¥80 关于海信电视聚好看安装应用的问题
  • ¥15 vue引入sdk后的回调问题
  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信