Jump to content

dropdown not populated


dflow

Recommended Posts

i have 3 drop downs

categories

countries

city or  region

 

CatID filters the switch im using to upgrade this ajax script i found:

this is findstate.php

<?php require_once('Connections/tests.php'); ?>
<?php $CatID=intval($_GET['Category']);
echo $CatID;
?>


<!--//---------------------------------+
//  Developed by Roshan Bhattarai    |
//	http://roshanbh.com.np           |
//  Contact for custom scripts       |
//  or implementation help.          |
//  [email protected]     |
//---------------------------------+-->
<?php
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - [email protected]
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://roshanbh.com.np
?>



<?php mysql_query("SET NAMES 'utf8'")?>
<?php

$dbconnection = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("international", $dbconnection) or die("Couldn't open database: ".mysql_error());


if (isset($CatID)) {
    
}
switch ($CatID)
{
    case 1:
$result = mysql_query("SELECT country_list.CountryID,country_list.CountryName,country_list.CountryName_heb from country_list WHERE CategoryID = '".$_GET['CatID']."'");
        break;
    case 2:
        $result = mysql_query("SELECT country_list.CountryID,country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID2 = '".$_GET['CatID']."'");
        break;
case 3:
        $result = mysql_query("SELECT country_list.CountryID,country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID3 = '".$_GET['CatID']."'");
        break;
case 4:
        $result = mysql_query("SELECT country_list.CountryID,country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID4 = '".$_GET['CatID']."'");
        break;
case 5:
        $result = mysql_query("SELECT country_list.CountryID,country_list.CountryName,country_list.CountryName_heb from country_list WHERE CatID5 = '".$_GET['CatID']."'");
        break;
}   	
?>



<form>
<select name="state" onchange="getCity(<?php echo $CatID;?>,this.value)">
<option>Select State</option>
<?php while($row=mysql_fetch_assoc($result)) { ?>

<option value="<?php echo  $row_result['CountryID']; ?>"><?php echo $row_result['CountryName']; ?></option>
<?php } ?>
</select>
</form>
<?php
mysql_free_result($result);
?>

 

this is the index.php with the ajax

<?php require_once('Connections/international.php'); ?>
<?php mysql_query("SET NAMES 'utf8'")?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_international, $international);
$query_RsCategories = "SELECT CategoryID, CategoryName, CategoryName_heb FROM category_list ORDER BY CategoryName ASC";
$RsCategories = mysql_query($query_RsCategories, $international) or die(mysql_error());
$row_RsCategories = mysql_fetch_assoc($RsCategories);
$totalRows_RsCategories = mysql_num_rows($RsCategories);
?>
<html>
<head>
<title>Roshan's Triple Ajax dropdown code</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
// Roshan's Ajax dropdown code with php
// This notice must stay intact for legal use
// Copyright reserved to Roshan Bhattarai - [email protected]
// If you have any problem contact me at http://roshanbh.com.np
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}

	return xmlhttp;
    }

function getState(CategoryId) {		

	var strURL="findState2.php?Category="+CategoryId;
	var req = getXMLHTTP();

	if (req) {

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('statediv').innerHTML=req.responseText;						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}		
}
function getCity(CategoryId,stateId) {		
	var strURL="findCity.php?Category="+CategoryId+"&state="+stateId;
	var req = getXMLHTTP();

	if (req) {

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('citydiv').innerHTML=req.responseText;						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}

}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150">Country</td>
    <td  width="150"><select name="Category" onChange="getState(this.value)">
 <option value="">Select Category</option>
  <?php
do {  
?>
  <option value="<?php echo $row_RsCategories['CategoryID']?>"><?php echo $row_RsCategories['CategoryName']?></option>
  <?php
} while ($row_RsCategories = mysql_fetch_assoc($RsCategories));
  $rows = mysql_num_rows($RsCategories);
  if($rows > 0) {
      mysql_data_seek($RsCategories, 0);
  $row_RsCategories = mysql_fetch_assoc($RsCategories);
  }
?>
</select></td>
  </tr>
  <tr style="">
    <td>Country</td>
    <td ><div id="statediv"><select name="state" >
<option>Select Category First</option>
        </select></div></td>
  </tr>
  <tr style="">
    <td>CityorRegion</td>
    <td ><div id="citydiv"><select name="city">
<option>Select Country First</option>
        </select></div></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
</table>
</form>
</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/186180-dropdown-not-populated/
Share on other sites

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.