generate and download xml file after input submit (php and mysql) - JPK












1














I want to do a page with form: two inputs (start and end dates) and two types of report to generate. One type of report is generated using one file, the other type of report using the other. To generate one of these reports, you must complete both dates. After entering both dates, click the button who generates the XML file and automatically downloads it to the disk as XML file.



XML is generated by using PHP, which extracts data from the MySQL database.



Unfortunately, I have a problem with creating this form, which after entering two dates will generate and save me an XML file in which there will be those rows from the database that are included in the given period.



I would also like to be able to insert three dates into this XML file: the date from the first input, the date from the second input, and the creation date of the XML file. The place for these dates is stored in PHP as: filecreateddate, startdate and enddate.



My PHP file:



<?php

/** create XML file */
$mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");

/* check connection */
if ($mysqli->connect_errno) {

echo "Connect failed ".$mysqli->connect_error;

exit();
}

$query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy FROM FA";

$booksArray = array();

if ($result = $mysqli->query($query)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {

array_push($booksArray, $row);
}

if(count($booksArray)){

createXMLfile($booksArray);

}

/* free result set */
$result->free();
}

/* close connection */
$mysqli->close();

function createXMLfile($booksArray){

$filePath = 'jpk.xml';

$dom = new DOMDocument('1.0', 'utf-8');

$root = $dom->createElement('JPK');

$naglowek = $dom->createElement('Naglowek');
$root->appendChild($naglowek);

$naglowek->appendChild( $dom->createElement('KodFormularza', 'JPK_FA') );
$naglowek->appendChild( $dom->createElement('WariantFormularza', '1') );
$naglowek->appendChild( $dom->createElement('CelZlozenia', '1') );
$naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', filecreateddate) );
$naglowek->appendChild( $dom->createElement('DataOd', startdate) );
$naglowek->appendChild( $dom->createElement('DataDo', enddate) );

for($i=0; $i<count($booksArray); $i++){


$bookid = $booksArray[$i]['id'];

$bookNazwaKontrahenta = $booksArray[$i]['firma'];
$bookAdresKontrahenta = $booksArray[$i]['ulica'];
$bookDowodSprzedazy = $booksArray[$i]['nr'];
$bookDataWystawienia = $booksArray[$i]['data_wystawienia'];
$bookDataSprzedazy = $booksArray[$i]['data_sprzedazy'];


$book = $dom->createElement('FA');

$book->setAttribute('id', $bookid);
$book->appendChild($NazwaKontrahenta = $dom->createElement('firma', $bookNazwaKontrahenta));
$book->appendChild($AdresKontrahenta = $dom->createElement('ulica', $bookAdresKontrahenta));
$book->appendChild($DowodSprzedazy = $dom->createElement('nr', $bookDowodSprzedazy));
$book->appendChild($DataWystawienia = $dom->createElement('data_wystawienia', $bookDataWystawienia));
$book->appendChild($DataSprzedazy = $dom->createElement('data_sprzedazy', $bookDataSprzedazy));

$root->appendChild($book);

}

$dom->appendChild($root);

$dom->save($filePath);

}
?>


My form with button must look like this:
click










share|improve this question







New contributor




Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1














    I want to do a page with form: two inputs (start and end dates) and two types of report to generate. One type of report is generated using one file, the other type of report using the other. To generate one of these reports, you must complete both dates. After entering both dates, click the button who generates the XML file and automatically downloads it to the disk as XML file.



    XML is generated by using PHP, which extracts data from the MySQL database.



    Unfortunately, I have a problem with creating this form, which after entering two dates will generate and save me an XML file in which there will be those rows from the database that are included in the given period.



    I would also like to be able to insert three dates into this XML file: the date from the first input, the date from the second input, and the creation date of the XML file. The place for these dates is stored in PHP as: filecreateddate, startdate and enddate.



    My PHP file:



    <?php

    /** create XML file */
    $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");

    /* check connection */
    if ($mysqli->connect_errno) {

    echo "Connect failed ".$mysqli->connect_error;

    exit();
    }

    $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy FROM FA";

    $booksArray = array();

    if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {

    array_push($booksArray, $row);
    }

    if(count($booksArray)){

    createXMLfile($booksArray);

    }

    /* free result set */
    $result->free();
    }

    /* close connection */
    $mysqli->close();

    function createXMLfile($booksArray){

    $filePath = 'jpk.xml';

    $dom = new DOMDocument('1.0', 'utf-8');

    $root = $dom->createElement('JPK');

    $naglowek = $dom->createElement('Naglowek');
    $root->appendChild($naglowek);

    $naglowek->appendChild( $dom->createElement('KodFormularza', 'JPK_FA') );
    $naglowek->appendChild( $dom->createElement('WariantFormularza', '1') );
    $naglowek->appendChild( $dom->createElement('CelZlozenia', '1') );
    $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', filecreateddate) );
    $naglowek->appendChild( $dom->createElement('DataOd', startdate) );
    $naglowek->appendChild( $dom->createElement('DataDo', enddate) );

    for($i=0; $i<count($booksArray); $i++){


    $bookid = $booksArray[$i]['id'];

    $bookNazwaKontrahenta = $booksArray[$i]['firma'];
    $bookAdresKontrahenta = $booksArray[$i]['ulica'];
    $bookDowodSprzedazy = $booksArray[$i]['nr'];
    $bookDataWystawienia = $booksArray[$i]['data_wystawienia'];
    $bookDataSprzedazy = $booksArray[$i]['data_sprzedazy'];


    $book = $dom->createElement('FA');

    $book->setAttribute('id', $bookid);
    $book->appendChild($NazwaKontrahenta = $dom->createElement('firma', $bookNazwaKontrahenta));
    $book->appendChild($AdresKontrahenta = $dom->createElement('ulica', $bookAdresKontrahenta));
    $book->appendChild($DowodSprzedazy = $dom->createElement('nr', $bookDowodSprzedazy));
    $book->appendChild($DataWystawienia = $dom->createElement('data_wystawienia', $bookDataWystawienia));
    $book->appendChild($DataSprzedazy = $dom->createElement('data_sprzedazy', $bookDataSprzedazy));

    $root->appendChild($book);

    }

    $dom->appendChild($root);

    $dom->save($filePath);

    }
    ?>


    My form with button must look like this:
    click










    share|improve this question







    New contributor




    Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1







      I want to do a page with form: two inputs (start and end dates) and two types of report to generate. One type of report is generated using one file, the other type of report using the other. To generate one of these reports, you must complete both dates. After entering both dates, click the button who generates the XML file and automatically downloads it to the disk as XML file.



      XML is generated by using PHP, which extracts data from the MySQL database.



      Unfortunately, I have a problem with creating this form, which after entering two dates will generate and save me an XML file in which there will be those rows from the database that are included in the given period.



      I would also like to be able to insert three dates into this XML file: the date from the first input, the date from the second input, and the creation date of the XML file. The place for these dates is stored in PHP as: filecreateddate, startdate and enddate.



      My PHP file:



      <?php

      /** create XML file */
      $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");

      /* check connection */
      if ($mysqli->connect_errno) {

      echo "Connect failed ".$mysqli->connect_error;

      exit();
      }

      $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy FROM FA";

      $booksArray = array();

      if ($result = $mysqli->query($query)) {

      /* fetch associative array */
      while ($row = $result->fetch_assoc()) {

      array_push($booksArray, $row);
      }

      if(count($booksArray)){

      createXMLfile($booksArray);

      }

      /* free result set */
      $result->free();
      }

      /* close connection */
      $mysqli->close();

      function createXMLfile($booksArray){

      $filePath = 'jpk.xml';

      $dom = new DOMDocument('1.0', 'utf-8');

      $root = $dom->createElement('JPK');

      $naglowek = $dom->createElement('Naglowek');
      $root->appendChild($naglowek);

      $naglowek->appendChild( $dom->createElement('KodFormularza', 'JPK_FA') );
      $naglowek->appendChild( $dom->createElement('WariantFormularza', '1') );
      $naglowek->appendChild( $dom->createElement('CelZlozenia', '1') );
      $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', filecreateddate) );
      $naglowek->appendChild( $dom->createElement('DataOd', startdate) );
      $naglowek->appendChild( $dom->createElement('DataDo', enddate) );

      for($i=0; $i<count($booksArray); $i++){


      $bookid = $booksArray[$i]['id'];

      $bookNazwaKontrahenta = $booksArray[$i]['firma'];
      $bookAdresKontrahenta = $booksArray[$i]['ulica'];
      $bookDowodSprzedazy = $booksArray[$i]['nr'];
      $bookDataWystawienia = $booksArray[$i]['data_wystawienia'];
      $bookDataSprzedazy = $booksArray[$i]['data_sprzedazy'];


      $book = $dom->createElement('FA');

      $book->setAttribute('id', $bookid);
      $book->appendChild($NazwaKontrahenta = $dom->createElement('firma', $bookNazwaKontrahenta));
      $book->appendChild($AdresKontrahenta = $dom->createElement('ulica', $bookAdresKontrahenta));
      $book->appendChild($DowodSprzedazy = $dom->createElement('nr', $bookDowodSprzedazy));
      $book->appendChild($DataWystawienia = $dom->createElement('data_wystawienia', $bookDataWystawienia));
      $book->appendChild($DataSprzedazy = $dom->createElement('data_sprzedazy', $bookDataSprzedazy));

      $root->appendChild($book);

      }

      $dom->appendChild($root);

      $dom->save($filePath);

      }
      ?>


      My form with button must look like this:
      click










      share|improve this question







      New contributor




      Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I want to do a page with form: two inputs (start and end dates) and two types of report to generate. One type of report is generated using one file, the other type of report using the other. To generate one of these reports, you must complete both dates. After entering both dates, click the button who generates the XML file and automatically downloads it to the disk as XML file.



      XML is generated by using PHP, which extracts data from the MySQL database.



      Unfortunately, I have a problem with creating this form, which after entering two dates will generate and save me an XML file in which there will be those rows from the database that are included in the given period.



      I would also like to be able to insert three dates into this XML file: the date from the first input, the date from the second input, and the creation date of the XML file. The place for these dates is stored in PHP as: filecreateddate, startdate and enddate.



      My PHP file:



      <?php

      /** create XML file */
      $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");

      /* check connection */
      if ($mysqli->connect_errno) {

      echo "Connect failed ".$mysqli->connect_error;

      exit();
      }

      $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy FROM FA";

      $booksArray = array();

      if ($result = $mysqli->query($query)) {

      /* fetch associative array */
      while ($row = $result->fetch_assoc()) {

      array_push($booksArray, $row);
      }

      if(count($booksArray)){

      createXMLfile($booksArray);

      }

      /* free result set */
      $result->free();
      }

      /* close connection */
      $mysqli->close();

      function createXMLfile($booksArray){

      $filePath = 'jpk.xml';

      $dom = new DOMDocument('1.0', 'utf-8');

      $root = $dom->createElement('JPK');

      $naglowek = $dom->createElement('Naglowek');
      $root->appendChild($naglowek);

      $naglowek->appendChild( $dom->createElement('KodFormularza', 'JPK_FA') );
      $naglowek->appendChild( $dom->createElement('WariantFormularza', '1') );
      $naglowek->appendChild( $dom->createElement('CelZlozenia', '1') );
      $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', filecreateddate) );
      $naglowek->appendChild( $dom->createElement('DataOd', startdate) );
      $naglowek->appendChild( $dom->createElement('DataDo', enddate) );

      for($i=0; $i<count($booksArray); $i++){


      $bookid = $booksArray[$i]['id'];

      $bookNazwaKontrahenta = $booksArray[$i]['firma'];
      $bookAdresKontrahenta = $booksArray[$i]['ulica'];
      $bookDowodSprzedazy = $booksArray[$i]['nr'];
      $bookDataWystawienia = $booksArray[$i]['data_wystawienia'];
      $bookDataSprzedazy = $booksArray[$i]['data_sprzedazy'];


      $book = $dom->createElement('FA');

      $book->setAttribute('id', $bookid);
      $book->appendChild($NazwaKontrahenta = $dom->createElement('firma', $bookNazwaKontrahenta));
      $book->appendChild($AdresKontrahenta = $dom->createElement('ulica', $bookAdresKontrahenta));
      $book->appendChild($DowodSprzedazy = $dom->createElement('nr', $bookDowodSprzedazy));
      $book->appendChild($DataWystawienia = $dom->createElement('data_wystawienia', $bookDataWystawienia));
      $book->appendChild($DataSprzedazy = $dom->createElement('data_sprzedazy', $bookDataSprzedazy));

      $root->appendChild($book);

      }

      $dom->appendChild($root);

      $dom->save($filePath);

      }
      ?>


      My form with button must look like this:
      click







      php mysql xml






      share|improve this question







      New contributor




      Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Dec 27 '18 at 15:58









      Kacper

      61




      61




      New contributor




      Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Kacper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Simply retrieve your date fields from $_POST or $_GET arrays then pass them as parameters in SQL and as node values in XML. The type of web form array depends on the HTML form method used which you do not post. Assuming the below form, consider following PHP:



          HTML



          <form action="/action_page.php" method="post">
          Start Date: <input type="text" name="start_date"><br>
          End Date: <input type="text" name="end_date"><br>
          <input type="submit" value="Submit">
          </form>


          PHP



          if (!empty($_POST["start_date"]) && !empty($_POST["end_date"]) ) {

          $start_date = $_POST["start_date"];
          $end_date = $_POST["end_date"];
          $file_created_date = date("Y-m-d");

          $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");
          $booksArray = array();

          // PREPARED STATEMENT
          $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy
          FROM FA
          WHERE mydate BETWEEN ? AND ?";

          // BIND PARAMS
          $stmt = $mysqli->prepare($query);
          $stmt->bind_param('ss', $start_date, $end_date);

          // RUN QUERY
          if ($stmt->execute()) {
          $result = $stmt->get_result();
          while ($row = $result->fetch_assoc()) {
          array_push($booksArray, $row);
          }

          if(count($booksArray)){
          createXMLfile($booksArray);
          }

          $result->free();
          }

          $mysqli->close();
          }


          Be sure to update function by prefixing variables with $:



          ...
          $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', $file_created_date) );
          $naglowek->appendChild( $dom->createElement('DataOd', $start_date) );
          $naglowek->appendChild( $dom->createElement('DataDo', $end_date) );
          ...


          Be sure to update SQL above for actual date field (i.e., replace mydate):



          SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy 
          FROM FA
          WHERE mydate BETWEEN ? AND ?;





          share|improve this answer





















          • Thanks! Form works fine with dates from inputs (only I used "date" instead "text"), but $file_created_date, $start_date and $end_date in appendChild not works. Have you any idea? Maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase?
            – Kacper
            Dec 28 '18 at 9:31










          • Please be specific as it does not work is not helpful. What happens instead? Any error in log files? Empty nodes in XML?
            – Parfait
            Dec 28 '18 at 13:15










          • OK, right. Fortunately I repaired this. I forgot that I had to add this variables in function. It was empty nodes in XML. But maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase? For example - at the moment my $filePath is generated as "jpk.xml". I want to have something like "jpk_startdate_enddate.xml", assuming that "startdate" is $start_date and "enddate" is $end_date. It is possible? Assume that the date format is "dMY", but it's not necessarily. My dream is that this XML file should not be generated on the server, but only on my computer.
            – Kacper
            Dec 28 '18 at 16:13










          • After you build XML, send headers for user to download: Let user download a XML file. For server download use file_put_contents('file.xml', $dom->saveXML()) where you change file.xml to string that concatenates needed variables.
            – Parfait
            Dec 28 '18 at 19:09













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Kacper is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947688%2fgenerate-and-download-xml-file-after-input-submit-php-and-mysql-jpk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Simply retrieve your date fields from $_POST or $_GET arrays then pass them as parameters in SQL and as node values in XML. The type of web form array depends on the HTML form method used which you do not post. Assuming the below form, consider following PHP:



          HTML



          <form action="/action_page.php" method="post">
          Start Date: <input type="text" name="start_date"><br>
          End Date: <input type="text" name="end_date"><br>
          <input type="submit" value="Submit">
          </form>


          PHP



          if (!empty($_POST["start_date"]) && !empty($_POST["end_date"]) ) {

          $start_date = $_POST["start_date"];
          $end_date = $_POST["end_date"];
          $file_created_date = date("Y-m-d");

          $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");
          $booksArray = array();

          // PREPARED STATEMENT
          $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy
          FROM FA
          WHERE mydate BETWEEN ? AND ?";

          // BIND PARAMS
          $stmt = $mysqli->prepare($query);
          $stmt->bind_param('ss', $start_date, $end_date);

          // RUN QUERY
          if ($stmt->execute()) {
          $result = $stmt->get_result();
          while ($row = $result->fetch_assoc()) {
          array_push($booksArray, $row);
          }

          if(count($booksArray)){
          createXMLfile($booksArray);
          }

          $result->free();
          }

          $mysqli->close();
          }


          Be sure to update function by prefixing variables with $:



          ...
          $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', $file_created_date) );
          $naglowek->appendChild( $dom->createElement('DataOd', $start_date) );
          $naglowek->appendChild( $dom->createElement('DataDo', $end_date) );
          ...


          Be sure to update SQL above for actual date field (i.e., replace mydate):



          SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy 
          FROM FA
          WHERE mydate BETWEEN ? AND ?;





          share|improve this answer





















          • Thanks! Form works fine with dates from inputs (only I used "date" instead "text"), but $file_created_date, $start_date and $end_date in appendChild not works. Have you any idea? Maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase?
            – Kacper
            Dec 28 '18 at 9:31










          • Please be specific as it does not work is not helpful. What happens instead? Any error in log files? Empty nodes in XML?
            – Parfait
            Dec 28 '18 at 13:15










          • OK, right. Fortunately I repaired this. I forgot that I had to add this variables in function. It was empty nodes in XML. But maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase? For example - at the moment my $filePath is generated as "jpk.xml". I want to have something like "jpk_startdate_enddate.xml", assuming that "startdate" is $start_date and "enddate" is $end_date. It is possible? Assume that the date format is "dMY", but it's not necessarily. My dream is that this XML file should not be generated on the server, but only on my computer.
            – Kacper
            Dec 28 '18 at 16:13










          • After you build XML, send headers for user to download: Let user download a XML file. For server download use file_put_contents('file.xml', $dom->saveXML()) where you change file.xml to string that concatenates needed variables.
            – Parfait
            Dec 28 '18 at 19:09


















          0














          Simply retrieve your date fields from $_POST or $_GET arrays then pass them as parameters in SQL and as node values in XML. The type of web form array depends on the HTML form method used which you do not post. Assuming the below form, consider following PHP:



          HTML



          <form action="/action_page.php" method="post">
          Start Date: <input type="text" name="start_date"><br>
          End Date: <input type="text" name="end_date"><br>
          <input type="submit" value="Submit">
          </form>


          PHP



          if (!empty($_POST["start_date"]) && !empty($_POST["end_date"]) ) {

          $start_date = $_POST["start_date"];
          $end_date = $_POST["end_date"];
          $file_created_date = date("Y-m-d");

          $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");
          $booksArray = array();

          // PREPARED STATEMENT
          $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy
          FROM FA
          WHERE mydate BETWEEN ? AND ?";

          // BIND PARAMS
          $stmt = $mysqli->prepare($query);
          $stmt->bind_param('ss', $start_date, $end_date);

          // RUN QUERY
          if ($stmt->execute()) {
          $result = $stmt->get_result();
          while ($row = $result->fetch_assoc()) {
          array_push($booksArray, $row);
          }

          if(count($booksArray)){
          createXMLfile($booksArray);
          }

          $result->free();
          }

          $mysqli->close();
          }


          Be sure to update function by prefixing variables with $:



          ...
          $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', $file_created_date) );
          $naglowek->appendChild( $dom->createElement('DataOd', $start_date) );
          $naglowek->appendChild( $dom->createElement('DataDo', $end_date) );
          ...


          Be sure to update SQL above for actual date field (i.e., replace mydate):



          SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy 
          FROM FA
          WHERE mydate BETWEEN ? AND ?;





          share|improve this answer





















          • Thanks! Form works fine with dates from inputs (only I used "date" instead "text"), but $file_created_date, $start_date and $end_date in appendChild not works. Have you any idea? Maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase?
            – Kacper
            Dec 28 '18 at 9:31










          • Please be specific as it does not work is not helpful. What happens instead? Any error in log files? Empty nodes in XML?
            – Parfait
            Dec 28 '18 at 13:15










          • OK, right. Fortunately I repaired this. I forgot that I had to add this variables in function. It was empty nodes in XML. But maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase? For example - at the moment my $filePath is generated as "jpk.xml". I want to have something like "jpk_startdate_enddate.xml", assuming that "startdate" is $start_date and "enddate" is $end_date. It is possible? Assume that the date format is "dMY", but it's not necessarily. My dream is that this XML file should not be generated on the server, but only on my computer.
            – Kacper
            Dec 28 '18 at 16:13










          • After you build XML, send headers for user to download: Let user download a XML file. For server download use file_put_contents('file.xml', $dom->saveXML()) where you change file.xml to string that concatenates needed variables.
            – Parfait
            Dec 28 '18 at 19:09
















          0












          0








          0






          Simply retrieve your date fields from $_POST or $_GET arrays then pass them as parameters in SQL and as node values in XML. The type of web form array depends on the HTML form method used which you do not post. Assuming the below form, consider following PHP:



          HTML



          <form action="/action_page.php" method="post">
          Start Date: <input type="text" name="start_date"><br>
          End Date: <input type="text" name="end_date"><br>
          <input type="submit" value="Submit">
          </form>


          PHP



          if (!empty($_POST["start_date"]) && !empty($_POST["end_date"]) ) {

          $start_date = $_POST["start_date"];
          $end_date = $_POST["end_date"];
          $file_created_date = date("Y-m-d");

          $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");
          $booksArray = array();

          // PREPARED STATEMENT
          $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy
          FROM FA
          WHERE mydate BETWEEN ? AND ?";

          // BIND PARAMS
          $stmt = $mysqli->prepare($query);
          $stmt->bind_param('ss', $start_date, $end_date);

          // RUN QUERY
          if ($stmt->execute()) {
          $result = $stmt->get_result();
          while ($row = $result->fetch_assoc()) {
          array_push($booksArray, $row);
          }

          if(count($booksArray)){
          createXMLfile($booksArray);
          }

          $result->free();
          }

          $mysqli->close();
          }


          Be sure to update function by prefixing variables with $:



          ...
          $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', $file_created_date) );
          $naglowek->appendChild( $dom->createElement('DataOd', $start_date) );
          $naglowek->appendChild( $dom->createElement('DataDo', $end_date) );
          ...


          Be sure to update SQL above for actual date field (i.e., replace mydate):



          SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy 
          FROM FA
          WHERE mydate BETWEEN ? AND ?;





          share|improve this answer












          Simply retrieve your date fields from $_POST or $_GET arrays then pass them as parameters in SQL and as node values in XML. The type of web form array depends on the HTML form method used which you do not post. Assuming the below form, consider following PHP:



          HTML



          <form action="/action_page.php" method="post">
          Start Date: <input type="text" name="start_date"><br>
          End Date: <input type="text" name="end_date"><br>
          <input type="submit" value="Submit">
          </form>


          PHP



          if (!empty($_POST["start_date"]) && !empty($_POST["end_date"]) ) {

          $start_date = $_POST["start_date"];
          $end_date = $_POST["end_date"];
          $file_created_date = date("Y-m-d");

          $mysqli = new mysqli("localhost", "XXX", "XXX", "XXX");
          $booksArray = array();

          // PREPARED STATEMENT
          $query = "SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy
          FROM FA
          WHERE mydate BETWEEN ? AND ?";

          // BIND PARAMS
          $stmt = $mysqli->prepare($query);
          $stmt->bind_param('ss', $start_date, $end_date);

          // RUN QUERY
          if ($stmt->execute()) {
          $result = $stmt->get_result();
          while ($row = $result->fetch_assoc()) {
          array_push($booksArray, $row);
          }

          if(count($booksArray)){
          createXMLfile($booksArray);
          }

          $result->free();
          }

          $mysqli->close();
          }


          Be sure to update function by prefixing variables with $:



          ...
          $naglowek->appendChild( $dom->createElement('DataWytworzeniaJPK', $file_created_date) );
          $naglowek->appendChild( $dom->createElement('DataOd', $start_date) );
          $naglowek->appendChild( $dom->createElement('DataDo', $end_date) );
          ...


          Be sure to update SQL above for actual date field (i.e., replace mydate):



          SELECT id, firma, ulica, nr, data_wystawienia, data_sprzedazy 
          FROM FA
          WHERE mydate BETWEEN ? AND ?;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 27 '18 at 17:07









          Parfait

          49.6k84269




          49.6k84269












          • Thanks! Form works fine with dates from inputs (only I used "date" instead "text"), but $file_created_date, $start_date and $end_date in appendChild not works. Have you any idea? Maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase?
            – Kacper
            Dec 28 '18 at 9:31










          • Please be specific as it does not work is not helpful. What happens instead? Any error in log files? Empty nodes in XML?
            – Parfait
            Dec 28 '18 at 13:15










          • OK, right. Fortunately I repaired this. I forgot that I had to add this variables in function. It was empty nodes in XML. But maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase? For example - at the moment my $filePath is generated as "jpk.xml". I want to have something like "jpk_startdate_enddate.xml", assuming that "startdate" is $start_date and "enddate" is $end_date. It is possible? Assume that the date format is "dMY", but it's not necessarily. My dream is that this XML file should not be generated on the server, but only on my computer.
            – Kacper
            Dec 28 '18 at 16:13










          • After you build XML, send headers for user to download: Let user download a XML file. For server download use file_put_contents('file.xml', $dom->saveXML()) where you change file.xml to string that concatenates needed variables.
            – Parfait
            Dec 28 '18 at 19:09




















          • Thanks! Form works fine with dates from inputs (only I used "date" instead "text"), but $file_created_date, $start_date and $end_date in appendChild not works. Have you any idea? Maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase?
            – Kacper
            Dec 28 '18 at 9:31










          • Please be specific as it does not work is not helpful. What happens instead? Any error in log files? Empty nodes in XML?
            – Parfait
            Dec 28 '18 at 13:15










          • OK, right. Fortunately I repaired this. I forgot that I had to add this variables in function. It was empty nodes in XML. But maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase? For example - at the moment my $filePath is generated as "jpk.xml". I want to have something like "jpk_startdate_enddate.xml", assuming that "startdate" is $start_date and "enddate" is $end_date. It is possible? Assume that the date format is "dMY", but it's not necessarily. My dream is that this XML file should not be generated on the server, but only on my computer.
            – Kacper
            Dec 28 '18 at 16:13










          • After you build XML, send headers for user to download: Let user download a XML file. For server download use file_put_contents('file.xml', $dom->saveXML()) where you change file.xml to string that concatenates needed variables.
            – Parfait
            Dec 28 '18 at 19:09


















          Thanks! Form works fine with dates from inputs (only I used "date" instead "text"), but $file_created_date, $start_date and $end_date in appendChild not works. Have you any idea? Maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase?
          – Kacper
          Dec 28 '18 at 9:31




          Thanks! Form works fine with dates from inputs (only I used "date" instead "text"), but $file_created_date, $start_date and $end_date in appendChild not works. Have you any idea? Maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase?
          – Kacper
          Dec 28 '18 at 9:31












          Please be specific as it does not work is not helpful. What happens instead? Any error in log files? Empty nodes in XML?
          – Parfait
          Dec 28 '18 at 13:15




          Please be specific as it does not work is not helpful. What happens instead? Any error in log files? Empty nodes in XML?
          – Parfait
          Dec 28 '18 at 13:15












          OK, right. Fortunately I repaired this. I forgot that I had to add this variables in function. It was empty nodes in XML. But maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase? For example - at the moment my $filePath is generated as "jpk.xml". I want to have something like "jpk_startdate_enddate.xml", assuming that "startdate" is $start_date and "enddate" is $end_date. It is possible? Assume that the date format is "dMY", but it's not necessarily. My dream is that this XML file should not be generated on the server, but only on my computer.
          – Kacper
          Dec 28 '18 at 16:13




          OK, right. Fortunately I repaired this. I forgot that I had to add this variables in function. It was empty nodes in XML. But maybe you know how I can set $filePath to add $start_date and $end_date after a fixed phrase? For example - at the moment my $filePath is generated as "jpk.xml". I want to have something like "jpk_startdate_enddate.xml", assuming that "startdate" is $start_date and "enddate" is $end_date. It is possible? Assume that the date format is "dMY", but it's not necessarily. My dream is that this XML file should not be generated on the server, but only on my computer.
          – Kacper
          Dec 28 '18 at 16:13












          After you build XML, send headers for user to download: Let user download a XML file. For server download use file_put_contents('file.xml', $dom->saveXML()) where you change file.xml to string that concatenates needed variables.
          – Parfait
          Dec 28 '18 at 19:09






          After you build XML, send headers for user to download: Let user download a XML file. For server download use file_put_contents('file.xml', $dom->saveXML()) where you change file.xml to string that concatenates needed variables.
          – Parfait
          Dec 28 '18 at 19:09












          Kacper is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Kacper is a new contributor. Be nice, and check out our Code of Conduct.













          Kacper is a new contributor. Be nice, and check out our Code of Conduct.












          Kacper is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947688%2fgenerate-and-download-xml-file-after-input-submit-php-and-mysql-jpk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Angular Downloading a file using contenturl with Basic Authentication

          Can't read property showImagePicker of undefined in react native iOS