Is it possible with Javascript to dynamically generate php code and run it without reloading?
-
1javascript is a client language, php a server language... i think the answer is no... why you want this?silly– silly2012-03-09 09:36:54 +00:00Commented Mar 9, 2012 at 9:36
-
Yes, but don't do it. It would be horribly, horribly unsecure. If you described a bit what you want to accomplish, I'm sure that there's another way of getting to it.JJJ– JJJ2012-03-09 09:37:12 +00:00Commented Mar 9, 2012 at 9:37
-
1Why the heck would you want to do that??Phil Rykoff– Phil Rykoff2012-03-09 09:37:14 +00:00Commented Mar 9, 2012 at 9:37
-
1You should just send the parameters that set the "things", not the entire PHP code itself.JJJ– JJJ2012-03-09 09:39:27 +00:00Commented Mar 9, 2012 at 9:39
-
1You can hand over parameters (post and/or get, e.g. an articleId) to the PHP script via the Ajax call. The PHP script could analyze these and respond accordingly (e.g. respond with the corresponding article). This is common practice.Phil Rykoff– Phil Rykoff2012-03-09 09:39:39 +00:00Commented Mar 9, 2012 at 9:39
3 Answers
You have to understand something, Javascript is Client-Side language, and PHP is Server-Side, hence you can't use Javascript to "generate" PHP code.
What you can do is send data through a request via POST or GET using AJAX to a PHP file, which will do something with that data and then return a response without reloading the page.
1 Comment
Yes you can do it but it is pointless and dangerous. You can "generate" php code for some purposes /eg automated php script modification/ but you should never allow server side to directly execute unescaped input.
Looking at your question I suppose you dont clearly make the difference between Client and Server sides scripting. So make sure you realize it!
I suggest you think again and find better solution to your problem /or ask if you cant/.
Some further information : XSS