views:

146

answers:

3

Hello,

I have the following code, which is the core part of my small AJAX application. I am not getting any errors, it is just that nothing happens. I am guessing there is a more efficient way to do what I am trying to do.

Here is the code:

var xmlHttp

var layername

function update(layer, part, pk, query)

{

if (part=="1")

{

$url  "get_auction.php?cmd=GetAuctionData&pk="+pk+"&sid="+Math.random()

}

else if (part=="2")

{

var url  "get_records.php?cmd=GetRecordSet&query="+query+"&sid="+Math.random()

}

xmlHttp=GetXmlHttpObject()

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"

        }

    };

xmlHttp.open("GET",url,true)

xmlHttp.send(null)

}

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(json_encode(<?php echo $row2["ARTICLE_DESC"]; ?>));

child1.document.close(); 

}

and an example of how I am calling the function from php

onclick="update(\'Layer3\',\'2\','.$pk.'\',\'0\',)">'

pk or query will never be passed at the same time, only one of them will ever be passed.

edit: I am also wondering if it would make more sense for the makewindows function to take a parameter, or stay as it is. Are there advantages and disadvantages for each approach?

+1  A: 

I'd check the HTML the PHP is generating. Assuming $pk is a string it looks like you're missing an opening quote. Try this:

onclick="update(\'Layer3\',\'2\',\''.$pk.'\',\'0\',)">
Stoo
Is my logic the right way to do it though? Having the function accept two parameters if only one of them are ever used?
Joshxtothe4
+1  A: 

json_encode is a PHP function, and thus you need to modify that particular line like so:

child1.document.write(<?php echo json_encode($row2["ARTICLE_DESC"]); ?>);
Andreas Grech
That is not the cause of this problem however, the problem is that get_records is never called in the update function, not with makewindows.
Joshxtothe4
+1  A: 

Looks like you may have some javascript errors:

if (part=="1")
{
   $url  "get_auction.php?cmd=GetAuctionData&pk="+pk+"&sid="+Math.random()
}
else if (part=="2")
{
   var url  "get_records.php?cmd=GetRecordSet&query="+query+"&sid="+Math.random()
}

Use Firefox and Open the javascript console to get the javascript errors, then try to fix the lines it complains about.

Javascript will stop running as soon as it encounters an error.

Also, checkout firebug if you haven't already. Great tool!

Kieveli
I checked JSLint here http://www.jslint.com/ which gave mostly missing semicolons and linebreaking errors. When viewing the page I get no errors. Is my logic ok to use like this, if only one of p or query are ever used?
Joshxtothe4
Well, your url should be declared before the if statement so that when you use it in javascript further down, it finds one or the other. Also one is '$url' perl and the other doesn't have an '=', likeurl = "get_auction.php?blah";
Kieveli