PHP: Ajax-to-PHP parameter issue
<p>Using the Ajax code, designated below, in the head of my page, <a href="http://tobedetermined.com.nu/etd2.html" rel="nofollow">ETD2.html</a>, 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.</p>

<p>Ajax:</p>

<pre><code>&lt;script type="text/javascript"&gt;
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 &amp;&amp; xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; }
  }
xmlhttp.open("GET","getuser.php?q="+str+"func="+ajaxFunction,true);
xmlhttp.send(); }
&lt;/script&gt;
</code></pre>

<p>PHP:</p>

<pre><code>&lt;?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 . '&lt;br /&gt;' . 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;
    }
?&gt;
</code></pre>

<p>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.</p>

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

<p>This is what I tried, although it didn't work.</p>

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

<p>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.</p>
