Displaying php results on current page


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>

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>"; ?>

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>

The php works and displays text contents but I can't get it to display below the drop down. What did I miss?


remove onchanged attribute form select tag

Choose a file: <select name="files" id="files"> <option value="File1">File1</option> <option value="File2">File2</option> </select>

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>


As your original html and php uses POST, you would need to use POST in your ajax script as well.

And you are not sending any data to your script.

So the result should be something like:

$('#files').on('change', function(){ $.post('show.php', $(this).closest('form').serialize(), function(data); $('#result').html(data); }); });

As mentioned in the comments, you also only need one event handler.


  • Permission denied to access the file from php script
  • Passing variables from PHP to PERL [closed]
  • zip -P command not working to create password protect zip file
  • Get value of selected drop down list item
  • how to include a file inside a model in codeigniter?
  • F# signature file error
  • How to upload excel file in angular js?
  • F# NativePtr.stackalloc Unexpected Stack Overflow
  • Ansible sudo hangs after 5 tasks
  • Html select multiple get all values at onchange event
  • Intellisense keeps showing errors while build is successful Visual Studio 2015
  • Setting the run time properties on SpringApplicationBuilder()
  • how to set variables in a php include file?
  • How to print columns containing value
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Button click event not firing in jQuery
  • What's the purpose of QString?
  • How do I access an unhandled exception in an MVC Error view?
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • JSON response opens as a file, but I can't access it with JavaScript
  • Google Custom Search with transparent background
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • req.body is undefined - nodejs
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • vba code to select only visible cells in specific column except heading
  • How can I use Kendo UI with Razor?
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • How do you join a server to an Active Directory (domain)?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Why joiner is not used after Sequence generator or Update statergy
  • How does Linux kernel interrupt the application?
  • Busy indicator not showing up in wpf window [duplicate]
  • To Get the radio button value in ruby on rails