javascript - PHP 获取文本区域的文件内容

标签 javascript php html

我有一个表单,可以从服务器中选择文件,然后在更改时将文件名和内容放入文本区域。如果我将其放在/evo/的基本目录中,其中保存所有内容(如/evo/users/username 中提取文件的位置),则效果很好。但是,当我将此页面移至文件夹更深处时,它无法解析下拉列表中的文件。我必须添加

$_SERVER['DOCUMENT_ROOT']

到文件,以便它找到要在下拉列表中正确显示的文件,但是当它尝试将它们放入文本区域时,它会显示:

Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: GET "http://mywebsite.site/home/revo/public_html/evo/users/Addiction/Addiction.html".

这意味着我找到它们的目录是错误的,或者(希望并且可能)我在底部的功能更改中回显或使用它的方式是错误的。未正确加载的文本区域是显示文件内容的“CodeValue”框。

<select size="1" name="CodeList" id="CodeList"><option selected disabled>(Select Your Code)</option>
<?php
$directory = $directory = $_SERVER['DOCUMENT_ROOT'] . '/evo/' . '/users/' . $_SESSION['username'];
$filesContents = Array();
$files = scandir( $directory ) ;

foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
$filesContents[$file] = file_get_contents($directory , $file);
echo '<option value="'. $file .'">' . $file . '</option>';
}
}
?>
</select>
            <input type="hidden" name="CodeId" id="CodeId" value="0" />
            <input type="hidden" name="CodeDescription" size="40" maxlength="50" id="CodeName" value="" />
            <font color=#33A6A6>Code:</font>&nbsp;<input name="USER" value="" SIZE=40 name="CodeValue" id="CodeValue" />&nbsp;&nbsp;&nbsp;<input type=hidden name="ACTION" value="says to"><input type=hidden name="WHOTO" value="ALL"><input type=submit value="Enter"><font size="-1"><font color=#33A6A6>Entrance:&nbsp;</font><input name="SAYS" value="Enters the room..."><font color=#33A6A6>History:&nbsp;</font><input name="HISTORY" value="20" size=2><font size="-1"><font color=#33A6A6>No Pics:&nbsp;</font><input name="NOPIC" value="1" type="checkbox" checked></form>
<script>
$(document).ready(function(){
    // apply a change event
    $('#CodeList').change(function() {
      // update input box with the currently selected value
        $('#CodeName').val($(this).val());
        $.get( '<? echo $directory ?>' + '/' + $('#CodeName').val(), function( data ) {
         $( "#CodeValue" ).val( data );
        });
  });
 });
</script>

最佳答案

错误消息表明 AJAX 请求发送至此 URL:

http://mywebsite.site/home/revo/public_html/evo/users/Addiction/Addiction.html

而我认为你希望它去:

http://mywebsite.site/evo/users/Addiction/Addiction.html

既然您说 /evo/ 是基本目录。输出的脚本包括服务器上的内部目录路径,而不是外部可见的 HTTP 路径。您可以通过在顶部执行此操作来更改此设置:

$httpPath = '/evo/' . '/users/' . $_SESSION['username'];
$directory = $directory = $_SERVER['DOCUMENT_ROOT'] . $httpPath;

然后在脚本输出中:

$.get( '<? echo $httpPath ?>' + '/' + $('#CodeName').val(), function( data ) {

关于javascript - PHP 获取文本区域的文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29112202/

相关文章:

javascript - 如何使 AngularJS 指令在不隔离范围的情况下明确定义其依赖项

javascript - 从 javascript/jquery 创建 Flash 消息

php - 在 WC 3.0+ 的单品页面中显示接近销售价格的折扣百分比

php - PHP 应用程序是否应该对不正确的数据库值执行错误处理?

html - 使用 winjs 将 webview 捕获为图像快照

javascript - Reactjs 中是否有 setState() 的同步替代方案

javascript - onkeyup 字符在字符串中的位置

php - 使用 php 以 UTF-8 格式导出到 CSV for Excel

python - 如何读取 Pandas 中的html表并输出到数据框而不是列表

javascript - Ajax 通过 post 将文本从一个文本区域复制到另一个文本区域