jQuery guardado automático ejecutando la función de éxito, pero sin actualizar 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";
?>

preguntado el 24 de agosto de 12 a las 23:08

Are you getting an error from the mysql query? If not, then it's probably not affecting any rows. Use mysqli_affected_rows or enter a test query directly into mysql. -

Hmm.. I ran this in my SQL browser and it's getting an error on line 1: "UPDATE translations SET trans='lkjh' WHERE iddoc='25' AND transowner='hubrid'" -

1 Respuestas

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");
   }
 });

Reemplaza XXX con tu iddoc .

Then in PHP fetch them as:

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

Respondido 25 ago 12, 00:08

Isn't translation defined as the value of my textarea in my js? If not, how can I pass it over? I thought I was. And iddoc is passed as a GET parameter in the URL? - señor_jueves

I have updated the answer with code on how to pass the values in Javascript and how to fetch them in PHP - Raidenace

Great it works! Just having trouble grabbing the iddoc from my URL in ,iddoc:"XXX"}, parte. - señor_jueves

el valor de XXX no debe be in quotes. It should be XXX no "XXX". Also, do you mean you cannot get the value to replace inside XXX? - Raidenace

Update the javascript line like this: data: {translation:translation,iddoc:<?php echo $_GET['iddoc'];?>}, - Raidenace

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.