如何在 Javascript 中编写 PHP 以便我可以加载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7068691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I write PHP within Javascript so that I can load files?
提问by Kristen Martinson
How do I write PHP within Javascript so that I can load files? Or is there a better way? (I am loading text files from the server... so I need to use PHP to load those files. I don't know how to handle call backs in PHP but I can do it in Javascript. And from there if I can do some PHP from within the Javascript, I can solve my problem.)
如何在 Javascript 中编写 PHP 以便我可以加载文件?或者,还有更好的方法?(我正在从服务器加载文本文件......所以我需要使用 PHP 来加载这些文件。我不知道如何在 PHP 中处理回调,但我可以用 Javascript 来完成。如果我可以的话,从那里开始Javascript 中的一些 PHP,我可以解决我的问题。)
thx
谢谢
desired sequence:
SaveButton
LoadButton
When the "load button" is pressed, load a textfile from the server into a textbox.
When the "save button" is pressed, save the textbox text to the server.
回答by John Giotta
You may use PHP to create a file that would be used as a JavaScript source.
您可以使用 PHP 创建一个用作 JavaScript 源的文件。
<script type="text/javascript" src="/my/js/file.php"></script>
or
或者
<script type="text/javascript">
var str = '<?php echo 1+1;?>';
</script>
PHP is serverside. JavaScript is clientside. So you cannot execute either language in the wrong platform.
PHP是服务器端。JavaScript 是客户端。所以你不能在错误的平台上执行任何一种语言。
回答by Larry Cinnabar
Maybe you are talking about AJAX?
也许你在谈论 AJAX?
In this example, for simplicity, I'm using jQuery JavaScript library for AJAX calls.
在此示例中,为简单起见,我使用 jQuery JavaScript 库进行 AJAX 调用。
foo.php:
foo.php:
<?php
$param = $_POST['param'];
if ( $param == 'header' ) {
$markup = '<div class="header">Hello</div>';
} else if ($param == 'content') {
$markup = '<div class="content">Main div</div>';
} else {
$markup = '<div></div>';
}
return $markup;
?>
bar.html:
酒吧.html:
<div id="example"></div>
<script>
$(function(){
$("#example").load('foo.php',{param:'header'});
// html markup that php file returns - will be in #example div
}());
</script>
回答by Sarel Botha
You don't specify if the file is on the server or on the client. If it's on the client side you can't access it. The user must first upload it to your site. You can do this via a form.
您没有指定文件是在服务器上还是在客户端上。如果它在客户端,您将无法访问它。用户必须首先将其上传到您的网站。您可以通过表单执行此操作。
If the file is on the server you can use javascript/ajax to reference the file. If you need to do some kind of processing on the file that should be done in the PHP when the file is uploaded.
如果文件在服务器上,您可以使用 javascript/ajax 来引用该文件。如果您需要对文件进行某种处理,应该在上传文件时在 PHP 中完成。
Edit following clarification: You basically want to use AJAX requests to transfer data from the browser (javascript) to the server (PHP) and back. A library like this may help: http://www.modernmethod.com/sajax/. The graffiti example looks similar to what you're talking about.
编辑以下说明:您基本上希望使用 AJAX 请求将数据从浏览器 (javascript) 传输到服务器 (PHP) 并返回。像这样的图书馆可能会有所帮助:http: //www.modernmethod.com/sajax/。涂鸦示例看起来与您所说的相似。