Simple AJAX problem...

  • neksus
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2194
  • Loc: Canada

Post 3+ Months Ago

Alright, here's the javascript in question:
Code: [ Select ]
function getGames(str){
   var xmlHttp;
   try
    {
      xmlHttp=new XMLHttpRequest();
    }catch(e){
      try
      {
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }catch(e){
         try
         {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }catch(e){
            alert("Your browser does not support AJAX!");
            return false;
         }
      }
   }
    xmlHttp.onreadystatechange=function()
   {
      if(xmlHttp.readyState==4)
        {
         document.getElementById(gameslist).innerHTML = xmlHttp.responseText;
        }
   }
   var url='addgames.php?console='+str;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}
  1. function getGames(str){
  2.    var xmlHttp;
  3.    try
  4.     {
  5.       xmlHttp=new XMLHttpRequest();
  6.     }catch(e){
  7.       try
  8.       {
  9.          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  10.       }catch(e){
  11.          try
  12.          {
  13.             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  14.          }catch(e){
  15.             alert("Your browser does not support AJAX!");
  16.             return false;
  17.          }
  18.       }
  19.    }
  20.     xmlHttp.onreadystatechange=function()
  21.    {
  22.       if(xmlHttp.readyState==4)
  23.         {
  24.          document.getElementById(gameslist).innerHTML = xmlHttp.responseText;
  25.         }
  26.    }
  27.    var url='addgames.php?console='+str;
  28.    xmlHttp.open("GET",url,true);
  29.    xmlHttp.send(null);
  30. }


and here's the php page:
Code: [ Select ]
<?
 
include 'includes/header.php';
 
if(isset($_GET['console'])){
   $thisconsole = $_GET['console'];
   $query = "SELECT id, name, system FROM tradevalues WHERE system = '$thisconsole' ORDER BY name ASC";
   $result = mysql_query($query);
   $games = "";
   //$games = "<select name=\"whatgame\">";
   while($rout=mysql_fetch_array($result)){
      $games .= "<option value=\"$rout[id]\">$rout[name]</option>\n";
   }
   //$games .= "</select>";
   echo "$games";
   exit;
}
 
if(!$thisconsole) $thisconsole = "";
 
if($_POST['submit']){
 
} else {
   if($authorized == "true"){
?>
<form method="POST" action="<? echo "$targself"; ?>" enctype="multipart/form-data"><select name="consolelist" id="consolelist" onchange="getGames(this.value);"><option>Please select a console</option>
<?
$query = "SELECT DISTINCT system FROM tradevalues ORDER BY system ASC";
$result = mysql_query($query);
while($rout=mysql_fetch_array($result)){
   echo "<option value=\"$rout[system]\">$rout[system]</option>\n";
}
?>
</select>
<select id="gameslist" name="gameslist"></select>
<?
   }
}
?>
</form>
  1. <?
  2.  
  3. include 'includes/header.php';
  4.  
  5. if(isset($_GET['console'])){
  6.    $thisconsole = $_GET['console'];
  7.    $query = "SELECT id, name, system FROM tradevalues WHERE system = '$thisconsole' ORDER BY name ASC";
  8.    $result = mysql_query($query);
  9.    $games = "";
  10.    //$games = "<select name=\"whatgame\">";
  11.    while($rout=mysql_fetch_array($result)){
  12.       $games .= "<option value=\"$rout[id]\">$rout[name]</option>\n";
  13.    }
  14.    //$games .= "</select>";
  15.    echo "$games";
  16.    exit;
  17. }
  18.  
  19. if(!$thisconsole) $thisconsole = "";
  20.  
  21. if($_POST['submit']){
  22.  
  23. } else {
  24.    if($authorized == "true"){
  25. ?>
  26. <form method="POST" action="<? echo "$targself"; ?>" enctype="multipart/form-data"><select name="consolelist" id="consolelist" onchange="getGames(this.value);"><option>Please select a console</option>
  27. <?
  28. $query = "SELECT DISTINCT system FROM tradevalues ORDER BY system ASC";
  29. $result = mysql_query($query);
  30. while($rout=mysql_fetch_array($result)){
  31.    echo "<option value=\"$rout[system]\">$rout[system]</option>\n";
  32. }
  33. ?>
  34. </select>
  35. <select id="gameslist" name="gameslist"></select>
  36. <?
  37.    }
  38. }
  39. ?>
  40. </form>


I just don't know why the 2nd drop-down box isn't populating...I've put in an alert to output the str variable in the js to make sure it's calling, and it definitely is...it just isn't updating the data to the select. Anybody have anything for me to try or correct?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

Code: [ Select ]
<?
$authorized = "true"; //assumed
$targself = 'addgames.php';
if(isset($_GET['console'])){
   $thisconsole = $_GET['console'];
//   $query = "SELECT id, name, system FROM tradevalues WHERE system = '$thisconsole' ORDER BY name ASC";
//   $result = mysql_query($query);
   $games = "";
   $games = "<select name=\"whatgame\">";
//   while($rout=mysql_fetch_array($result)){
$n=0;
while($n<4){
      $games .= "<option value=\"$n\">$n</option>\n";
      $n++;
    }
   $games .= "</select>";
   echo "$games";
   exit;
}
 
 
if(!$thisconsole) $thisconsole = "";
 
if($_POST['submit']){
 
} else {
   if($authorized == "true"){
?>
<form method="POST" action="<? echo "$targself"; ?>"  enctype="multipart/form-data"><select name="consolelist" id="consolelist" onchange=alert(this.value)><option>Please select a console</option>
<?
//$query = "SELECT DISTINCT system FROM tradevalues ORDER BY system ASC";
//$result = mysql_query($query);
//while($rout=mysql_fetch_array($result)){
$i=0;
while($i<4)
{  
    echo "<option value=\"$i\">$i</option>\n";
    $i++;
}
?>
</select>
<select id="gameslist" name="gameslist"></select>
<?
   }
}
?>
</form>
 
  1. <?
  2. $authorized = "true"; //assumed
  3. $targself = 'addgames.php';
  4. if(isset($_GET['console'])){
  5.    $thisconsole = $_GET['console'];
  6. //   $query = "SELECT id, name, system FROM tradevalues WHERE system = '$thisconsole' ORDER BY name ASC";
  7. //   $result = mysql_query($query);
  8.    $games = "";
  9.    $games = "<select name=\"whatgame\">";
  10. //   while($rout=mysql_fetch_array($result)){
  11. $n=0;
  12. while($n<4){
  13.       $games .= "<option value=\"$n\">$n</option>\n";
  14.       $n++;
  15.     }
  16.    $games .= "</select>";
  17.    echo "$games";
  18.    exit;
  19. }
  20.  
  21.  
  22. if(!$thisconsole) $thisconsole = "";
  23.  
  24. if($_POST['submit']){
  25.  
  26. } else {
  27.    if($authorized == "true"){
  28. ?>
  29. <form method="POST" action="<? echo "$targself"; ?>"  enctype="multipart/form-data"><select name="consolelist" id="consolelist" onchange=alert(this.value)><option>Please select a console</option>
  30. <?
  31. //$query = "SELECT DISTINCT system FROM tradevalues ORDER BY system ASC";
  32. //$result = mysql_query($query);
  33. //while($rout=mysql_fetch_array($result)){
  34. $i=0;
  35. while($i<4)
  36. {  
  37.     echo "<option value=\"$i\">$i</option>\n";
  38.     $i++;
  39. }
  40. ?>
  41. </select>
  42. <select id="gameslist" name="gameslist"></select>
  43. <?
  44.    }
  45. }
  46. ?>
  47. </form>
  48.  


seems fine to me.

oh ya, I just had this awhile ago.

did you tested it on IE or firefox?

IE seems to be using cache data for your page, so the result will be the same.

if you test it on FireFox, it will update as you wanted.

you would need to insert a date hack with each ajax request for IE to work

Code: [ Select ]
 var url='addgames.php?date?=Date()&console='+str;
  • neksus
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2194
  • Loc: Canada

Post 3+ Months Ago

Always test in Firefox :)
I couldn't get it to work - and everything seemed sound in the code, so I redid it a bit different. I now load each option into it's own array and simply call it when needed. It's more resource intensive than I'd hoped, but I can always come back to it later :)
  • ecuador
  • Student
  • Student
  • User avatar
  • Posts: 90
  • Loc: Germany

Post 3+ Months Ago

Code: [ Select ]
# $thisconsole = $_GET['console'];
#    $query = "SELECT id, name, system FROM tradevalues WHERE system = '$thisconsole' ORDER BY name ASC";
  1. # $thisconsole = $_GET['console'];
  2. #    $query = "SELECT id, name, system FROM tradevalues WHERE system = '$thisconsole' ORDER BY name ASC";


This opens doors wide to any kind of attack to your DB... always distrust user input. Do a addlashes and strip_tags to your GET variable before passing it on to your DB.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 120 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.