如何在PHP中一键保存数据和打印表单?

时间:2017-07-03 15:31:47

标签: php pdf printing

我正在处理发票,我必须在数据库中保存表单的数据,同时应该打印。所以,我有3个页面:invoice.php,invoice_print.php和insert_data.php

invoice_print.php是一个应该打印的html表单。

现在,用户将转到invoice.php,然后他将填写详细信息,他将点击提交按钮将数据保存在数据库中,否则他将点击打印按钮打印该发票。

现在,让我们来到第二部分:如果用户将选择打印按钮,那么数据将首先进入数据库,然后它将转到invoice_print.php,其中包含相同的数据。

怎么做?我应该使用什么逻辑来保存数据并在按钮中捕获数据的id,然后将该id发送到另一个页面上以显示?

invoice.php:

<form id="demo-form2"  action="insert_data.php" method="post" data-parsley-validate class="form-horizontal form-label-left">
<div class="col-md-12 form-group">
  <div class="col-md-6">
    <label class="control-label">Location</label>
  </div>
  <div class="col-md-6">                                  
    <input class="form-control" type="text" value="" name="designation">  
  </div>                         
</div>        
<div class="col-md-12 form-group">
  <div class="col-md-6">
    <label class="control-label">Address</label>
  </div>
  <div class="col-md-6">                                  
    <input class="form-control" type="text" value="" name="contact">  
  </div>                         
</div>
</form>

invoice_print.php:

<div class="col-md-12">
  <div class="col-md-6">
    <h2>DIGILIFE BIZCARE SOLUTIONS</h2>
    <p>414, Vashi Infotech Park,Maharashtra</p>
  </div>
  <div class="col-md-6">
    <h2>BILL OF SUPPLY</h2>
  </div>
</div>
<div class="col-md-12">
  <div class="col-md-6">
    <div class="col-md-12">
      <div class="col-md-6">
        <label>GSTIN</label>
        <input type="text" name="gstin" value="">
        <label>Serial No & Date of Invoice</label>
        <input type="text" name="serialNo">
      </div>
      <div class="col-md-6">

      </div>        
    </div>  
  </div>
  <div class="col-md-6">
    <div class="col-md-12">
      <div class="col-md-6">
        <label>Mode of Transport</label><br>
        <label>Vehicle No</label><br>
        <label>Date & Time of Supply</label><br>
        <label>Place Of Supply</label>
      </div>
      <div class="col-md-6s">
        <input type="text" name="">
        <input type="text" name="">
        <input type="text" name="">
        <input type="text" name="">
      </div>
    </div>
  </div>
</div>
<div class="col-md-12 form-group">
  <div class="col-md-6">
    <label class="control-label">Location</label>
  </div>
  <div class="col-md-6">                                  
    <input class="form-control" type="text" value="" name="designation">  
  </div>                         
</div>        
<div class="col-md-12 form-group">
  <div class="col-md-6">
    <label class="control-label">Address</label>
  </div>
  <div class="col-md-6">                                  
    <input class="form-control" type="text" value="" name="contact">  
  </div>                         
</div>

1 个答案:

答案 0 :(得分:0)

我会将表单的所有代码放入一个文件中,结构如下:

<?php
$formDone=!empty($_POST['formDone']);
$printReq=!empty($_POST['printReq']);
if ($formDone)
{
    // perform validation
}
if ($formDone && $validationPassed)
{
    // write to database using $_POST data
}
if ($formDone && $databaseWriteSuccess && $printReq)
{
    // print using $_POST data
}
// end of PHP
?>
<form method="post" action="<?php echo $PHP_SELF ?>">
<fieldset>
    <input type="hidden" name="formDone" value="1" />
</fieldset>
    <!-- field forms -->
<input type="submit" value="Save to db" />
<input type="submit" name="printReq" value="Print Invoice" />
</form>