AJAX HTML PHP question -
this scripts.js
function showhint(str) { if (str.length==0) { document.getelementbyid("txthint").innerhtml=""; return; } if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("txthint").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","inputprocess.php?q="+str,true); xmlhttp.send(); }
this html
<script type="text/javascript" src="scripts.js"></script> <form> type name here : <input type="text" onkeypress="showhint(this.value)" name="name" /> </form>
this php file
<?php
$q = $_get['q'];$dbc=mysql_connect("localhost","root","") or die (mysql_error()); mysql_select_db('input_oop') or die (mysql_error()); $sql = "insert users set name = '".$q."'"; mysql_query($sql) or die (mysql_error()); ?>
here problem: when type 1 value in textbox, save in database multiple times.
eg: input "jordan". when check in database, appears
userid 1 j userid 2 jo userid 3 jor
and on
onkeypress fire every keypress within box. here's happens:
- you type 'j'
- showhint() triggered, sends 'j' server
- the script inserts 'j' user table
- you type 'o', there 'jo' in text field
- showhint() triggered, sends 'jo' server
- the script insert 'jo' user table
- etc...
in other words, you're not showing hint @ all, you're blindly inserting whatever user types database.
if want show hints, should doing @ least 'select' query instead , returning results page.
you should use mootools or jquery ajax calls. they'll handle hard parts of building/sending request you, without having worry browser user's using.
as well, read about sql injection before releasing script such yours out wild.
you have mal-formed insert query. insert new record, basic syntax is:
insert sometable (field1, field2, field3, ...) values (value1, value2, value3, ...)
you've got mixed in sort of partial 'update' query in there, format of is
update sometable set field1=value1, field2=value2, ....
i can't see how query inserting database now, syntax broken.
Comments
Post a Comment