1

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

7
  • 1
    Just onclick="window.print();" will do the trick. No need for the long coding :) Commented Jun 17, 2013 at 17:55
  • Thanks @kevin I can fix it but what about writing to the file as well? Commented Jun 17, 2013 at 17:56
  • 1
    You need to call a different JS function with the onclick. One that calls your logging PHP file via AJAX and, on success, returns the print command. Commented Jun 17, 2013 at 17:57
  • Well I would want it to print regardless :) Commented Jun 17, 2013 at 17:58
  • are you missing the type="submit" attribute for the <input> tag in line 6? Commented Jun 17, 2013 at 18:02

2 Answers 2

3

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
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Is there anyway I can have so it doesn't reload the page and instead stays on the current page?
2

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.

7 Comments

Ok got it. I decided to go with Stephanie Rosario's post and it's working fine with some minor tweaks. One thing I am looking to avoid is, once the print method is invoked and completed, I do not want the page to reload. Any idea? (I think I need to use ajax so it doesn't replace the page and writes in the background?)
If you want to submit data without a page reload, you're looking at using AJAX, which is what my solution will do.
I updated my question with what I am looking to do... How would I convert what you have with what I have to accomplish without loading? Or should I open a new question? Thanks
You've got your answer already... I put it up on a jsfiddle here: jsfiddle.net/KEHtF. When you click "submit", a chunk of jQuery runs that sends data to your PHP file, which writes to the file... it also invokes the print() command. You'll have to tweak the jQuery a bit to work with your form submission, but this is almost exactly what you need.
Ok... I think I know what you are trying to say. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.