Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

this is probably a very stupid question, but here it goes anyways... would it be possible to somehow access PHP header variables like the $_POST variable for instance, from within a HTML page, using Javascript/jQuery?

The problem is that I have a site that uses a PHP CMS but the templating system uses .html files and pre defined tags, so basically I would like to be able to use PHP on the HTML template files somehow.

I have tried all of the methods shown on this site, but my PHP seems to be showing up in the source of the page as an HTML comment, like so,

<!--php echo "hello world" -->

any idea how I can get around this problem?

Thanx in advance!

share|improve this question
1  
Could you use AJAX to call another PHP script? – Mild Fuzz May 5 '11 at 12:07
Which CMS are you using? – Nick Pyett May 5 '11 at 12:15
@Mild: Why would one need to rely on JavaScript if one merely wants to insert a POST parameter into the markup? Moreover, the parameter will not be available anymore when the page loading is finished, so you'll have to work around that. BTW, don't forget to protect against XSS attacks. – Marcel Korpel May 5 '11 at 12:17
@Nick: It is actually a shopping cart with built in CMS functionality, so I can understand them removing PHP from the templating system. – Odyss3us May 5 '11 at 12:27
@Marcel: Thanks, will do! – Odyss3us May 5 '11 at 12:31

3 Answers

If you let PHP process your HTML files by changing .htaccess accordingly, the most likely thing that goes wrong is that you don't use proper PHP tags. Change your example to

<?php echo "hello world" ?>

and see what happens.

share|improve this answer
My PHP tags are fine in the template file, but then when I load the page, it shows up as an HTML comment, so it might be getting parsed along the way? – Odyss3us May 5 '11 at 12:07
1  
@user: that sounds weird; what do you use to process those template files? It might be some security setting that disallows the use of PHP in there. – Marcel Korpel May 5 '11 at 12:09
It's a Interspire product, I'm thinking that is probably it, it's understandable, but very very limiting. -_- – Odyss3us May 5 '11 at 12:12
@user I don't know those, but I can't think of anything else. – Marcel Korpel May 5 '11 at 12:14
No worries, thanx for the help! ;) – Odyss3us May 5 '11 at 12:29

You cannot use Javascript/jQuery to access PHP header variables. The reason behind it is because this kind of information is only available at the server side of things since they're related to HTTP and javascript is a client side language that knows nothing about the server.

What you could do is use PHP to save the HTTP header's information in a variable and do whatever you want to do with it.

share|improve this answer

If your HTML pages are not being interpreted by the PHP interpreter (indicated by the PHP statements in your source), you cannot access any PHP header variables.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.