douyan6958 2012-01-12 14:55
浏览 36
已采纳

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 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! ----------------------------------/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧