为什么使用jQuery更新文本框后数据库不会自动更新?


                    

我正在创建一个文本框,该文本在用户键入后无需提交便会自动更新为mysql。现在的问题是该文本框不会更新数据库。 这是我参考以下所有观点进行编辑的结果。虽然仍然存在一些问题,但是总比没有显示好。谢谢。</ p>

这是文本框输入</ p>

  echo“   </ td>”;
</ code> </ pre>

这是我的JavaScript </ p>

 函数getComment(id,txt_box)
{
var value = txt_box.value;

     $ .ajax({
         'url':'updatecomment.php',
         '数据':{“ id”:id,“ comment”:值},
         '成功':功能(响应){
             console.log(response);
             // TODO:使用服务器响应
         }
     });
}
</ code> </ pre>

最后是updatecomment.php </ p>

  <?php require_once(“ ../ includes / connection.php”)?>
  <?php require_once(“ ../ includes / functions.php”)?>

  <?php
      $ id = $ _ GET ['id'];
      $ comment = $ _GET ['comment'];

      $ query =“ UPDATE tblinternapplication SET评论='” .mysql_real_escape_string($ comment)。“'WHERE id = $ id”;
      $ result = mysql_query($ query,$ connection);
  ?>
</ code> </ pre>
     </ div>

展开原文

原文

我正在创建一个文本框,该文本在用户键入后无需提交便会自动更新为mysql,现在的问题是该文本框不会更新数据库 这是我参考以下所有观点进行编辑的结果,虽然仍然存在一些问题,但是总比没有显示好。

这是文本框输入:

  echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"getComment($id,this)\" /></td>";

我的JavaScript:

function getComment(id, txt_box)
{
var value = txt_box.value;

    $.ajax({
        'url': 'updatecomment.php',
        'data': {"id": id, "comment": value},
        'success': function (response) {
            console.log(response);
            //TODO: use server response
        }
    });
}

最后是updatecomment.php:

 <?php require_once("../includes/connection.php") ?>
 <?php require_once("../includes/functions.php") ?>

 <?php
     $id =$_GET['id'];
     $comment = $_GET['comment'];


     $query = "UPDATE tblinternapplication SET comment = '".mysql_real_escape_string($comment) ."' WHERE id = $id";
     $result = mysql_query($query, $connection);
 ?>

5个回答


以下是您的代码</ p>

  echo“   </ td>”;
</ code> </ pre>

将此更改为</ p>

  echo“   </ td>”;
</ code> </ pre>

基本上,当您传递javascript函数的值时,如果传递的值是字符串或整数而不是变量,则将它们括在单引号中,例如 getComment('$ id',this.value)</ strong> </ p >

我添加的另一个更改是onKeyup而不是onKeyPress。 这是因为 onKeyPress </ strong>事件将在按下键</ strong>时发生,并且您调用的函数将没有您键入的最后一个字符</ strong>。 而 onKeyPress </ strong>事件将在您释放键时触发</ strong>,即在键入字符后,您将在文本框中输入所有字符</ strong> < / p>

类似地,在Mysql查询中,值始终用单引号引起来,如下所示</ p>

  $ query =“ UPDATE tblinternapplication SET注释='” .mysql_real_escape_string($ comment)。“'WHERE id ='$ id'”;
</ code> </ pre>

希望您了解这些更改</ p>
     </ div>

展开原文

原文

Following is your code

  echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onPressKey=\"getComment($id,this.id)\" required/></td>";

Change this to

  echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyup=\"getComment('$id',this.value)\" required/></td>";

Basically when you pass values for javascript function if the value you pass is string or integer rather than variable enclose them in single quote like getComment('$id',this.value)

And another change that I added is onKeyup instead of onKeyPress. This is because onKeyPress event will happen when a key is pressed down and the function you called will not have the last character you keyed in. Whereas onKeyPress event will be triggered when you release the key i.e. after typing the character so you will get all the characters entered in the text box

similarly in Mysql query always enclose values in single quotes like following

     $query = "UPDATE tblinternapplication SET comment = '".mysql_real_escape_string($comment) ."' WHERE id = '$id'";

Hope you understand those changes


您的查询有多个错误。 应该是:</ p>

  $ query =“ UPDATE tblinternapplication SET注释='” .mysql_real_escape_string($ comment)。“'WHERE id = $ id”;
</ code> </ pre>

while mysql _ * </ code>不应使用,因为它们已被弃用。 改用MySQLi或PDO。 还要注意SQL注入。</ p>
     </ div>

展开原文

原文

Your query has multiple errors. It should be:

$query = "UPDATE tblinternapplication SET comment = '".mysql_real_escape_string($comment) ."' WHERE id = $id";

while mysql_* should NOT be used as they are deprecated. use MySQLi or PDO instead. Watch out for SQL Injection too.


  echo“   </ td>”;
</ code> </ pre>

您使用 onPressKey </ code>代替 onKeyPress </ code> </ p>
     </ div>

展开原文

原文

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"javascript: return getComment($id,this.value);\" required/></td>";

You use onPressKey instead of onKeyPress


尝试替换</ p>

  echo“   </ td>”;
</ code> </ pre>

使用</ p>

  echo“   </ td>”;
</ code> </ pre>

编辑:</ strong> </ p>

  echo“   </ td>”;
</ code> </ pre>

然后在您的函数中获取类似的值</ p>

  function getComment(txt_box,comment){
     var value = txt_box.value;
}
</ code> </ pre>

您需要再次发送文本框值而不是ID,并尝试更新更新查询,例如</ p>

  $ query =“ UPDATE tblinternapplication
           SET comment =“ .mysql_real_escape_string($ comment)。”
           WHERE id = $ id“;
</ code> </ pre>

这里$ id是一个整数,您最后一次错过了关闭“ </ strong>的权限。由于不推荐使用mysqli_ *语句或pdo语句而不是mysql_ *语句,因此请尝试</ p>

主要要确保根据您的注释,数据库中的注释列应为数据类型文本</ strong> </ p>
     </ div>

展开原文

原文

Try to replace

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onPressKey=\"getComment($id,this.id)\" required/></td>";

with

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"getComment($id,this.value)\" required/></td>";

EDIT:

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"getComment($id,this)\" required/></td>";

And at your function grab the value like

function getComment(txt_box, comment) {
    var value = txt_box.value;
}

You need to send the textbox value not again the id and try to update your update query like

$query = "UPDATE tblinternapplication 
          SET comment = ".mysql_real_escape_string($comment) ."
          WHERE id = $id";

Here $id is an integer na dyou have missed closing " at the last.And try to use mysqli_* statements or pdo statements instead of mysql_* statements due to they are deprecated

AND main thing make sure that comment column in DataBase should be data type text as per your comment


  echo“   </ td>”;
</ code> </ pre>

应该是</ p>

  echo“   </ td>”;
</ code> </ pre>
     </ div>

展开原文

原文

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onPressKey=\"getComment($id,this.id)\" required/></td>";

should be

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onkeypress=\"getComment($id,this.id)\" required/></td>";

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐