Jump to content

[SOLVED] Need help with missing form values .


larsbrimmer

Recommended Posts

I was sure I had this script working. When I select House from List1, List2 propagates with values whose parent is 1. But when i select Trance from List1, List2 is empty.

 

Here is what the form should look like if house is selected:

  <select name="list1" id="LBox1" onchange="ajaxFunction('LBox2', this.value);">
      <option value=''></option>
      <option value='1'>House</option>
      <option value='8'>Trance</option>
    </select>


    <select name="list2" id="LBox2">
       <option value='2'>Progressive House</option>
       <option value='3'>Acid house</option>
       <option value='4'>Deep house</option>
       <option value='5'>Electro house</option>
    </select>

 

 

Here is what the form should look like if trance is selected:

    <select name="list2" id="LBox2">
       <option value='9'>Acid trance</option>
       <option value='10'>Hard trance</option>
       <option value='11'>Progressive trance</option>
       <option value='12'>Vocal trance</option>
    </select>

 

Here is the table dump:

`RayMusicCategories` (`ID`, `Parent`, `Title`) VALUES
(1, 0, 'House'),
(2, 1, 'Progressive House'),
(3, 1, 'Acid house'),
(4, 1, 'Deep house'),
(5, 1, 'Electro house'),
(6, 1, 'Tech house'),
(7, 1, 'Tribal house'),
(8, 0, 'Trance'),
(9, 2, 'Acid trance'),
(10, 2, 'Hard trance'),
(11, 2, 'Progressive trance'),
(12, 2, 'Vocal trance');

 

 

Here is the code:

<?php
   $hostname = "localhost";
   $username = "";
   $password = "";
   $dbh = mysql_connect($hostname, $username, $password)
      or die("Unable to connect to MySQL");
   $selected = mysql_select_db("testdb",$dbh);
      if( isset($_POST['Submit']) )
      {
         echo "<pre>";
         print_r($_POST);
      }
      if( isset($_GET['ajax']) )
   {
      //In this if statement
      $query = sprintf("SELECT * FROM RayMusicCategories WHERE Parent=%d",$_GET['ajax']);
      
      $result = mysql_query($query);
      echo "<option value=''></option>";
      while ($row = mysql_fetch_assoc($result))
      {
         echo "<option value='{$row['ID']}'>{$row['Title']}</option>\n";
      }
      mysql_close($dbh);
      exit; //we're finished so exit..
   }
   
$result = mysql_query("SELECT * FROM RayMusicCategories WHERE Parent=0") or die(mysql_error());
   //for use with my FIRST list box
   $List1 = "";
   while ($row = mysql_fetch_assoc($result))
   {
      $List1 .= "<option value='{$row['ID']}'>{$row['Title']}</option>\n";
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Simple Dymanic Drop Down</title>
<script language="javascript">
   function ajaxFunction(ID, Param)
   {
      //link to the PHP file your getting the data from
      //var loaderphp = "register.php";
      //i have link to this file
      var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>";
      
      //we don't need to change anymore of this script
      var xmlHttp;
      try
       {
         // Firefox, Opera 8.0+, Safari
         xmlHttp=new XMLHttpRequest();
       }catch(e){
         // Internet Explorer
         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)
           {
              //THIS SET THE DAT FROM THE PHP TO THE HTML
            document.getElementById(ID).innerHTML = xmlHttp.responseText;
           }
      }
       xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true);
       xmlHttp.send(null);
   }
</script>
</head>
<body>
<!-- OK a basic form-->
<form method="post" enctype="multipart/form-data" name="myForm" target="_self">
<table border="0">
  <tr>
    <td>
      <!--
      OK here we call the ajaxFuntion LBox2 refers to where the returned date will go
      and the this.value will be the value of the select option
      -->
      <select name="list1" id="LBox1" onchange="ajaxFunction('LBox2', this.value);">
         <option value=''></option>
      <?php 
         echo $List1;
      ?>
      </select>
   </td>
    <td>
      <select name="list2" id="LBox2">
         <option value=''></option>
            <!-- OK the ID of this list box is LBox2 as refered to above -->
      </select>
   </td>
  </tr>
</table>
  <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

 

What does this do? Could it be the problem?

 

WHERE Parent=%d",$_GET['ajax'])

 

-lars

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.