1
<a href = 'view.php?path=...very long path' target='_blank'>PREVIEW</a>

Is it possible the same but using $.post, instead of url?

Something like this:

index.js

var lpath = 'home/lorem ipsum/lorem lorem/target.html';
$.post('../view.php', {path: lpath});
window.open('../view.php', '_blank');

view.php

$cnt = file_get_contents($_POST['path']);
echo $cnt;

result on view.php:

Undefined index path  
file_get_contents(): Filename cannot be empty

I hope you understand the idea:

  • send the path of target.html to view.php using $.post method (or any suitable, but not via address bar

  • on view.php get and echo the content of target.html

Any help?

Barodian
  • 13
  • 4
qadenza
  • 7,864
  • 17
  • 50
  • 92
  • post here is an ajax call, separate to your window open call. what are you trying to achieve? –  Apr 03 '19 at 19:46
  • @tim, this is a blog and the standatd method is - `view.php?id=5`. I want the same but without variables on address bar, - there is no database, just file system, all articles are inside files – qadenza Apr 03 '19 at 19:49
  • what about using a session to hold the variable? –  Apr 03 '19 at 19:50
  • @tim but how to send the variable from `index.js` - there is a click event here - to `vew.php` using session? – qadenza Apr 03 '19 at 19:53
  • 1
    https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – AbraCadaver Apr 03 '19 at 20:30

2 Answers2

0
$("button").click(function(){
  $.post("view.php",
  {
    imgPath: $("img").attr("href");
  },
  function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

Maybe you can try something like this

LucasLaurens
  • 137
  • 1
  • 1
  • 9
0

try

$.post(`../view.php?path=${lpath}`);

in php use

$cnt = file_get_contents($_REQUEST['path']);
echo $cnt;
Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241