JavaScript function not responsive

I have the following code which is the main part of my small AJAX application. I am not getting any errors, nothing is happening. I am guessing there is a more efficient way of doing 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 call the function from php

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

      

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

edit: I'm also wondering if it makes sense for the makewindows function to take a parameter or stay as is. Are there advantages and disadvantages for each approach?

0


source to share


3 answers


It looks like you might have 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 javascript errors and then try to fix the lines it is complaining about.



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

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

+1


source


I would check the HTML that PHP generates. Assuming $ pk is a string, it looks like you are missing an introductory quote. Try the following:



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

      

+1


source


json_encode

is a PHP function
, so you need to change this specific line like this:

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

      

0


source







All Articles