/    Sign up×
Community /Pin to ProfileBookmark

PHP code in Javascript

Hello experts,

I want to run the below php code in a javascript function but its not working and showing error:

[code=php]
<script type=’text/javascript’>
function test()
{
<?php echo “Testing”; ?>
}
</script>
[/code]

Please help.

Regards.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@DasherDec 26.2011 — PHP is server based, and Javascript is client based. Anything that is being executed in the browser by definition is too late to run back to the server, and be executed. No way to do that.

In Javascript just put: document.write("Testing");
Copy linkTweet thisAlerts:
@TheBearMayDec 26.2011 — Could use AJAX to call a PHP script on the server, but otherwise the order of execution is always server based code (PHP, etc), and then HTML and client based code (javascript, etc.)
Copy linkTweet thisAlerts:
@007JulienDec 27.2011 — You can too write
[code=php]<script type='text/javascript'>
function test()
{
alert("<?php echo "Testing"; ?>");
}
test();// to call the function
</script>[/code]
Copy linkTweet thisAlerts:
@nyt1972authorDec 29.2011 — thanks a lot to all.
×

Success!

Help @nyt1972 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...