I've gotten in over my head on this one. I found a dropdown script and some tables for countries and states. I'm using it to enter data into a table. I've put in my own code to put the results into a table - but only if they select everything.
What I'm trying to do now is, if they only select country and ignore the last 'states' dropdown, that on submit it enters every state of that country into the database. So, I could pick North America, USA and Florida. That's all it puts into the table: Item ID and Florida's code. But if I stop at USA it puts an entry into the table for all 50 states.
This, I can't seem to get working. It' just drops a zero into the first table column, over four rows. The second column remains empty.
Here's the code I wrote at the top of the dropdown:
// start of my code
if( empty($_POST['States']) )
{
$descriptor1 = trim($_POST['item_id']);
$descriptor2 = trim($_POST['Country']);
$query = "SELECT ID FROM States AS ID WHERE List3Ref = ‘$descriptor2'";
$result = @mysql_query($query);
if ($result) {
foreach ($query as $val ) {
$queryb = "INSERT INTO locations_link(item_id, state) VALUES ('$descriptor', '$val')";
mysql_query($queryb) or die( mysql_error() );
// echo $queryb . '<br />';
echo 'locations added for this plant.';
}
} else {
$descriptor1 = trim($_POST['item_id']);
$descriptor2 = trim($_POST['States']);
$query = "INSERT INTO locations_link(item_id, state)
VALUES ('$descriptor1', '$descriptor2')";
$result = @mysql_query ($query);
}
}
// // end of my code
and here's the rest of the code, if it helps spot any error. There's a line,
echo '<option value=""></option>';
This seems to be what goes into the space if nobody chooses the dropdown. I've tried playing with this bit too, but to no success.
if( isset($_GET['ajax']) )
{
//In this if statement
switch($_GET['ID'])
{
case "LBox2":
$query = sprintf("SELECT * FROM continent_regions WHERE List1Ref=%d",$_GET['ajax']);
break;
case "LBox3":
$query = sprintf("SELECT * FROM Country WHERE List2Ref=%d",$_GET['ajax']);
break;
case "LBox4":
$query = sprintf("SELECT * FROM States WHERE List3Ref=%d",$_GET['ajax']);
break;
}
$result = mysql_query($query);
// this determines what happens if nothing is selected
echo '<option value=""></option>';
while ($row = mysql_fetch_assoc($result))
{
echo "<option value='{$row['ID']}'>{$row['Name']}</option>\n";
}
mysql_close($dbh);
exit; //we're finished so exit..
}
if (!$result = mysql_query("SELECT * FROM Continents"))
{
echo "Database is down";
}
//for use with my FIRST list box
$Continents = "";
while ($row = mysql_fetch_assoc($result))
{
$Continents .= "<option value='{$row['ID']}'>{$row['Name']}</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)
{
//the line below reset the third list box incase list 1 is changed
document.getElementById('LBox4').innerHTML = "<option value=''></option>";
//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-->
<br><br>
<FORM style="border: 1px dotted red; padding: 2px" method="post" enctype="multipart/form-data" name="myForm" target="_self">
<table border="0" width=720>
<tr>
<td>
<!--
OK here we call the ajaxFuntion LBox2 refers to where the returned data will go
and the this.value will be the value of the select option
-->
// I added this box in for an item id passed from elsewhere. The rest is pretty much original script
<input type="text" name="item_id" value="<?php $var1 = $_GET['var1']; echo $var1; ?>" />
<br><br>
<b>Continent:</b><br>
<select name="Continents" id="LBox1" onchange="ajaxFunction('LBox2', this.value);">
<option value='default'></option>
<?php
echo $Continents;
?>
</select>
</td>
<td><b>Subcontinent:</b><br>
<select name="continent_regions" id="LBox2" onchange="ajaxFunction('LBox3', this.value);">
<option value=''></option>
<!-- OK the ID of this list box is LBox2 as refered to above -->
</select>
</td>
<td><b>Country:</b><br>
<select name="Country" id="LBox3" onchange="ajaxFunction('LBox4', this.value);">
<option value=''></option>
<!-- OK the ID of this list box is LBox3 as refered to above -->
</select>
</td>