0

I'm creating a textbox where the text will automatic update into mysql after user type and without submit.Problem now is the textbox doesn't not update the database. This is what i edited by refering to all the opinion below.yet still have some problem but is better than nothing display now.thanks.

here is the textbox input

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

here is my 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
        }
    });
}

and last is 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
  • 2
    sidenote: stop using deprecated mysql_* functions. use MySQLi or PDO instead. Commented Jun 5, 2013 at 8:57
  • Double quot " is missing for $query = "... Commented Jun 5, 2013 at 9:09

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks your method is correct deliver the words. thank you so much!
This is not good....I gave the ans first and the same content that I gave in my ans...and your accepted ans is 36minutes after all of our ans....how can you do that.you accepted my ans and removed it and given to this which is 36min late of all our ans
@Gautam you where correct but on using onKeyPress you will not get all characters in the textbox since the event will trigger even before the user enters the character
1

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.

Comments

1
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

Comments

1

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

11 Comments

Is the any event like onPressKey
Thanku @Dineshkani I have updated it....I even not considered that event....Sorry for that
thanks, i tried to change to this.value but seem like nothing happen
Or do one thing....send this to the function and get the value there....See my edit
thanks now able to read what i'm typing in but still got a minor problem let say i type "test test" the sql only save "test tes"
|
0
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>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.