Hello.
There is an index.php page

echo $var1; echo $var2; echo "<a href='script.php'>Ссылка</a>" 

And there is a script script. Php that uses the phpWord library

 $section->addText(htmlspecialchars('Код производителя: '), 'rStyle'); $section->addText(htmlspecialchars('Цена производителя: '), 'rStyle'); 

And automatically forms a word-document and offers to keep it with the client. Ie I click on the link and crashes a window with a proposal to save the file.
I had a question how to transfer $var1 $var2 variables to this script so that it could save their contents into a word document? Something like this function?

 function set_document(){ var link = "{$var1;}"; var value = "{$var2;}"; jQuery.ajax({ url:'script.php' , type:'POST' , data:{name:value, link:link } , success: function(response) { } }); } 
  • one
    $ section-> addText (htmlspecialchars ($ _ POST ["name"]), 'rStyle'); - cerberus
  • "<a href='script.php'> Link </a> onclick = 'set_document ()'" - so unfortunately it does not work - the file is not downloaded. - votanko

2 answers 2

You can send the form for this:

index.php

 <?php $var1 = json_encode(['var' => 'one']); $var2 = json_encode(['var' => 'one']); ?> <form id="target" action="script.php" method="post"> <input type="hidden" name="link" value='<?php echo $var1; ?>'> <input type="hidden" name="value" value='<?php echo $var2; ?>'> </form> <a id="download" href="#">Скачать</a> <script type="text/javascript"> $(function() { $("#download").on("click", function (e) { e.preventDefault(); $('#target').submit(); }); }); </script> 

script.php

 if (!isset($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] !== 'POST') { echo 'wrong request'; exit; } $link = isset($_POST['link']) ? json_decode($_POST['link'], true): ''; $value = isset($_POST['value']) ? json_decode($_POST['value'], true): ''; //Работа с файлом //... 
  • thanks, be sure to try - votanko
  • $ ("# download"). on ("click", function (e) {- swears at this line "Uncaught type error $ (..) not is a function" - I do not really shave in jquery. - votanko
  • I tried without a function, everything worked, thank you very much. - votanko
  • @votanko Please glad to help. The error could occur due to the old version of jQuery <1.7 - postrel

It is possible as you described, or you can write them into the session and in the file script.php you can get it from the session.

Well, for example, $_SESSION['var1'] = $var1; , and in the file script.php $var = $_SESSION['var1']; and everyone can shove anywhere.

  • I tried the session, there are many such pages - it is inconvenient to track which part of the session is responsible for which page. - votanko
  • Is it just a post or get request transfer? for example, get <a href='script.php?var1=LALALA&var2=ALALA'> Link </a> and in script.php $ var1 = GET ['var1']; and so on - Winteriscoming
  • 4 variables, and they are large in volume. As an option, transfer the IDs in the same way, and in the script, drag the data from the database, but this is the last resort. - votanko
  • Why the extreme, and should be done. - Winteriscoming
  • In index.php, these variables have already been obtained from the database, that is, I need to make a request to the database again to get them in script.php - well, this is not the same as Fotchua -