Help please; getting desperate

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
washingtonmike
Forum Newbie
Posts: 3
Joined: Sat Jun 20, 2009 11:30 am

Help please; getting desperate

Post by washingtonmike »

I apologize in advance for being a little "thick" but I've been working on this script for 2 days straight, tried literally dozens of tips from the inet and forums and still hitting the wall. Will someone please help this new guy out?

I built a php webpage for an organization with multiple "locations". Each location is referenced in the body html in a <div> tag, initially set to visible (by default or other).

In php I'm reading an external text file, pulling out the tag id for the location I want to change from "visible" to "invisible". I have this code in a Javascript function:

<script languge="javascript" type="text/javascript">
function toggle(location) {
if (docment.getElementById(location).style.visibility=="hidden") {
document.getElementById(location).style.visibility = "visible";
} else {
document.getElementById(location).style.visibility = "hidden";
}
}
</script>

installed in the header section. the php variable is generated in the body from an array. Getting the array element value to a php variable is certainly no problem but all the variations (most using the echo statement) I've tried so far to get the php variable into the javascript variable "location" refuse to work (both with or without explicit declaration).

Most appreciative of any help I can get. Thanks, Michael
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Help please; getting desperate

Post by Eric! »

Its been almost 3 years since I did the php to javascript game, but this is how I did it:
php script starts first, gets/defines all data then exits to countinue with html code. in the java function try this

Code: Select all

       var location = <?php echo($location);?> ; // example with numbers
        var location = "<? echo $location; ?>"; // example get php string
Also I had to filter the php strings before passing them to java, see example

Code: Select all

       $testvar = str_replace(Chr(13), "", $testvar); // remove carriage returns for javascript
       $testvar = str_replace(Chr(9), "", $testvar); // remove tab for javascript
       $testvar = str_replace(Chr(10), "", $testvar); // remove newline for javascript
       $testvar = str_replace(Chr(0), "", $testvar); // remove nulls for javascript
 
Can you please change the title of your post to something descriptive? Like help passing data from php to javascript? You'll find people with specific knowledge about it are more likely to read your messages and help that way.
Post Reply