AJAX HTML PHP问题 [英] AJAX HTML PHP question
问题描述
function showHint(str)
{
if(str.length == 0)
{
document.getElementById(txtHint)。innerHTML =;
return;
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为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();
}
这是我的HTML
< form>
在此输入您的姓名:< input type =" text" onkeypress事件= QUOT; showHint(THIS.VALUE)"命名= QUOT;名称" />
< / form>
这是我的PHP文件
<?php
$ q = $ _GET ['q'];
$ dbc = mysql_connect( localhost,root,)或die(mysql_error());
mysql_select_db('input_oop')或死(mysql_error());
$ sql =" INSERT INTO users set name ='"。$ q。"'";
mysql_query($ sql)或者死(mysql_error());
?>
下面是问题:当我在文本框中只输入一个值时,它会保存在我的数据库多次。
EG:我输入Jordan。当我检入数据库时,它显示为:
userid 1 J
userid 2 Jo
userid 3 Jor
等等
onkeypress会为每个按键启动,即使您在该方框内执行操作。所以发生了什么:
- 输入'j'
- showHint 'j'到服务器
- 脚本将'j'插入到您的用户表中
- 键入'o',现在有'jo'在文本字段中触发
- showHint(),并向服务器发送'jo'
- 脚本在用户表中插入'jo' / li>
- etc ...
换句话说,你根本没有提示,你只是盲目地插入任何用户类型到你的数据库中。
如果你想显示提示,那么你应该至少使用'SELECT'查询并返回结果到您的页面。
您还应该使用类似 Mootools 或 jQuery 来执行您的AJAX调用。他们会处理构建/发送请求的困难部分,而不必担心用户使用的浏览器。
同样,阅读about SQL注入在将脚本发布到野外之前。
您还有一个错误的插入查询。插入新记录的基本语法如下:
pre $ INSERT INTO sometable(field1,field2,field3,...)VALUES (value1,value2,value3,...)
'update'查询,其格式为
UPDATE sometable SET field1 = value1,field2 = value2,... 。
我无法看到您的查询如何将任何内容插入到数据库中,因为语法完全被破坏。
This is my scripts.js
function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for 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 is my HTML
<script type="text/javascript" src="scripts.js"></script> <form> Type your name here : <input type="text" onkeypress="showHint(this.value)" name="name" /> </form>
This is my 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 INTO users set name = '".$q."'"; mysql_query($sql) or die (mysql_error()); ?>
Here is the problem: When I type only one value in the textbox, it will save in my database multiple times.
EG: I input "Jordan". When I check in my database, it appears as
userid 1 J
userid 2 Jo
userid 3 Jor
And so on
onkeypress will fire for EVERY keypress even you do within that box. So here's what happens:
- You type 'j'
- showHint() is triggered, sends 'j' to the server
- The script inserts 'j' into your user table
- You type 'o', there is now 'jo' in the text field
- showHint() is triggered, sends 'jo' to the server
- The script insert 'jo' into your user table
- etc...
In other words, you're not showing a hint at all, you're just blindly inserting whatever the user types into your database.
If you want to show hints, then you should be doing at LEAST a 'SELECT' query instead and returning the results to your page.
You should also use something like Mootools or jQuery to do your AJAX calls. They'll handle the hard parts of building/sending the request for you, without having to worry about what browser the user's using.
As well, read about about SQL injection before releasing a script such as yours out into the wild.
You also have a mal-formed insert query. To insert a new record, the basic syntax is:
INSERT INTO sometable (field1, field2, field3, ...) VALUES (value1, value2, value3, ...)
You've got mixed in some sort of partial 'update' query in there, the format of which is
UPDATE sometable SET field1=value1, field2=value2, ....
I can't see how your query could be inserting anything into the database as it is now, as the syntax is completely broken.
这篇关于AJAX HTML PHP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!