Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印时使用PHP写入文件_Php_Html_Css - Fatal编程技术网

打印时使用PHP写入文件

打印时使用PHP写入文件,php,html,css,Php,Html,Css,我有以下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>

我有以下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>
我要做的是,当按下打印证书时,我想写入服务器中的文件,并显示当前正在执行的打印窗口。我怎样才能做到这一点

我知道我可以使用以下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");
    }
} 
?>

如果您更改html文件,可以将php合并到其中。您可以通过使html按钮成为表单的一部分来触发该php表单。表单提交时,操作可以是whateveryounamedyourphpfile.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
?>

如果您更改html文件,您可以将php合并到其中。您可以通过将html按钮作为表单的一部分来触发该php表单。表单提交时,操作可以是whateveryounamedyourphpfile.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
?>

我为你创建了这个JS小提琴,让你走上正轨


它使用
.ajax
将信息提交到PHP脚本,同时调用print命令。

我为您创建了这个JS提琴,让您上路



它使用
.ajax
将信息提交到PHP脚本,同时调用print命令。

只需
onclick=“window.print();”
就可以了。无需长时间编码:)谢谢@kevin,我可以修复它,但是写入文件又如何呢?您需要使用onclick调用不同的JS函数。该函数通过AJAX调用您的日志PHP文件,成功后返回print命令。不管怎样,我都希望它打印:)是否缺少
type=“submit”第6行
标记的属性?只要
onclick=“window.print();”
就可以了。不需要长时间的编码:)谢谢@kevin,我可以修复它,但是写入文件怎么样?你需要用onclick调用不同的JS函数。一个通过AJAX调用您的日志PHP文件并在成功后返回print命令的程序。我希望它打印出来:)您是否缺少
type=“submit”第6行中的
标记的属性?是否有任何方式可以使其不重新加载页面,而是保持在当前页面上?是否有任何方式可以使其不重新加载页面,而是保持在当前页面上?好的,收到了。我决定使用Stephanie Rosario的帖子,不过做了一些小改动,效果很好。我希望避免的一件事是,一旦调用并完成print方法,我就不希望重新加载页面。有什么想法吗?(我认为我需要使用ajax,这样它就不会取代页面并在后台写入?)如果你想在不重新加载页面的情况下提交数据,你可以考虑使用ajax,这就是我的解决方案。我用我想做的事更新了我的问题。。。我如何将您拥有的转换为我必须完成的而不加载的内容?还是我应该提出一个新问题?谢谢你已经有答案了。。。我把它放在这里的一个jsfiddle:。当您单击“提交”时,将运行一段jQuery,将数据发送到您的PHP文件,PHP文件将写入该文件。。。它还调用print()命令。为了处理表单提交,您必须稍微调整jQuery,但这几乎正是您所需要的。。。我想我知道你想说什么。谢谢。好的,我知道了。我决定使用Stephanie Rosario的帖子,不过做了一些小改动,效果很好。我希望避免的一件事是,一旦调用并完成print方法,我就不希望重新加载页面。有什么想法吗?(我认为我需要使用ajax,这样它就不会取代页面并在后台写入?)如果你想在不重新加载页面的情况下提交数据,你可以考虑使用ajax,这就是我的解决方案。我用我想做的事更新了我的问题。。。我如何将您拥有的转换为我必须完成的而不加载的内容?还是我应该提出一个新问题?谢谢你已经有答案了。。。我把它放在这里的一个jsfiddle:。当您单击“提交”时,将运行一段jQuery,将数据发送到您的PHP文件,PHP文件将写入该文件。。。它还调用print()命令。为了处理表单提交,您必须稍微调整jQuery,但这几乎正是您所需要的。。。我想我知道你想说什么。谢谢