开发者

How to make Ajax Requests on Mobile Devices using mySQL

I'm new to ajax and I would like to make all of my textboxes submit to my MySQL database without an update page.

T开发者_Go百科his 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜