Save to mysql and show text while typing onkeypress

2 replies
The idea is simple. A key you press (a letter) appears in updated page:

keyj.php:

<input id='k'></input>
<script type="text/javascript">
document.getElementById('k').onkeypress=function()
{
<?php
$db=new PDO('mysql:host=localhost;dbname=test;charset=utf8 ','$us','$ps');
$db->exec("UPDATE TXT SET texts=k.value");
$rl=$db->query("SELECT texts FROM TXT");
print_r($rl->fetchColumn());
?>
}
</script>


The intention is to have text on the screen via print_r. I successfully used it in another php to exchange information with MySQL and users screen (worked).
Here - the more you type and the more k.value is the more text will be on screen (just from server).

Unfortunately something went wrong.
#javascript #mysql #onkeypress #pdo #php #save #show #text #typing
  • Profile picture of the author David Beroff
    I'm not entirely sure why you would need to send the data to and then from the database server here. You might want to take a look at jQuery; that may help you do what you're trying to do without involving a database.
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[9105317].message }}
    • Profile picture of the author Paul Gordelo
      David is right,

      You should use jquery to pass your data on keypress. here are steps.

      1. create update.php which will handle data to insert into database and return entered string.
      2. jquery code
      var parameters='keyword=' + $(this).val();
      $.ajax({type:"POST",url:"/update.php",data:parameters,
      success:function(data,textStatus,XMLHttpRequest){
      //success code
      },
      error:function(xhr,textStatus,errorThrown){
      //error code
      } });
      3. get the success code from update.php
      Signature
      {{ DiscussionBoard.errors[9106912].message }}

Trending Topics