如何使用 mySQL 在移动设备上发出 Ajax 请求
我是 ajax 新手,我想让所有文本框都提交到我的 MySQL 数据库,而无需更新页面。
如果我使用 js 捕获文本框的值作为参数“str”(“type”只是我正在使用的另一个参数),那么效果很好
function sendAjax(type, str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("txtResp").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("txtResp").innerHTML=xmlhttp.responseText;
}
}
switch(type)
{
case 'search':
xmlhttp.open("GET","mysql_process_search.php?q="+str,true);
break;
case 'add':
xmlhttp.open("GET","mysql_process_event.php?q="+str,true);
break;
}
xmlhttp.send();
}
并且通过页面 mysql_process_event.php 我在 < 上获得了正确的结果em>桌面浏览器使用:
echo $_GET['q'];
$q = $_GET['q'];
$con = mysql_connect('xxx', 'xxx', 'xxx');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xxx", $con);
$result = mysql_query("SELECT * FROM Events");
mysql_query("INSERT INTO Events (Title)
VALUES ('".$q."')");
mysql_close($con);
但这不适用于移动 Safari 或移动 Android。它将捕获 xml.responseText 对象中的回显响应并将其显示在页面上,但它不会与数据库通信。
有什么想法吗?
I'm new to ajax and I would like to make all of my textboxes submit to my MySQL database without an update page.
This works fine if I capture the value of the textbox with js as the parameter "str" ("type" is just another parameter i'm using)
function sendAjax(type, str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("txtResp").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("txtResp").innerHTML=xmlhttp.responseText;
}
}
switch(type)
{
case 'search':
xmlhttp.open("GET","mysql_process_search.php?q="+str,true);
break;
case 'add':
xmlhttp.open("GET","mysql_process_event.php?q="+str,true);
break;
}
xmlhttp.send();
}
And with the page mysql_process_event.php I get the proper results on a Desktop browser using:
echo $_GET['q'];
$q = $_GET['q'];
$con = mysql_connect('xxx', 'xxx', 'xxx');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xxx", $con);
$result = mysql_query("SELECT * FROM Events");
mysql_query("INSERT INTO Events (Title)
VALUES ('".$q."')");
mysql_close($con);
But this does not work on Mobile Safari or Mobile Android. It will capture the echoed response in the xml.responseText object fine and display it on the page, but it will not communicate with the database.
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到的错误消息是什么?如果直接调用 php 页面,即使在移动设备上,您也应该能够看到最终结果。
其次,您编写的代码本质上是危险的,并且容易受到 SQL 注入的影响,这意味着很容易破解您的数据库并做坏事(例如从中选择所有内容、删除表等)。最好使用绑定变量,并且至少转义字符串。
What is the error message you receive? If you call the php page directly, you should be able be able to see the end-result, even on a mobile device.
Secondly, your code, as written, is inherently dangerous and wide open to SQL Injection, which means that it would be simple to hack your database and do bad things (like selecting everything from it, dropping tables, etc.). You want to be using bind variables, preferably, and escaping your string at the least.