如何显示在一个textarea一个文件的内容不被重定向到一个新的一页 [英] How to display the contents of a file in a textarea without being redirected to a new page

查看:174
本文介绍了如何显示在一个textarea一个文件的内容不被重定向到一个新的一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php

    $blacklist = array("one.jps", "two.txt", "four.html");
    $files = array_diff(glob("*.*"), $blacklist);

    foreach($files as $file)
        echo "<div class='post'><a href='" . $_SERVER['PHP_SELF'] . "?file=" . $file . "'><p>" . $file . "</p></a></div>";


    if(!empty($_GET["file"]) && !in_array($_GET["file"], $blacklist) && file_exists($_GET["file"])) 
        $thesource = htmlentities(file_get_contents($_GET["file"]));

?>

<textarea rows="40" cols="100" placeholder="Source code of file" class="source"><?php if(!empty($thesource))echo $thesource; ?></textarea>

这是我使用的回声的一切在一个目录(除了那些在黑名单​​中)里面的文件上面的code。当用户点击该文件,源$ C ​​$ C(包含在该文件中的所有数据)显示在文本区域。

The above code that I am using echo's all the files that are inside a directory (apart from those in the blacklist). When the user clicks on that file, the source code (everything contained inside that file) is displayed in the textarea.

点击该文件将显示源$ C ​​$ C在textarea的,但是当你点击该文件,发送到源$ C ​​$ C显示在textarea的新的一页。总之,code的作品,但你被发送到一个新的页面。我该如何让这个一切发生在当前页面,你上。

Clicking on the file will display the source code in the textarea but when you click on the file, you are sent to a new page where the source code is displayed in the textarea. In short, the code works but you are sent to a new page. How do I make it so that everything happens on the current page that you are on.

我相信我会需要使用AJAX,但我会怎么做呢?

I believe I would need to use AJAX but how would I do so?

需要注意的是,当我说的文件,我说的那些呼应出到页面上的目录中的文件。他们在里面&LT嵌套; P>&LT; / p>元素

推荐答案

使用AJAX来加载文件内容到文本区域。

Use ajax to load the file content to the textarea.

注意

我所承担的文字区域有一个id =源代码在这个例子中。

I have assumed the textarea has an id = "source" in this example.

URL-这里是页面的URL与其中显示该文件的内容的GET参数。例如, http://example.com/get_file.php?file=abc.txt

url- here is the page url with the get parameters where the content of the file is displayed. eg, http://example.com/get_file.php?file=abc.txt

function loadDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("source").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

修改

您需要一个的onclick 事件添加到按钮来调用上面的函数。

You need to add a onclick event to the button to call the above function.

继承人,一个的jsfiddle

这篇关于如何显示在一个textarea一个文件的内容不被重定向到一个新的一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆