简体   繁体   中英

jQuery autosave running success function, but not updating MySQL

My jQuery autosave is running the success function, but not updating the MySQL database. What am I doing incorrectly?

jQuery:

function autosave() {
    var t = setTimeout("autosave()", 5000);

    var translation = $("#doc-translation").val();

    if (translation.length > 0) {
        $.ajax({
            type: "POST",
            url: "update-draft-submission.php",
            data: translation,
            cache: false,
            success: function() {   
                         $(".autosaved").empty().append("saved");
            }
        });
    }
} 

PHP:

<?php
session_start();
//retrieve our data
$iddoc = $_GET['iddoc'];
$trans = translation;
$transowner = $_SESSION['userid'];
$true = 1;
include "../dbconnect.php";
$query = "UPDATE translations
          SET trans='$trans'
          WHERE iddoc='$iddoc'
          AND transowner='$transowner'";
mysqli_query($query);
mysqli_close();

echo "Saved";
?>

You are not fetching the data in your PHP correctly:

$iddoc = $_GET['iddoc'];
$trans = translation;
  • iddoc is not passed as a GET parameter anywhere
  • "translation" is not a variable (neither do I think it is a constant)

Your SQL will break if it does not get the required values in the query.

Update your javascript so:

$.ajax(
 {
   type: "POST",
   url: "update-draft-submission.php",
   data: data: {translation:translation,iddoc:"XXX"},
   cache: false,
   success: function()
   {   
     $(".autosaved").empty().append("saved");
   }
 });

Replace XXX with your iddoc value.

Then in PHP fetch them as:

$iddoc = $_POST['iddoc'];
$trans = $_POST['translation'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM