Si8
Si8

Reputation: 9225

Write to file using PHP while printing

I have the following HTML:

<form method="post" id="print" action="writefile.php" onsubmit="Javascript:window.print(); return true;">
<div id="non-printable">
Enter First & Last Name:
<input id="who" name="who" type="text" /><br><br>
Enter Taken Date:
<input id="tdate" readonly name="todays_date" onfocus="showCalendarControl(this);" type="text" /><br><br>
<input id="submit" type="button" class="btn" value="Submit" />
<div  id="printout" style="text-align:center;display:none;">
    <input type=submit value="Print Certificate" />
</div>
</div>
</form>
    <div id="parent">
    <h1>THIS WILL PRINT WHEN 'PRINT CERTIFICATE' IS PRESSED</h1>
    </div>

The following CSS:

@media print {
    #non-printable { display: none; }
    #parent { display: block; }
}

What I am looking to do is when the PRINT CERTIFICATE is pressed I want to write to a file located in my server and also display the Print window as it is currently doing right now. How can I accomplish that?

I know I can use the following PHP but the incorporation is the issue:

<?php 
session_start();

// Clean up the input values 
foreach($_POST as $key => $value) {  
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
}
$printfor = $_POST['who'];
$dte = $_POST['todays_date'];

$filename = "writefile.txt"; #Must CHMOD to 666, set folder to 777

if($printfor && $dte) {
$text = "\n" . str_pad($_SESSION['printlogin'], 15) . "" . str_pad($printfor, 30) . "" . str_pad($dte, 15);

$fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
if ($fp) {
    fwrite ($fp, $text);
    fclose ($fp);
    #echo ("File written");
    }
    else {
        #echo ("File was not written");
    }
} 
?>

Append to a file:

Image

Upvotes: 1

Views: 536

Answers (2)

Tim Wasson
Tim Wasson

Reputation: 3656

I created this JS Fiddle for you to get you on your way. http://jsfiddle.net/KEHtF/

It uses .ajax to submit the information to your PHP script while also invoking the print command.

Upvotes: 2

xsearingheaven
xsearingheaven

Reputation: 311

Well, if you change your html file, you can incorporate the php into it. You can trigger that php form by making the html button part of a form. When the form submits, the action can be whateveryounamedyourphpfile.php. If you have put hidden input fields on your form (the php you have there is looking for the 'userprint' and 'date' inputs, you can have it write to the file.

The HTML:

<form id="print" method="post" action="whateveryounamedyourphpfile.php" onsubmit="Javascript:window.print(); return true;">
   <input type="hidden" name="date" value="yourvaluehere" />
   <input type="hidden" name="userprint" value="yourvaluehere" />
   <input type="submit" value="Print Certificate" />
</form>

The PHP:

<?php 


$printfor = $_post['userprint'];
$date = $_post['date'];

if($printfor && $date) {
$text = "\n" . str_pad($_SESSION['printlogin'], 10) . "" . str_pad($printfor, 25) . "" . str_pad($dte, 15);

$fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
if ($fp) {
    fwrite ($fp, $text);
    fclose ($fp);
    #$writeSuccess = "Yes";
        #echo ("File written");
    }
    else {
        #$writeSuccess = "No";
        #echo ("File was not written");
    }
} 

//Echo your HTML here
?>

Upvotes: 3

Related Questions