/    Sign up×
Community /Pin to ProfileBookmark

JavaScript and PHP

Would this code work?

[CODE]<script type=”text/javascript”>

if (somethin != true) {

<?php // do some server side processing ?>

}
</script>[/CODE]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 23.2008 — The PHP will have run by the time the JavaScript is executed, so no. You have to use AJAX.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJun 23.2008 — How would AJAX work any better than JavaScript?
Copy linkTweet thisAlerts:
@shakeukJun 23.2008 — because php is server side script and it will run b4 any information is even rceived by your browser, so javascript wont be able to run it like you have suggested.

ajax is a combination of javascript and a server side script (i.e. php) so have a look at this page it may help.

http://www.w3schools.com/php/php_ajax_intro.asp
Copy linkTweet thisAlerts:
@Declan1991Jun 23.2008 — AJAX tells the server to run the PHP when its supposed to, unlike the above, when it's being outputted to the browser.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJun 23.2008 — So with the code I wrote above, would it do what I wanted it to do if I just simply wrote it in AJAX instead of plain JavaScript?
Copy linkTweet thisAlerts:
@shakeukJun 23.2008 — yes and you could pull the result of the server side process could be displayed without having to reload the page at all. not like you would have to with php.
Copy linkTweet thisAlerts:
@Declan1991Jun 23.2008 — So with the code I wrote above, would it do what I wanted it to do if I just simply wrote it in AJAX instead of plain JavaScript?[/QUOTE]
The thing is that it is just plain JavaScript. Remember that what the browser gets has no PHP. So view the source if you ever want to check something.
×

Success!

Help @Joseph_Witchard 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,
)...