1

私は次の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>

次の CSS:

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

私がやろうとしているのは、PRINT CERTIFICATE が押されたときに、サーバーにあるファイルに書き込み、現在実行中の [印刷] ウィンドウも表示したいということです。どうすればそれを達成できますか?

次のPHPを使用できることはわかっていますが、組み込みが問題です。

<?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");
    }
} 
?>

ファイルに追加:

画像

4

2 に答える 2

3

html ファイルを変更すれば、そこに php を組み込むことができます。HTMLボタンをフォームの一部にすることで、そのphpフォームをトリガーできます。フォームが送信されるときのアクションは、yourphpfile.php と名付けられたものであれば何でもかまいません。フォームに非表示の入力フィールドを配置した場合 (そこにある php が「userprint」および「date」入力を探している場合、ファイルに書き込むことができます。

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>

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
?>
于 2013-06-17T18:05:50.237 に答える
2

この JS Fiddle を作成して、あなたを道に導きました。http://jsfiddle.net/KEHtF/

.ajax印刷コマンドを呼び出しながら、情報を PHP スクリプトに送信するために使用します。

于 2013-06-17T18:16:32.767 に答える