views:

89

answers:

6

I know JavaScript is client side and PHP is server-side. I also know this is an odd question. However, the CMS editor I'm using will not allow php to be stored to the database (it's the same editor used by Dupral). I can store JavaScript though.

Can I import a php file with JavaScript. Or rather read the php file, store the content in a variable then out put the content to the screen?

+2  A: 

short answer: no. long answer: probably not.

reason: see first sentence of your question

zaphod
:::headdesk::: fantastic
dcp3450
I saw the first sentence, but didn't find a reason.
Ben Shelock
@Ben Shelock, The reason is because Javascript is clientside and PHP is serverside
Starx
after some thought I need this to work when javascript is disabled. I'll just do it by hand with straight php.
dcp3450
A: 

As long as the server you're requesting is configured to server PHP pages and NOT show them, you can never get the contents of the file, it will always be served.

Babiker
A: 

I really liked the answer you received by zaphod. Anyway, there are some circumstances under it could be possible: if the editor allows you to insert full JavaScript, and the PHP file that you want to invoke is in the same server, you could try to execute it using AJAX. If you want to retrive its content, you will have to use another php file that reads it and serves it to you.

Cristian
A: 

No, it's like saying: "if I change the words on my CD's booklet, will the songs sound different?"

kemp
Explain that analogy please. Im intrigued.
Ben Shelock
By the time your browser receives the Javascript code to execute, the PHP interpreter is already done.
kemp
+1  A: 

Even though you manage to store PHP codes inside javascripts like document.write("<? echo $myvar; ?>"); the portion <? echo $myvar; ?> will simply display as text because only server can parse this type of codes.

However, through AJAX, you can execute the PHP code through Javascript and import the results to display to the user.

Starx
A: 

You can't import php from Javascript, but you can make AJAX call from your javascript to a PHP page to get his result and then add the result to your page.

HoLyVieR