Returning JavaScript through PHP and HttpPost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
New Reverie
Forum Newbie
Posts: 2
Joined: Thu Feb 04, 2010 8:52 pm

Returning JavaScript through PHP and HttpPost

Post by New Reverie »

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:

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);
                }
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.

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");
 
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.
New Reverie
Forum Newbie
Posts: 2
Joined: Thu Feb 04, 2010 8:52 pm

Re: Returning JavaScript through PHP and HttpPost

Post by New Reverie »

An additional note. Even a very simple script such as this:

Code: Select all

$output ="<script type='text/javascript'>
document.write('<a>Hello World!</a>');
</script>";
print_r("$output");
 
Does not seem to execute. Is it possible that the page i am trying to inset the javascript into has some protection built in that does not allow it to be executed?
Post Reply