Update data record onBlur

I am trying to update a record in a database on blur but it is not working. Any idea how I can achieve this? my code looks like below.

$(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['meta_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;
}


}
?>

what is not working? The JS or the PHP? And what is the errormessage?

It does not update. Sometimes the old data still shows

You’re sending a GET request to the server, but you’re checking if isset($_POST['meta_key']) – which it never is.

That is an error but that still is not the problem.

JS variable names case-sensitive?

                var meta_key   =   $(this).attr("id");
    $.get('script-url', { key: meta_Key, value: meta_value}, function(data) {

So meta_key is not the same as meta_Key?

I believe that I have corrected that in my code and it does not still update.

Well, as I’ve said before on other recent threads, it’s going to be difficult for anyone to spot a problem in your code if the code you post isn’t the code you’re actually having trouble with.

1 Like

Let me show you how the code is now

$(function () {
    "use strict";
       var meta_key;
          var meta_value;
$("textarea").blur(function() {
    meta_value =   $(this).val();
    meta_key = $(this).attr("id");
                
             alert(meta_value + meta_key);

                
    $.get('http://url/chib-action/', {
                     key: meta_key, value: meta_value}, 
                        alert("success"),
                        function(data) {
      alert(data);
    });
});
});
<?php /* Template name: Nuffin */ get_header(); $options = get_option( 'theme_settings' ); ?>


<body id="<?php if ( is_front_page() ) { echo 'home'; } else echo 'other'; ?>" class="my-profile" >

<?php
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );

require_once( $parse_uri[0] . 'wp-load.php' );
$servername = "jhjh";
$username = "hjhj";
$password = "jhjh";
$dbname = "hjhjh";

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



 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 wpd_dusermeta SET meta_value = $meta_value, WHERE meta_key = $meta_key AND user_id= 10";

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

$conn->close();
}

?>

As said, earlier. Isn’t it more proper to use POST when updating data, rather than GET?

Which of your alert() messages appear?

all but the data alert. what should i do?

You could try adding this to the lower end of the PHP after you close the connection:

$conn->close();
}
// add this bit:
else {
  echo "didn't get POST var";
 }

That would tell you whether it’s the GET/POST thing. I don’t know much about JS, but my guess would be changing from $.get to $.post might make the difference.

Okay, let me try that.

@droopsnoot it still does not update

But does the data alert tell you anything?

Nothing from the data alert

OK, stick a random echo() at the start of the PHP, before it’s even tried to check the POST variable, see if that helps. If the PHP is running, it should at least display that if nothing else.

Also, is there any need for the html body tag in there, if it’s just a background update routine?

OKay, Let me try that.

The echos printer “what is today’s datedidn’t get POST var”

OK, so you can see that it’s running the “else” bit that you added in, so it’s not getting a value for $_POST['key'], hence not doing the update.

As I said before I’m not that familiar with JS and Ajax stuff, how do you decide that the POST variable is called ‘key’? Won’t the value be whatever the id of the changed textarea is? Instead of the initial echo() you added in, could you do:

var_dump($_POST);

and see what’s in the data alert?