tags:

views:

328

answers:

3

Hello,

I have the following code, which will not work. The javascript gives no errors and appears to load fine. but clicking on a link will do nothing. An example of a link is:

<a href="#" onclick="updateByQuery('Layer3', "Ed Hardy");">Link 1</a><li>Link 2</li>

and the code:

var xmlHttp
var layername
var url
function update(layer, url) {
    var xmlHttp=GetXmlHttpObject(); //you have this defined elsewhere

    if(xmlHttp==null) {
        alert("Your browser is not supported?");
    }

    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            document.getElementById(layer).innerHTML=xmlHttp.responseText;
        } else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
            document.getElementById(layer).innerHTML="loading";
        }

       //etc
    }

    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function updateByPk(layer, pk) {
   url = "get_auction.php?cmd=GetAuctionData&pk="+pk+"&sid="+Math.random();
   update(layer, url);
}


function updateByQuery(layer, query) {
   url = "get_records.php?cmd=GetRecordSet&query="+query+"&sid="+Math.random();
   update(layer, url);
}

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        xmlHttp=new XMLHttpRequest();
    }catch (e)
    {

        try
        {
                xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}

    }
return xmlHttp;
}
function makewindows(){
child1 = window.open ("about:blank");
child1.document.write(<?php echo htmlspecialchars(json_encode($row2["ARTICLE_DESC"]), ENT_QUOTES); ?>);
child1.document.close(); 
}
+4  A: 

It may probably be due to the double-quote characters surrounding 'Ed Hardy'. Does this work:

<a href="#" onclick="updateByQuery('Layer3', 'Ed Hardy');">Link 1</a><li>Link 2</li>
ayaz
i don't know why there are double characters. The line I am using to produce in PHP is: echo '<a href="#" onclick="updateByQuery(\'Layer3\', ' . json_encode($query) . ');">Link 1</a>';
Joshxtothe4
A: 

From the wonderful JSLint

You are missing semicolons after these

var xmlHttp
var layername
var url

xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            document.getElementById(layer).innerHTML=xmlHttp.responseText;
        } else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
            document.getElementById(layer).innerHTML="loading";
        }

       //etc
    }

(e) is used 2x, change the second to 'ex'.

 try
    {
        xmlHttp=new XMLHttpRequest();
    }catch (e)
    {

        try
        {
                xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}

    }

Try using single quotes for EVERYTHING in JS, and use double quotes for EVERYTING in PHP.

Get Firebug to see if there are any other syntax errors.

StingyJack
Yes, I said 'EVERYTING' =)
StingyJack
A: 

Additionally, this line:

child1.document.write(<?php echo htmlspecialchars(json_encode($row2["ARTICLE_DESC"]), ENT_QUOTES); ?>);

Should probably include quotes around the PHP reference, so JavaScript knows what you want it to write:

child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");

Note that I changed the double-quotes surrounding ARTICLE_DESC to single-quotes as well.

cLFlaVA
I fixed this, but nothing actually got fixed. The link still has double quotes in it as ayaz pointed out.
Joshxtothe4