0

So I have multiple anchors that will get their id and text from a directory, these are ip addresses.

I can get the ip address from each when clicked into the title of a modal that the anchors open using this javascript in my php file:

<script>
        $(document).on('click', 'a', function () {
             document.getElementById("ipaddresstitle").innerHTML = this.id;
             var idoftitle = this.id
        });
</script>

But I want a file to be displayed in the div 'modal-body'. The file wants to be for example /etc/ip_files/192.168.0.1 if I click 192.168.0.1 button. There will be a file that corresponds to each anchor.

I used this php before:

nl2br( file_get_contents('/etc/ip_files/192.168.0.1') );

it works manually. How can I make the div display the different text file each time I click one?

Is there a way that Javascript can output the file for me? Instead of using php?

  • Duplicate of https://stackoverflow.com/questions/371875/local-file-access-with-javascript? – Tim Morton Apr 29 '18 at 00:47
  • I am meaning a file on the server not the client – AvidPontoon Apr 29 '18 at 00:47
  • But the javascript is being executed on the client. Would you want any client having access to your server's file system? I would hope not. – Tim Morton Apr 29 '18 at 00:51
  • The only way here since you want to load dynamic data from the server without reloading the page each time is to send an AJAX request with Javascript (on the client side) to your PHP script on the server side. – Mouradif Apr 29 '18 at 01:01

1 Answers1

1

You can use ajax to send the ip address to the server to read the and send back the contents

<script>
        $(document).on('click', 'a', function () {
             document.getElementById("ipaddresstitle").innerHTML = this.id;
             var idoftitle = this.id;
             $('#modal-body').load('script.php?ip='+encodeURIComponent(idoftitle));
        });
</script>

PHP

<?php
$ip = filter_input(INPUT_GET, 'ip', FILTER_VALIDATE_IP);
if ($ip){
    echo nl2br( file_get_contents('/etc/ip_files/'.$ip) );
}
Musa
  • 96,336
  • 17
  • 118
  • 137