Parse error: syntax error, unexpected ','

Hello, I have an sql query that dilivers the responceText to a div in my index.html page but couldn’t get the javascript to run the new html? I decided to use ‘php include checkoutjs.php’ but unfortunatley the responceText is declaring this error.
Error:


Parse error: syntax error, unexpected ',', expecting '&' or T_VARIABLE in D:\\use_ide_1\\UniServer\\www\\checkoutjs.php on line 10

checkoutjs.php


<?php

//checkout.js
//==========================GLOBAL FUNCTIONS===========================//

//-----------------------------ADDEVENT--------------------------------//

//thanks to steve chapman at about.com javascript forum 

function addEvent(el, eType, fn, uC) { //line 10 ERROR! I need this function and the addevents it creates
if (el.addEventListener) {
el.addEventListener(eType, fn, uC);
return true;
} else if (el.attachEvent) {
return el.attachEvent('on' + eType, fn);
}
}

//----------------------GETELEMENTSBYCLASSNAME()-----------------------//

//thanks to steve chapman at about.com javascript forum 

document.getElementsByClassName=function(cl){
var retnode = [];
var myclass = new RegExp('\\\\b'+cl+'\\\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}

//--------------------------NUMBERFORMAT()----------------------------//

function numberFormat(nStr,prefix){var prefix = prefix || '';    
nStr += '';    
x = nStr.split('.');    
x1 = x[0];    
x2 = x.length > 1 ? '.' + x[1] : '';    
var rgx = /(\\d+)(\\d{3})/;    
while (rgx.test(x1))        
x1 = x1.replace(rgx, '$1' + ',' + '$2');    
return prefix + x1 + x2;}

//----------------------------ISNUM()----------------------------------//

function isNum(argvalue) {
argvalue=argvalue.toString();
if (argvalue.length==0)
return false;
for (var n=0;n<argvalue.length;n++)
if(argvalue.substring(n, n+1)<"0" || argvalue.substring(n, n+1)>"9")
return false;
return true;
}

//--=========================ADDTOCART()==============================//

var f1=document.getElementById('f1');
var f2=document.getElementById('f2');
var carttable=document.getElementById('carttable');
var cartbody=document.getElementById('cartbody');

function addtocart() {
    var bObj = document.getElementsByClassName('button');
    
    for (var i = 0; i < bObj.length; i++) {
        bObj[i].onclick = function() {
            insertItemRow(this);
        };
    }
}

function insertItemRow(button){
var fs=button.parentNode.parentNode;
var q=fs.getElementsByTagName('select')[2].value;
if(q!='' || q!=0) {
var code = fs.getElementsByTagName('input')[0].value;
var item = fs.getElementsByTagName('input')[1].value;
var color = fs.getElementsByTagName('select')[0].value;
var size = fs.getElementsByTagName('select')[1].value;
var price = fs.getElementsByTagName('input')[2].value;
var quantity = fs.getElementsByTagName('select')[2].value;
var val =  [[quantity],[price]];
var multiply = 0;
multiply+= (val[0]*val[1]);
var psrc=fs.parentNode.parentNode.parentNode.getElementsByTagName('input')[2].value;
var subtotal = document.getElementById('subtotal');
var itemrow=cartbody.insertRow(0); 
itemrow.setAttribute('title','Item that you have ordered.');
itemrow.setAttribute('id','itemrow[]');
itemrow.setAttribute('name','itemrow[]');
itemrow.className='itemrow';

var addcode=itemrow.insertCell(0);
addcode.className='code';
addcode.setAttribute('colspan','1');
addcode.appendChild(document.createTextNode(code));

var additem=itemrow.insertCell(1);
additem.className='item';
additem.setAttribute('colspan','3');

var additemlink=document.createElement('span');
additemlink.setAttribute('src',psrc);
additemlink.setAttribute('title','Review this item then click Continue Shopping to return.');
additemlink.className='itemlink';

additemlink.onclick=function(){
var spansrc=this.src
var eimg=document.getElementById('eimg');
eimg.firstChild.src=spansrc;
eimg.style.display='block';};

additemlink.appendChild(document.createTextNode(item));
additem.appendChild(additemlink);

var addcolor=itemrow.insertCell(2);
addcolor.className='color';
addcolor.setAttribute('colspan','1');
addcolor.appendChild(document.createTextNode(color));

var addsize=itemrow.insertCell(3);
addsize.className='size';
addsize.setAttribute('colspan','1');
addsize.appendChild(document.createTextNode(size));

var addprice=itemrow.insertCell(4);
addprice.className='price';
addprice.setAttribute('colspan','2');
addprice.appendChild(document.createTextNode(price));

var addquantity=itemrow.insertCell(5);
addquantity.className='quantity';
addquantity.setAttribute('colspan','1');
addquantity.innerHTML='<input type="text" value="'+quantity+'" title="You may change the quantity of this item" name="qty[]" class="qty" onkeydown="changeqty(this.value);"/>';

var addcost=itemrow.insertCell(6);
addcost.setAttribute('colspan','1');
addcost.setAttribute('title','The cost of the quantity of this item.');
addcost.className='cost';
addcost.appendChild(document.createTextNode(multiply.toFixed(2)));

var sTotal=sumsubtotal();
var addrowsubtotal=itemrow.insertCell(7);
addrowsubtotal.setAttribute('colspan','1');
addrowsubtotal.setAttribute('title','Subtotal cost of your order.');
addrowsubtotal.className='rowsubtotal';
addrowsubtotal.appendChild(document.createTextNode(subtotal.innerHTML));

var addclear=itemrow.insertCell(8);
addclear.setAttribute('colspan','1');
addclear.className='clear';
addclear.setAttribute('align','center');
addclear.innerHTML='<a title="Remove this item" class="removeitem" onclick="deleterow(this);">[x]</a>';

clientmessage.innerHTML='Thank You for Shopping with www. Boutique Wholesale Items .com and Mexicali Rose!'; 
var y=f1.clientHeight;
f1.scrollTop+=y;

EvalSound3();
}
else{
alert('Please enter a Quantity for this item         ');
button.parentNode.parentNode.getElementsByTagName('select')[2].focus();
}
}

//-------------------------------SUMSUBTOTAL()--------------------------------//

function sumsubtotal(){
var cartitems=document.getElementsByClassName('itemrow');
var itemrows=cartitems;
var sTotal=0.0;
for(var i=0;i<itemrows.length;i++)
{
var itemrow=itemrows[i];
var price=itemrow.cells[4];
var quantity=itemrow.cells[5].getElementsByTagName('input')[0].value;
var cost=itemrow.cells[6].innerHTML;
sTotal+=Number(cost) || 0;
multiply=price*quantity;
}
cost=parseFloat(multiply).toFixed(2);
subtotal.innerHTML=numberFormat(parseFloat(sTotal).toFixed(2));
ordersum.innerHTML=subtotal.innerHTML;
noofitems.innerHTML=itemrows.length;
}

//---------------------------------DELETEROW()--------------------------------//

function deleterow(a) {
// remove row
var row=a.parentNode.parentNode;
row.parentNode.removeChild(row);
var sTotal=sumsubtotal( );
EvalSound4();
}

//---------------------------------CHANGEQTY()-------------------------------//

//thanks to fang at webdeveloper.com addevent attachevent
function changeqty(){
var aObj=document.getElementsByClassName('qty');
for(var i=0;i<aObj.length;i++){
aObj[i].onchange=function(){qtymultiply(this);};
}};

//--------------------------------QTYMULTIPLY()------------------------------//

function qtymultiply(qty){
var itemrows=document.getElementsByClassName('itemrow');
var ir=qty.parentNode.parentNode;
var args=[1, 2, 4, 6, 8, 10, 20];
var multiply=0.0;
var price=ir.cells[4].innerHTML;
var quantity=ir.cells[5].getElementsByTagName('input')[0].value;
var cost=ir.cells[6];
var rowsubtotal=ir.cells[7];
//muchos gracias andrew_g at about.com javascript forum
for(var i=0;i!==args.length;i++){
if(qty.value==args[i]){
multiply+=price*quantity;
cost.innerHTML=parseFloat(multiply).toFixed(2);
sumsubtotal();
noofitems.innerHTML=itemrows.length;
ordersum.innerHTML=subtotal.innerHTML;
rowsubtotal.innerHTML=subtotal.innerHTML;
return; // exit loop and function.
}}
// This should only run if the statements in the if statement nested in the for loop do not execute.
alert('You only have these options: 1, 2, 4, 6, 8, 10 or 20         ');
qty.focus;
cost.innerHTML='0';
qty.value='!!';
sumsubtotal();
if(qty.value=='!!') {
alert('You only have these options: 1, 2, 4, 6, 8, 10 or 20         ');
qty.value='1';
cost.innerHTML=parseFloat(1*price).toFixed(2);
sumsubtotal( );
}}
/*
function rowtotals(){
var rowsubtotals=document.getElementsByClassName('rowsubtotal');
for(var i=0;i<rowsubtotals.length;i++){
var rowsubtotal=rowsubtotals[i].innerHTML;
}
rowsubtotal=subtotal.innerHTML;
}
*/

//----------------------------image a tags--------------------------------//

function pictureclick(){
var f1=document.getElementById('f1');
var pictures=f1.getElementsByTagName('img');
var iObj=pictures;
for(var i=0;i<iObj.length;i++){
iObj[i].onclick=function(){enhance(this);};
}
};

function enhance(img){
var icell = img.parentNode.parentNode;
var isrc = icell.getElementsByTagName('input')[2].value;
var eimg=document.getElementById('eimg');
eimg.firstChild.src=isrc;
alert(eimg.firstChild.src);
eimg.style.display='block';
}

var eimgbutton=document.getElementById('eimgbutton');
addEvent(eimgbutton,'click',function(){
var eimg=document.getElementById('eimg').style.display='none';
},false);



function f1alarm() {
    var bObj = document.getElementsByClassName('button');
    
    for (var i = 0; i < bObj.length; i++) {
        bObj[i].onclick = function() {
            showt(this);
        };
    }
}

function showt(button) {
    if (true/* do something */) {
        alert('I am Here!');
    } else {
        alert('I am not here!');
    }
}

function init(){
setTimeout('f1alarm()', 5000);
}

addEvent(window,'load',function(){init();},false);
//pictureclick();
//addtocart();
//init();

?>

getproduct.php


<?php

include("checkoutjs.php");//Throws error  

$q=$_GET["q"];

$link=mysql_connect("localhost","root","root")or die(mysql_error( ));

mysql_select_db("bwi",$link) or die(mysql_error( ));

$query=mysql_query("SELECT * FROM ".$q)or die(mysql_error());

while($row = mysql_fetch_array($query))

{

echo "<div id='rowcontainer" . $row['id'] . "' class='rowcontainer' title='Scroll to See more'>";

/*
verbose multi div html that works
*/

echo "</div></fieldset></div></div>";

echo "<link rel='stylesheet' type='text/css' href='styles/product.css'/>";

}

mysql_close($link);
?> 

Yeah it’s a week latter again without any valuable results from web searching the issue. There are plenty of nubies with way too many questions on php syntax errors caused by writing javascript code to a seperate js.php page so I guess that the php include method is the way to go if you want your javascript to run on php delivered html. Please let me know if I’m right or not.:nono:

You are trying to run Javascript as PHP which wont work.
Save checkoutjs.php as checkout.js and use the proper script src tag to include it in your getproduct page
<script src=“checkout.js”></script> above the <?php ?> tag.

PHP outputs the HTML for the javascript to hook on to so it should run after the output is generated.

well checkoutjs.php is the wrong idea, it is a javascript source not a php source so you will get tons of errors when php trys to parse javascript. change the checkoutjs.php to checkout.js then set proper links in your html to include the javascript.

spikeZ Thanks for the reply, are you saying I can put the script tag alone above the php tags like so
getproduct.php



<script language='javascript' src='scripts/checkout.js' type='text/javascript'></script>

<?php

#getproduct.php  

$q=$_GET["q"];


spikeZ and jgetner thanks for the responces I already create and append the script tag to the index.html with the ajax call and the script tag is there after the call but has no affect on the responceText HTML whatsoever?

Yeah fellas this is the create script tag function that runs when the ajax call is made with a click of a ul li element. It works because I setup another button onclick to reveal the new sript tags outerHTML which it does.
index.js


function loadjs(checkout){
var head=document.getElementsByTagName('head')[0];
var script=document.createElement('script');
script.language='javascript';
script.type='text/javascript';
script.src='scripts/checkout.js';
head.appendChild(script);
}

I also tried appending to the body tag with the same result, the javascript doesn’t affect the responceText html?

Hello, anybody out there? Feel like Major Tom trying to contact Ground Control.:smiley:

Ok. apparently there is no other way than to hard code the events into the get.php file like so:
get.php


//should have fetch_as_array statement to hold new html data in memory
echo "<img src='" . $row['src'] . "' class='image' onclick='pictureclick()' title='Click to get a better look'/></div>";

The ‘OnClick’ function is 'Hard Coded/Writen in-line of the HTML requested. As there is no window reload required by the Ajax call the function that makes the Ajax call actually writes the ‘src’ of a script tag with getelementbyid script tag id . This prevents appending or writing 10 or more of the same script tag to the document. If you force window reload the responceText HTML will disapear so the new script will draw errors like ‘object not found/object expected’. The responceText HTML has to be hardcoded to run the new external.js!
Add external.js with Ajax call


document.getElementById('blank').src='scripts/checkout.js';
};

index.html


<script id="blank" src="" type="text/javascript"></script>

Which makes me question why we decided to use ‘Unobtrussive or External.js’ in the first place as it actually adds a third step to comunicating with the server. The answer of course is security as this method doesn’t reveal the script src in the original page source code! Not that a hacker won’t look for it in the script file that is revealed. That becomes his only recourse and is an additional means of security.:blush: