• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Save to mysql and show text while typing onkeypress

xcislav

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

keyj.php:

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.
 
That won't work. It either needs Ajax or an appropriate JS framework which supports server communication. To achieve a mutual updating of model and view, AngularJS with it's two-way data-binding would perfect fit here.
Code:
<div ng-app>
  <input type="text" ng-model="data.message">
  <h1>{{data.message}}</h1>
</div>
http://jsfiddle.net/v75kL/
 
Zurück
Oben