douyan6958
2012-01-12 14:55 阅读 34
已采纳

PHP:Ajax-to-PHP参数问题

Using the Ajax code, designated below, in the head of my page, ETD2.html, it'll send a parameter to a php script containing a switch statement. The problem I'm having is that in order for the switch statement to work correctly, the parameter has to include more than just a number, while for the mysql query to run, I need a variable which will be equal to the parameter, although it'll only contain the number in the variable and nothing else.

Ajax:

<script type="text/javascript">
function doAjax(ajaxFunction, str)
{ 
if (str=="")
  { 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","getuser.php?q="+str+"func="+ajaxFunction,true);
xmlhttp.send(); }
</script>

PHP:

<?php

include("dbinfo.inc.php");
$con = mysql_connect(localhost, $username, $password);
if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

$db_selected = mysql_select_db($database, $con);
if (!$db_selected)
    {
    die('Could not connect to ' . $database . '<br />' . mysql_errno() . ': ' . mysql_error());
    }

$sFunction  = $_GET["func"];
$q=$_GET["q"];

switch($sFunction){
    case 'showCharacters':
       $result = mysql_query("SELECT * FROM `tablename` WHERE id = '" . mysql_real_escape_string($q) . "'") or die(mysql_error());
    Display Info from Database.
    break;
    case 'showTowers':
       $result = mysql_query("SELECT * FROM `tablename` WHERE id = '" . mysql_real_escape_string($q) . "'") or die(mysql_error());
    Display Info from Database.
    break;
    case 'showEnemies':
       $result = mysql_query("SELECT * FROM `tablename` WHERE id = '" . mysql_real_escape_string($q) . "'") or die(mysql_error());
    Display Info from Database.
    break;
    }
?>

I've tried a few different things, although nothing has worked yet. One thing I tried is since the parameter is made into a variable at the start of the script, I made it into a string and tried the preg_replace expression, but that still had no effect. Maybe I didn't use the correct syntax? I'm not sure. I'm not very knowledgeable about PHP, but I'm sure I'm on the right track.

The parameter is passed to the script in this format: ("Number" func= "parentelement-name") = 2func=showTowers

This is what I tried, although it didn't work.

$para =$_GET["q"];
$bres = (string)$para;
$ares = preg_replace("/[^0-9]/","", $bres); 

Any help is appreciated. Please don't comment about jquery or say anything negative about how dumb my question is. If it was really that dumb, I wouldn't be asking for help.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doujiexi1824 doujiexi1824 2012-01-12 14:56

    You do not delimit your query variables with an ampersand, as you should. Do this:

    xmlhttp.open("GET","getuser.php?q="+str+"&func="+ajaxFunction,true); 
                                             ^
                                             |
    ADDED! ----------------------------------/
    
    点赞 评论 复制链接分享

相关推荐