I have the following javascript code, which loads without error, however the update function does not actually seem functional, as get_Records.php is never loaded. I can not test if get_auction.php is loaded as it is loaded from within get_records.php
One of my main concerns is that I am doing the wrong thing by having update() take the paramters pk and query, as only one of them will ever be used. That seems like a bad hack, and poor logic, but I am not aware of a better way.
Here is the code
var xmlHttp var layername var url function update(layer, part, pk, query) { alert ('update'); if (part=='1') { alert ('part 1'); url = 'get_auction.php?cmd=GetAuctionData&pk='+pk+'&sid='+Math.random() } else if (part=='2') { alert ('part 2'); 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(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>)); child1.document.close(); }
I placed alert statements into the update function, andnot one is displayed, indicated the update function is never called?
I do not want to, and cannot use a framework, nor do I have access to use firebug, so please do not suggest these things. I am aware of them and use them when I can.
I would also like to know if calling php from within makewindows() is preferred to having makewindows simply take a parameter.., is there any advantage or disadvantage to each approach?
I seem to get an error when trying to call the function, this is how I am doing it in PHP:
echo '<li><a href='#' onclick=update('Layer3','2','0','hello')'>Link 1</a></li>' .
which makes this html, which should be fine?’\n’;
<li><a href='#' onclick='update('Layer3','2','0','hello')'>Link 1</a></li>
edit: I have taken tester101’S advice and changed it to this:
echo '<li><a href='#' onclick='update(\'Layer3\',\'2\',\'0\',\'hello\')'>Link 1</a></li>' . '\n';
Which still gives an error. I will probably end up using toms answer, but would like to know why this is not working.
You could just take Url as a parameter to update(), then you wouldn’t need to build the url within and you wouldn’t need the ‘optional’ parameters
Edit (response to comment)
If you have an update() function that just takes url and element id (layer) and updates it based on the result of an AJAX call, you could do something like this to remove the need for optional parameters and using a function to do two different things.
Then call either, depending on what you want to do:
or
e.g. in HTML
or to generate that in PHP