如何从PHP表单输入创建word文档并将其保存在文件中

如何从PHP表单输入创建word文档并将其保存在文件中,php,Php,我有一个表格页 index.php <form name="proposal_form" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td width="23%" class=

我有一个表格页

index.php

<form name="proposal_form" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
  <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
          <tr> 
            <td width="23%" class="body"> Name</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="text" name="strname" class="textfield"></td> 
        </tr> 


      </table>    
  <input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" />
</form>

<?php


  if(isset($_POST['submit_docs'])){
 $doc_body ='
 <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
          <tr> 
            <td width="23%" class="body"> Name</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%">'.$_POST["strname"].'</td> 
        </tr> 

      </table>    
 ';}

  header("Content-Type: application/vnd.msword");
  header("Expires: 0");//no-cache
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");//no-cache
 header("content-disposition: attachment;filename=sampleword.doc"); 
 //store in local file
file_put_contents( "/files/sample.doc",$doc_body);
 ?>

在PHP中尝试将其另存为Word文档。只要使用你的价值观,而不是我在其中的例子

<?php
$value1 = $_POST['Name'];
$value2 = $_POST['Phone'];
$value3 = $_POST['State1'];
$value4 = $_POST['MemberID'];
$value5 = $_POST['TaxID'];
$value6 = $_POST['UserID'];
$value7 = $_POST['Facility'];
$value8 = $_POST['Email'];
$value9 = $_POST['AdditionalNotes'];


header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=PSSD.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b><i>Name:</i> ".$value1."</b> \t \t \n <br>";
echo "<b><i>Phone#:</i> ".$value2."</b> \t \t \n <br>";
echo "<b><i>State:</i> ".$value3."</b> \t \t \n <br>";
echo "<b><i>Member ID:</i>".$value4."</b> \t \t \n <br>";
echo "<b><i>Tax ID: </i>".$value5."</b> \t \t \n <br>";
echo "<b><i>User ID:</i> ".$value6."</b> \t \t \n <br>";
echo "<b><i>Facility: </i>".$value7."</b> \t \t \n <br>";`enter code here`
echo "<b><i>Email: </i>".$value8."</b> \t \t \n <br>";
echo "<b><i>Additional Notes: </i>".$value9."</b> \t \t \n <br>";

echo "</body>";
echo "</html>";
?>

在PHP中尝试将其另存为Word文档。只要使用你的价值观,而不是我在其中的例子

<?php
$value1 = $_POST['Name'];
$value2 = $_POST['Phone'];
$value3 = $_POST['State1'];
$value4 = $_POST['MemberID'];
$value5 = $_POST['TaxID'];
$value6 = $_POST['UserID'];
$value7 = $_POST['Facility'];
$value8 = $_POST['Email'];
$value9 = $_POST['AdditionalNotes'];


header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=PSSD.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b><i>Name:</i> ".$value1."</b> \t \t \n <br>";
echo "<b><i>Phone#:</i> ".$value2."</b> \t \t \n <br>";
echo "<b><i>State:</i> ".$value3."</b> \t \t \n <br>";
echo "<b><i>Member ID:</i>".$value4."</b> \t \t \n <br>";
echo "<b><i>Tax ID: </i>".$value5."</b> \t \t \n <br>";
echo "<b><i>User ID:</i> ".$value6."</b> \t \t \n <br>";
echo "<b><i>Facility: </i>".$value7."</b> \t \t \n <br>";`enter code here`
echo "<b><i>Email: </i>".$value8."</b> \t \t \n <br>";
echo "<b><i>Additional Notes: </i>".$value9."</b> \t \t \n <br>";

echo "</body>";
echo "</html>";
?>


在哪个文件夹中找不到该文件?在服务器上或您希望在用户计算机上看到?设置标题后,只需输出文件内容。现在,您的顺序错误,表单不需要了(当您尝试创建文档文件时)。@VladimirSerykh在我的服务器上,如果我回显变量
$doc\u body
,那么我将在用户中获取单词filecomputer@VladimirSerykh您能指导我如何创建此文件并保存到我的服务器上,以便我可以将其作为电子邮件附件发送。对不起,我帮不上医生的忙。但如果根本没有文件,则需要检查路径和权限。在哪个文件夹中找不到该文件?在服务器上或您希望在用户计算机上看到?设置标题后,只需输出文件内容。现在,您的顺序错误,表单不需要了(当您尝试创建文档文件时)。@VladimirSerykh在我的服务器上,如果我回显变量
$doc\u body
,那么我将在用户中获取单词filecomputer@VladimirSerykh您能指导我如何创建此文件并保存到我的服务器上,以便我可以将其作为电子邮件附件发送。对不起,我帮不上医生的忙。但如果根本没有文件,则需要检查路径和权限。