在当前页面上显示php结果 [英] Displaying php results on current page
问题描述
我使用此编码,可以显示从下拉列表中选择的任何文件的.txt文件内容.
I have this coding that allows me show the .txt file contents of whatever file is selected from the drop down.
<form name="add" method="post" id="add" action="show.php">
Choose a file:
<select name="files" id="files" onchange="this.form.submit()">
<option value="File1">File1</option>
<option value="File2">File2</option>
</select>
</form>
带有相应的show.php(是的,目的是仅显示文件的最后三行):
with corresponding show.php (yes the purpose was to display only the last three lines of the file):
<?php
$ChosenFile = $_POST['files'];
$file = $ChosenFile.'.txt';
$contents = escapeshellarg($file);
$line = `tail -n 3 $contents`;
echo nl2br($line);
echo "<br><br>";
?>
试图使其显示在选择下拉列表下方的结果,而不是使用以下代码直接显示到php本身:
Trying to get it to display the results below the select drop down instead of direct to the php itself with this code:
<script>
$('#files').on('change', function(){
$.get('show.php', function(data);
$('#result').html(data);
});
});
</script>
<div id="result"></div>
该php可以工作并显示文本内容,但我无法在下拉菜单下方显示它.我想念什么?
The php works and displays text contents but I can't get it to display below the drop down. What did I miss?
推荐答案
从select
标记中删除onchanged
属性
Choose a file:
<select name="files" id="files">
<option value="File1">File1</option>
<option value="File2">File2</option>
</select>
您必须将表单数据发送到show.php
文件以获取正确的结果
you must send form data to show.php
file to get correct result back
<script>
$(function(){
$('#files').on('change', function(){
$.ajax({
url: 'show.php',
type: 'post',
data: $('form#add').serialize()
}).done(function(data) {
$('#result').html(data);
});
});
});
</script>
这篇关于在当前页面上显示php结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!