开发者

How do I write PHP within Javascript so that I can load files? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.)

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 th开发者_开发知识库e server.


You may use PHP to create a file that would be used as a JavaScript source.

<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.


Maybe you are talking about AJAX?

In this example, for simplicity, I'm using jQuery JavaScript library for AJAX calls.

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:

<div id="example"></div>

<script>
$(function(){
    $("#example").load('foo.php',{param:'header'});
    // html markup that php file returns - will be in #example div
}());
</script>


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜