2013-06-05 41 views
0

我正在创建一个文本框,其中的文本会自动更新到mysql后用户类型和提交。现在问题是文本框不会更新数据库。这是我参考下面的所有意见编辑的。仍然有一些问题,但比没有显示now.thanks好。使用jQuery更新文本框

这里是文本框中输入

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); 
?> 
+2

*旁注:*停止使用废弃的'mysql_ *'功能。改用MySQLi或PDO。 – Raptor

+0

双QUOT'“'缺少'$查询=” ...' –

回答

1

以下是你的代码

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

更改为

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

基本上当你通过值JavaScript函数如果传递的值字符串或整数而不是变量将它们括在单引号中,如getComment(' $ ID”,这个。值)

而且我添加了另一个变化是onkeypress事件中的onkeyup代替。这是因为onKeyPress事件将发生当按下某个键时并且您称为的功能将不会有您在中键入的最后一个字符。而当你松开按键即键入的字符后,所以你会得到onkeypress事件将触发事件所有字符在文本框中的MySQL查询中输入

同样总是在单引号像下面的值

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

希望你能理解这些变化

+0

感谢你的方法是正确的传递的话。非常感谢! – user2359110

+0

这是不好的....我给了我第一个答案和我在答复中给出的相同内容......并且在我们所有答案后36分钟你接受的答案是......你怎么能这样做,你接受了我答,并删除它,并给这个是36分钟末我们所有的ANS – Gautam3164

+0

@Gautam你在哪里正确的,但在使用onkeypress事件,你不会得到在文本框中的所有字符,因为用户输入的字符,即使之前的事件将触发 – Lepanto

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

应该

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

尝试,但仍然在数据库没有更新,甚至 – user2359110

1

您的查询有多个错误。它应该是:

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

mysql_*不应使用,因为它们已被弃用。改用MySQLi或PDO。注意SQL注入。

+0

感谢现在可以更新,但它返回的话评论 – user2359110

+0

请接受的答案,如果你觉得它有用。谢谢 – Raptor

1

尝试更换

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

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

编辑:

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

而在你抢的功能像

值3210

你需要不再次发送文本框的值id和尝试更新您的更新查询像

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

这里的$ id是Na dyou已经错过了在last.And关闭整数尝试使用mysqli_ *陈述或者PDO语句,而不是mysql_ *语句由于他们已被弃用

及主要的事情确保在数据库中注释栏应该是数据类型文本按您的评论

+0

什么原因downvote它.. ?? – Gautam3164

+2

就好onPressKey – Dineshkani

+0

Thanku @Dineshkani在任何情况下我已经更新了它....我甚至不认为事件....对不起为 – Gautam3164

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

您使用onPressKey代替onKeyPress