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");
mysql_error();
while($result1=mysql_fetch_array($query1))
{
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

removeAllOptions(document.drop_list.SubCat);
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.
while($result2=mysql_fetch_array($query2)){
//echo "$result2[id]";
echo "if(document.drop_list.Category.value == '$result2[groupname]'){";
$query3=mysql_query("select item, price from pricelist where groupname='$result2[groupname]'");
while($result3=mysql_fetch_array($query3))
{
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";
mysql_query($customer);
$customer = mysql_query('select * from customerdetails');
if(mysql_error())
{
print(mysql_error());
}
?>

function removeAllOptions(selectbox){
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
//selectbox.options.remove(i);
selectbox.remove(i);
}//end of for
}//end of remove AllOptions

function addOption(selectbox, value, text ){
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
</script>

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.

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

Cheers for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.