Hi,
I’m using a Javascript code from my friend and incorporating the DB connection with PHP. I’m new to PHP and Jscript. The PHP does not throw a error but there is no display and it says
“Error: Object Expected” in the Jscript at the browser and not working.
I need to get this fixed. its urgent. Can someone kindly help.
Here is my code
<!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>Untitled Document</title>
<?php
include("db.php");
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
// Get a connection to the database ?>
<script language="javascript">
// ======================
function dblookup()
{
<?php
$result= mysql_query("SELECT * FROM products");
while ($rsproducts = mysql_fetch_array($result))
{
?>
var arrproducts = <?php $rsproducts ?>;
arrproducts = arrproducts.toArray();
var colsproducts= <?php mysql_num_rows($result) ?>;
<?php } ?>
// connection object
//using getRows for rsproducts recordset to get the products
//if(!rsproducts.EOF){
// var arrproducts = rsproducts.GetRows();
//arrproducts = arrproducts.toArray();
//var colsproducts= rsproducts.Fields.Count
//}else var arrproducts=new Array()
//Closing recordset since we have the info in array arrproducts
//rsproducts.Close()
//rsproducts = null
// write the HTML for the form
var html = ''
//Construct the main table
html += '<table width="100%" cellspacing="0" cellpadding="0" border="2" ID="Table1">'
html += '<tr>'
html += '<td width="34%" height="25" bgcolor="#FF8F20"><font face="verdana" color="black"><span class="style41"> <b>Product</b></span></font></td>'
html += '<td width="17%" height="25" bgcolor="#FF8F20"><span class="style40"><strong><font face="verdana" color="black">Size</font> </strong></span> </td>'
html += '<td width="15%" height="25" bgcolor="#FF8F20"><span class="style40"><strong><font face="verdana" color="black">Price</font> </strong></span> </td>'
html += '<td width="34%" height="25" bgcolor="#FF8F20"><span class="style40"><strong><font face="verdana" color="black">Add</font> </strong></span> </td>'
html += '</tr>'
//main table and the heading TR ends here
var j = 1;
for(row=0;row < arrproducts.length;row+=colsproducts)//Looping product rows
{
html += '<tr>'
//dynamic creation of products row elements
html += '<td width="34%" height="25"><span class="style40"><font face="verdana" color="black">' + arrproducts[row + 1] + '</font></span></td>'
html += get_size(arrproducts[row])
html += '<td>ADD</td>'
html += '</tr>'
}
html += '</table>'
document.getElementById("main").innerHTML = html
}
function get_size(productid)
{
var html='';
<?php
$sql ="SELECT * FROM Type WHERE PID=" + productid+ ';';
//debug code:
//alert(sql);
$result= mysql_query($sql);
while($rstypes = mysql_fetch_array($result))
{
?>
var arrtypes = <?php $rstypes ?>;
arrtypes = arrtypes.toArray();
var colstypes= <?php mysql_num_rows($result) ?>;
<?php } ?>
// record set for types
/*var rstypes = new ActiveXObject("ADODB.Recordset");
rstypes.Open(sql,ConnectObj,adOpenForwardOnly,adLockReadOnly,adCmdText);
//using getRows for rstypes recordset to get the size and price
if(!rstypes.EOF){
var arrtypes= rstypes.GetRows();
arrtypes = arrtypes.toArray();
var colstypes= rstypes.Fields.Count
}else var arrtypes=new Array()
//Closing recordset since we have the info in array arrproducts
rstypes.Close()
rstypes = null*/
//we have more than one type to load in a drop down
//check for more than one type and also for only one type
if (arrtypes.length > 0 && arrtypes.length != colstypes)
{
html += '<td><select name=' + arrtypes[1] + ' onchange=get_price(this)><option value=0>Pick a Size</option>';
for(i=0;i < arrtypes.length;i+=colstypes)//Looping types rows
{
//dynamic creation of drop down [size] values from types
html += '<option value="' + arrtypes[i + 3] + '">' + arrtypes[i + 2]+ '</option>'
}
html += '</select></td>'
// here the catch is
// assign a static name to differntiate the dropdown id and the table td
html += '<td id= TSB' + arrtypes[1] + '>price</td>'
//debug code:
// alert(html);
return html;
}
//***************************************************
// Below code is for displaying only one type and no type conditions
// If we dont need this we can comment them if we are sure that we
// are going to have a type for all the products
//***************************************************
//we have only one type so just write down the price
if (arrtypes.length == colstypes)
{
//you have only one type so list the price here
html += '<td>' + arrtypes[2] + '</td>'
html += '<td>' + arrtypes[3] + '</td>'
return html;
}
//we have no type for this product
if (arrtypes.length == 0)
{
html += '<td>Coming Soon</td>'
html += '<td>$0</td>'
return html;
}
} // function get_size ends
/*
This function is used to set the price when a value is selected in the drop down
*/
function get_price(sel)
{
var name = "TSB" + sel.name
// move the price field to price td');
//debug code
//alert(sel.options[sel.options.selectedIndex].value);
//Get the price field which is hidden in the drop down index
//Get the TD by its ID
document.getElementById(name).innerHTML = sel.options[sel.options.selectedIndex].value;
}// function get_price ends
</script>
</head>
<body bgcolor="#FFFFFF" onLoad="dblookup()">
test this page
<div id="main"></div>
</body>
</html>