• Calling Javascript functions from PHP
    33 replies, posted
I'm making a website for my Computer Science class and to simplify my work, I used PHP to call Javascript functions. Something like this: [code] echo '<script type="text/javascript">', 'runFunction('.arg1.','.arg2.')', '</script>'; [/code] But, for some reason, none of the functions I tried to run that way have worked and I'm not sure why. Anyone know?
Make sure you're echoing that after the function is declared and inside the html tags. Also check the developer console for any errors.
PHP variables need to be prefixed with $.
[QUOTE=supersnail11;44810689]PHP variables need to be prefixed with $.[/QUOTE] They are defined that way in my code, I forgot about it here because I'm on my phone atm. [QUOTE=Bo98;44810674]Make sure you're echoing that after the function is declared and inside the html tags. [/QUOTE] Yep, that did the job. Thanks!
[CODE]echo "<script type='text/javascript'>, runFunction('.arg1.','.arg2.'), </script>"; [/CODE] Look at the code, remember to wrap your things properly, like how you did it before wasnt making a full string to echo, but rather was splitting it up you can see it in the code window, if you look at the color.
is the function trying to manipulate the DOM? if so, wrap it in a document.ready
He has already solved the problem guys.
[QUOTE=Bo98;44814723]He has already solved the problem guys.[/QUOTE] Which doesn't mean that we can't give supplemental advice.
[QUOTE=Alternative Account;44815350]Which doesn't mean that we can't give supplemental advice.[/QUOTE] Coming from a guy web browsing on a Wii
Got two of three pages working, can't seem to get the third one working. This is my php script: [code] <?php $data=date("d.m.Y"); $dzien=date("D"); echo $data, $dzien; if( isset( $data ) && isset( $dzien ) ) { echo '<script type="text/javascript"> setDate( '.$data. ',' .$dzien. ' ) </script>'; } ?> [/code] and my Javascript code: [code] function setDate( data, dzien ) { document.getElementById( "test" ).innerHTML = "Hello World!"; var dni = { Mon: 'Poniedzia&#322;ek', Tue: 'Wtorek', Wed: '&#346;roda', Thu: 'Czwartek', Fri: 'Pi&#261;tek', Sat: 'Sobota', Sun: 'Niedziela' }; var day = dni[dzien]; var p1 = "Dzisiaj jest "; alert( day ); alert( p1 ); var text = p1.concat( day, " ", data ); alert( text ); document.getElementById( "test" ).innerHTML = text; } [/code] Yes, Javascript is above php.
[QUOTE=Shadaez;44814261]is the function trying to manipulate the DOM? if so, [B]wrap it in a document ready[/B][/QUOTE] ^
[QUOTE=Shadaez;44817086]^[/QUOTE] How do I do that? I looked up document ready and found only jQuery stuff.
you could just put the script after the body tag then, or use window.onload
[QUOTE=Shadaez;44817129]you could just put the script after the body tag then, or use window.onload[/QUOTE] you mean before you end the body tag. right? :v:
[QUOTE=jung3o;44817190]you mean before you end the body tag. right? :v:[/QUOTE] i guess that's the right way, i've never used the method of putting it at the end of the html, just figured after body made the most sense you're supposed to put it right before the </body> apparently
Yes you put it right before the body end tag. After it will work but will not pass HTML5 standards
[QUOTE=djjkxbox360;44816417]Coming from a guy web browsing on a Wii[/QUOTE] Yes, he's browsing on Internet Explorer on a Wii.
[QUOTE=supersnail11;44817895]Yes, he's browsing on Internet Explorer on a Wii.[/QUOTE] Well it's obviously not Internet Explorer, the Wii browser is just being perceived as that
[QUOTE=djjkxbox360;44818530]Well it's obviously not Internet Explorer, the Wii browser is just being perceived as that[/QUOTE] do you need help? [url]https://chrome.google.com/webstore/detail/user-agent-switcher-for-c/djflhoibgkdhkhhcedjiklpkjnoahfmg?hl=en-US[/url]
He's on a Wii, not a computer with Google Chrome you moron
[QUOTE=djjkxbox360;44818638]He's on a Wii, not a computer with Google Chrome you moron[/QUOTE] He's spoofing his useragent you idiot. [editline]15th May 2014[/editline] The Wii useragent is [code]wii libnup/1.0[/code] The Internet Explorer useragent is [code]Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0[/code] How would you ever get those confused?
It says he's on a Nintendo Wii. Whatever, why would you spoof it to a Nintendo Wii anyway?
[QUOTE=djjkxbox360;44818784]It says he's on a Nintendo Wii. Whatever, why would you spoof it to a Nintendo Wii anyway?[/QUOTE] It also says he's on Internet Explorer. It's a joke.
[QUOTE=supersnail11;44818929]It also says he's on Internet Explorer. It's a joke.[/QUOTE] The Wii web browser is old so it can't identify the user agent properly, so it reports as Internet Explorer
[QUOTE=djjkxbox360;44818972]The Wii web browser is old so it can't identify the user agent properly, so it reports as Internet Explorer[/QUOTE] Now I see why AA decided to change his user agents :suicide: [editline]15th May 2014[/editline] maybe i should browse like this too. :v: [editline]15th May 2014[/editline] got my wii :v:
[QUOTE=djjkxbox360;44818972]The Wii web browser is old so it can't identify the user agent properly, so it reports as Internet Explorer[/QUOTE] I'm sure.
[QUOTE=djjkxbox360;44818972]The Wii web browser is old so it can't identify the user agent properly, so it reports as Internet Explorer[/QUOTE] I don't know what Garry's smoking if his code identifies the Wii's useragent as Internet Explorer.
f12 > open drawer(console) > emulation > User Agent > Other now you guys can quit flabbergasting
By popular request, I am now using the Playstation 3 browser on a Nintendo Wii. This really makes for a much better browsing experience. Thanks, Facepunch!
Well I didn't know he was using a useragent string spoofer did I? I've seen Opera come up as Safari and other browsers come up wrong (without using a useragent spoofer)
Sorry, you need to Log In to post a reply to this thread.