Returning JavaScript through PHP and HttpPost
Posted: Fri Feb 05, 2010 10:06 am
I am writing a greasemonkey application. Part of the app posts the html content of the current page to a php parser. The parser then returns code to be injected into the current page. Part of this returned code includes javascript, but the code does not seem to work. Can anyone help me figure out why?
First the simple post script:
The above code works fine. The code gets the return value from a PHP file and then inserts it after the table23 element in the current page.
Now for the PHP.
The tables display properly, but the toggle function that hides and shows them does not work. Everything works fine if save the statement inside the output quotes to a text file and open it locally as a webpage.
First the simple post script:
Code: Select all
xmlhttp.open("POST",posturl,false);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send(input);
var accountData;
accountData = xmlhttp.responseText;
var main, account;
main = document.getElementById('table23');
if (main) {
account = document.createElement('account');
account.innerHTML = accountData;
main.parentNode.insertBefore(account, main.nextSibling);
}Now for the PHP.
Code: Select all
$output ="<script type='text/javascript' language='javascript'>
function accountToggle(id)
{
if(id == 'accountchart')
{
document.getElementById('accountchart').style.display = '';
document.getElementById('accountdocs').style.display = 'none';
document.getElementById('accountAdmin').style.display = 'none';
}
if(id == 'accountdocs')
{
document.getElementById('accountchart').style.display = 'none';
document.getElementById('accountdocs').style.display = '';
document.getElementById('accountAdmin').style.display = 'none';
}
if(id == 'accountAdmin')
{
document.getElementById('accountchart').style.display = 'none';
document.getElementById('accountdocs').style.display = 'none';
document.getElementById('accountAdmin').style.display = '';
}
}
</script>
<table id='account'>
<tr>
<td onClick=accountToggle('accountchart') Style='cursor:hand'>chart</td>
<td onClick=accountToggle('accountdocs') Style='cursor:hand'>docs</td>
<td onClick=accountToggle('accountAdmin') Style='cursor:hand'>Admin</td>
</tr>
<tr><td colspan='3'>
<table id='accountchart'>
<tr><td>chart</td></tr>
</table>
</td></tr>
<tr><td colspan='3'>
<table id='accountdocs' bgcolor>
<tr><td>docs</td></tr>
</table>
</td></tr>
<tr><td colspan='3'>
<table id='accountAdmin'>
<tr><td>Admin</td></tr>
</table>
</td></tr>
</table>";
print_r("$output");