2013-06-05 38 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