-1

How do I print information when the user send the information
when user open the page, it displays a form just like any other form, when the user finishes the information gets saved in the DB, but at the same time I would like to print that information on paper...

<form method="post" action="save.php">
   <input name="name">
<!-- like 30 more inputs, i know is crazy -->
   <button type="submit">Send</button>
</form>
<?php
if(isset($_POST['_token']) && $c->validToken()) {
   // Get all inputs, re-validate, clean...etc
   // Save info into the DB
   // Done!

   // Here is where I would like to collect all the infomarion
   // and print it, send it to a printert...

   header('Location: thankyou.php');
   exit;
}
?>

I can use PHP, o jquery...
am I dreaming or this is possible?

Update

Yes there is a nice printer connected to the "server"(What kind of question is that?)...
Nop, I do not use ajax for this....
Nop, I have never use PHP to print, therefore this question.... I have use Java but the user always sees the printer dialog window, in this case the user don't need to see that.
There was 2 answers that pointed me to the right direction thank you, I have no idea who down-voted this question, but clearly must be a genius to find this question irrelevant or too dumb....

Tanker
  • 1,178
  • 3
  • 16
  • 49
  • 1
    is a printer connected to the server? –  Aug 21 '15 at 04:10
  • 1
    Internet Printing Protocol php-class: http://stackoverflow.com/a/13045206/588079, or `php_printer.dll`: http://stackoverflow.com/a/7841918/588079 & http://stackoverflow.com/questions/7916480/where-to-find-php-printer-dll, or `print`/`lpr`: http://stackoverflow.com/a/3723320/588079 – GitaarLAB Aug 21 '15 at 05:33
  • @GitaarLAB, thank you, i'm using the first link, it seems to be working, I wonder if I can use an array of printers... – Tanker Aug 21 '15 at 06:22

3 Answers3

1

It's possible but depend on how do you print the info (font, paper type, file format...). Hope this tut will help you more.

Quan Nguyen
  • 562
  • 1
  • 5
  • 20
0

I write forms like this all the time where I will have a form reference itself, as follows:

<form action="?" method="post"></form>

the "?" in the action parameter means "call the same page". When you submit a form all inputs and form elements like <select> and <textarea> that have a "name" attribute will be submitted with the "name" attribute being the $_POST key in the array and the "value" attribute being the value. Elements like text inputs don't have a written value attribute like radio buttons and checkboxes do ahead of time, but the browser knows to submit the value the user enters as the "value" attribute. SO . . . this includes elements like your submit button <input type="submit" name="submit" value="Submit">. So when the form posts to itself, you have a conditional at the top of the page which says:

if(isset($_POST['submit'])){ // the form has been submitted
    // get all $_POST values from the form and do great stuff with them
    // (send to an API like PayPal or similar)
    // when you have done all that you need to you can evaluate the response
    // from the API, do other stuff, etc. then if you want to, 
    // print to the page.
    // I will pretend that I created another array from some of the $_POST values, 
    // since I don't want ALL $_POST values like check boxes or the submit button to appear on the page
    // We'll call it $response (and it is an array of values)
    echo "<p>Your form was successfully submitted. The following values have 
    been added to our database:</p>";
    echo "<ul class='responseList'>";

    foreach($response as $k=>$v){
        echo "<li>". $k ."=". $v ."</li>";
    }

    echo "</ul>";

} else { // the form hasn't been submitted yet
    // show the form (this can even be put into a function to be called as needed)
}

I hope this makes sense. You should be able to create the smaller array from the $_POST array so that you can do things like rename the array key something more attractive on the page. For example you may use "firstName" as the name parameter in an input field, but you don't want to echo firstName, but clean it up to "First Name".

Joel
  • 87
  • 8
  • Apologies, after re-reading I see that you truly meant "print" as in . . . on paper (just like you said). You can create code that runs bash commands within your PHP, but you will need correct permissions on the files and directories. You would use the `shell_exec()` function as shown here http://stackoverflow.com/questions/29724718/placing-php-variable-inside-of-shell-exec-to-run-lsp-command – Joel Aug 21 '15 at 05:14
  • Don't worry, and thank for the link thouh, I have never use shell_exec(); before but I see the point. – Tanker Aug 21 '15 at 06:18
  • @tanker If you can up-vote me it helps with ratings. Thanks! – Joel Dec 10 '16 at 00:30
-1

If you are sending the data through AJAX, then retrieve the acknowledge through AJAX, when proper acknowledge retrieved, then call the print method like this

$(document).ready(function () { window.print(); });
Harshit
  • 5,147
  • 9
  • 46
  • 93
  • Not sure why this was downvoted, but possibly because of the code part. The "direction" is correct, but the code would always print on document ready, which is a bit different than handling in the AJAX success event. – Source Matters Aug 21 '15 at 04:34