Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7047961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:50:03+00:00 2026-05-28T02:50:03+00:00

Using the Ajax code, designated below, in the head of my page, ETD2.html ,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:50:04+00:00Added an answer on May 28, 2026 at 2:50 am

    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! ----------------------------------/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jQuery and Ajax. My MainFile has the following code: <html> <head>
My jQuery code (using ajax) request's data from a local php script (pgiproxy.php). This
I'm Using AJAX to dynamically fetch data from my php page (which contains html)
Code: <% using (Ajax.BeginForm(GetResourcesByProject, CreateRequest, new AjaxOptions { UpdateTargetId = ResourceListDiv})) { Response.Write(Html.DropDownList(SelectProject, Model.ProjectList,
I am using the below jquery code to call a ajax function in my
I am successfully posting a form via ajax, using the following code; $.post( Page.do?source=ajax,
I have a login form using jquery ajax and a php code. The issue
A button on page xyz.xhtml does invoke this code (using ajax). FacesContext fc =
i am using ajax with jquery .i have written this code: $.ajax({url:myurl.php, data:datastr, type:POST,
I'm Using AJAX to dynamically fetch data from my php page and display it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.