the setup:
2 dropdown menus(will be more later on), where once select options in one the other updated with only info for that options.
i have the datebase and sql select right but i can't get the drop downs to work right. right only works if i select a options in the first drop down the 2nd drop with update right. but if i start with the 2nd the first doesn't update right.
the code i have right now
session_start();
$_SESSION['link'] = new mysqli('localhost', 'asucrews_zlrope', 'FmdtSca04', 'asucrews_ropes');
function getTableData($table,$field){
$link = $_SESSION['link'];
if(!empty($_POST['vendor']) AND empty($_POST['fiber'])){
$sql = "select `fiber` from `viewVBF` where `vendor` = '".$_POST['vendor']."'";
}else if(empty($_POST['vendor']) AND !empty($_POST['fiber'])){
$sql = "select `vendor` from `viewVBF` where `fiber` = '".$_POST['fiber']."'";
}else if(empty($_POST['vendor']) AND empty($_POST['fiber'])){
$sql = "select ".$field." from ".$table;
}
$_SESSION['sql'] = $sql;
$results = $link->query($sql);
while($row = $results->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
function getTableData2($table,$field){
$link = $_SESSION['link'];
$sql = "select ".$field." from ".$table;
$results = $link->query($sql);
while($row = $results->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
function getTableData3(){
$link = $_SESSION['link'];
$sql = $_SESSION['sql'];
$results = $link->query($sql);
while($row = $results->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
function ddByTable($table,$field){
$name = substr($table,0,-1);
echo $_POST[$name] . "\n";
echo '<select name="'.$name.'" onchange="this.form.submit();">'."\n";
if(!empty($_POST[$name])){
echo '<option value="'.$row[$field].'"> </option>'."\n";
foreach(getTableData2($table,$field) as $row){
echo '<option value="'.$row[$field].'"';
if(!empty($_POST[$name]) AND $_POST[$name] == $row[$field]){
echo 'selected="selected"';
}
echo '>'.$row[$field].'</option>'."\n";
}
}else{
foreach(getTableData($table,$field) as $row){
echo '<option value="'.$row[$field].'"';
if(!empty($_POST[$name]) AND $_POST[$name] == $row[$field]){
echo 'selected="selected"';
}
echo '>'.$row[$field].'</option>'."\n";
}
}
echo '</select>'."\n";
}
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
ddByTable('vendors','name');
ddByTable('fibers','fiber');
echo '</form>'."\n";
print_r($_POST);
echo "<br />";
echo $_SESSION['sql'] . "\n";
getTableData3();
getTableData3() is use for test only.