我有一个表单,可以从服务器中选择文件,然后在更改时将文件名和内容放入文本区域。如果我将其放在/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> <input name="USER" value="" SIZE=40 name="CodeValue" id="CodeValue" /> <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: </font><input name="SAYS" value="Enters the room..."><font color=#33A6A6>History: </font><input name="HISTORY" value="20" size=2><font size="-1"><font color=#33A6A6>No Pics: </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/