问题
So I create text(php) editor and I want to execute PHP code without refreshing page. Now I use cookies to transfer code from javascript(which grab editor content). Then color box opens and PHP file write cookie content to file, which is included. And there is question:
Is there any other way to transfer PHP code from javascipt to PHP? Or any other way to execute PHP code? I know there is a AJAX, but I want to transfer a larger data including special characters(",',],[,$ etc.), that occur in PHP.
Yes, I use it only on localhost for education purpose only :) Sorry for my english, I hope you understand me.
回答1:
AJAX is suitable for sending large amounts of code. Take a look at jQuery.post and jQuery.ajax. Special characters pass through POST-request unchanged.
Make sure you set php setting magic_quotes_gpc
to off
, as this option tells PHP to escape any special character with backslash.
回答2:
You can still use AJAX, only with POST, this time. Try something like:
<script>
code = '<? echo "Hi"; ?>'
ajax = new XMLHttpRequest();
ajax.open("POST","http://foo.com/bar.php";
ajax.send("code="+code+"&foo=bar")
</script>
Look at https://developer.mozilla.org/en/ajax to learn more about Ajax.
来源:https://stackoverflow.com/questions/7010506/how-to-transfer-php-code-from-javascript-to-php