Hey guys,

I've heard that you can write php code when its combined with javascript, you just need to echo"" out all the javascript functions.

So i've done this and it is working.. to a degree. From using Firebug to test the page, the javascript is coming through and it is showing the PHP variables form the MySQL database. However the select box is still not working. The variables are not being displayed.

<script type="text/javascript">

require_once('../sqlconnect/connect.php'); // database connection details

echo "function fillCategory() {";
// this function is used to fill the category list on load
$query1=mysql_query("select * from groups");
echo "addOption(document.drop_list.Category, '$result1[groupname]', '$result1[groupname]');";
}// end of while
echo "}"; // end of JS function

function SelectSubCat(){
// ON or after selection of category this function will work

addOption(document.drop_list.SubCat, "", "Item Name", "");

// Collect all element of subcategory for various groupid

// let us collect all groupid and then collect all subcategory for each groupid
$query2=mysql_query("select distinct groupname FROM pricelist");
// In the above query you can collect groupid from groups table also.
//echo "$result2[id]";
echo "if(document.drop_list.Category.value == '$result2[groupname]'){";
$query3=mysql_query("select item, price from pricelist where groupname='$result2[groupname]'");
echo "addOption(document.drop_list.SubCat,'$result3[id]', '$result3[item]');";
} // end of while loop
echo "}"; // end of JS if condition
} //end of while function
echo "}"; //end of JS function

$query = "select * from customerdetails";
$customer = mysql_query('select * from customerdetails');

function removeAllOptions(selectbox){
var i;
}//end of for
}//end of remove AllOptions

function addOption(selectbox, value, text ){
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;

Sorry for the massive amounts of script, i didn't know where to shorten it for error checking.
Can anyone help?

selectbox.options is an array. The add() function is a member of the Element family ie., selectbox.add(<elem>); not selectbox.options.add(<elem>);

Ok i've tried that, but it just doesn't want to work. It's wierd because this javascript worked in an external .js file with the PHP as well. I don't know whats happened.

I realised that i had forgotten to include a <body onload=fillCategory()> in the html!

Cheers for all the help

This question has already been answered. Start a new discussion instead.