Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using ajax to replace javascript

Status
Not open for further replies.

trojanFisch

Programmer
Joined
Aug 5, 2006
Messages
2
Location
US
i have a php file that generates some javascript for a page. the my problem is is that things change on the page that the javascript cant account for.

now that i've described the general problem, i'll go into the specifics of my issue:

on the page there is a table, and when one of the cells is right clicked, a dropdown menu is displayed in that cell. when the user selects one of the options in the cell, the dropdown goes away and the option that was selected is displayed in the cell.

when one of the options is selected in one of the rows, that option need to be a different color in the dropdown list.

my problem lies with the options changing color.


i recently inherited this program, and the way it used to run was that the entire page was reloaded when a change was made, but i wos hired to implement ajax methods to not have the pages refresh but rather dynamically reload.

so when the page refreshes the new javascript is reflected, but i cannot seem to dynamically replace the current javascript with the javascript that is generated with php.

this is the code that calls the php code that generates the javascript:


function updateJS(){

http_request_update_js = getHttpRequest();

var url="Javascript2.phtml";
url=url+"?sid="+Math.random();
http_request_update_js.onreadystatechange=stateChangedUpdateJS;
http_request_update_js.open('GET',url,true);
http_request_update_js.send(null);

return;
}

function stateChangedUpdateJS() {
if (http_request_update_js.readyState==4 || http_request_update_js.readyState=="complete"){
if (http_request_update_js.status == 200) {
//alert("No problem with the request");
JS.innerHTML = http_request_update_js.responseText;
http_request_update_js = false;
} else {
alert('There was a problem with the stateChangedUpdateJS request.');
}
return;
}
}


and this is how i thought i could put the new javascript into my page, but appearently this method doesnt work with javascript.


<head>
<div id="JS">
<? include("Javascript.phtml");?>
</div>
</head>

if anyone could help with a solution or throw me a suggestion that'd be great
 
I am not sure why you don't just have all the javascript functions you want to use included on the page you are working on... and use them when you need them.. why do you have to include them at certain times?
 
it would be almost impossible to code all the different states the drop down menu could have. the reason the javascript code needs to be regenerated by the php is because the new javascript reflects changes made to the database. if javascript could interact directly with my database i would do that, but i cant.


this is the php code that generates the javascript code i am talking about:

//renders the dropdown list for grouping characters
//Although it might look like javascript is calling PHP which in turn
//calls database functions (wouldn't that be cool?), what actually
//happens is when the javascript gets streamed to the client
//it already has the dynamic content embedded and has already talked to the database
function GroupDropdown(id,callid,groupid)
{
var newHtml;

var aGroups = new Array();

<?

//we map a GRP_ID to a GRP_CHAR in the GROUPS table
//so when a call gets assigned a grouping character actually store the integer
//id of the character and not the character itself, this simplifies things in
//the end and allows us to have grouping characters of several characters,
//such as van-a
$sSql = "SELECT * FROM GROUPS WHERE GRP_ID != 0"; //dont want the first null value

$dbResult = $cDB->Query($sSql);


$bContinue = true;
for ($i = 1; $bContinue; $i++)
{
$bContinue = $dbResult->fetchRow();

if (true == $bContinue)
{
$iGrpID = $dbResult->getField("GRP_ID");
$sSql = "SELECT * FROM CALLS WHERE GROUP_ID=$iGrpID AND CLOSED != 1";
$RS_callsWithGroup = $cDB->Query($sSql);
if ($RS_callsWithGroup->fetchRow())
$sColor = "red";
else
$sColor = "black";

$iArrayIndex = $i - 1;
$sGrpChar = $dbResult->getField("GRP_CHAR" );

//because the results of this function are being embedded in javascript,
//we need to store it in a javascript data structure, in this case, an Array
echo "aGroups[$iArrayIndex] = new Array($i, '$sGrpChar','$sColor');//color=$sColor\n";
}
}

?>

newHtml = '';
newHtml += '<IMG src="images/c.gif">';

//callid and groupid are parameters of this javascript function passed by the row which is calling it
newHtml += '<SELECT style=\'font-size: 7pt;\' onblur=\'DoGroupChanges('+callid+','+groupid+',this.options[this.selectedIndex].value,false);\' onchange=\'DoGroupChanges('+callid+','+groupid+',this.options[this.selectedIndex].value,false);\'><OPTION id=opt0 id=0>None';

//make use of the array we just populated from the database
for (i = 0; i < aGroups.length; i++)
{
iKey = aGroups[0];
sName = aGroups[1];
sColor = aGroups[2];
newHtml += '<OPTION id=opt' + iKey + ' value=' + iKey + ' style="color:' + sColor + '"';

if (groupid == iKey)
newHtml += ' SELECTED';

newHtml += '>' + sName;
}

newHtml += '</SELECT>';

ReplaceContents(id,newHtml);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top