Print and submit data in database with one button of the form

I have a form in html/php which is currently submitting data into database successfully. But I want to knew is there anyway that when I click on submit button, it should also print few section of the form as well as submit data in database table.
below is my form code. I also dont want to print complete form, Just sections of the forms.

<div class="container ">
       <div class="panel panel-default">
 <div class="panel-heading" style="background-color: #3fbbc0;"><h4>Patient Bill</h4></div>   
              
             <div class="panel-body">
	<form method="POST" action="invoice.php" target="_blank" onsubmit="return redirectToHomePage();">
            <div class="card-body">          
                            
                            
                        </div>
            
                       <div class="row"> <div class="col-lg-4"> <div class="form-group">
		<label>MR No:</label> <input type="text" name= "mrno"  id= "mrno" value="<?php echo $mrno; ?>" class="form-control" Required>
		</div> </div> 


<div class="col-lg-4"> <div class="form-group">
		<label>Name:</label> <input type="text" name= "name1"  id= "name1"  value="<?php echo $name1; echo ' '; echo $rel2; ?>" class="form-control" Required>
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Age :</label> <input type="text" name= "age1"  id= "age1" value="<?php echo $age1; ?>"  class="form-control">
		</div> </div></div>

<div class="row"> <div class="col-lg-4"> <div class="form-group">
				<label>Gender:</label> <input type="text" name="gender1"  id='gender1'  value="<?php echo $gender1; ?>"  class="form-control">


		</div> </div> 
<div class="col-lg-4"> <div class="form-group">
		<label>Mobile:</label> <input type="text" name= "mobno"  id= "mobno" value="<?php echo $mobno; ?>" class="form-control"  Required>
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Bill No:</label> <input type="text" name= "bilno"  id= "bilno" class="form-control" value="<?php echo $number; ?>" readonly  style="background-color: #3fbbc0;">
		
		</div> </div></div>

<div class="row"> 

<div class="col-lg-4"> <div class="form-group">
		<label>Bill Date:</label> <input type="date" value = "<?php echo date("Y-m-d"); ?>" name= "bildate"  id= "bildate" class="form-control"  readonly  style="background-color: #3fbbc0;">
		</div> </div></div>

  <table class="table table-bordered">
                    <thead class="table-success" style="background-color: #3fbbc0;">
                      <tr>
                        <th width="15%"><center>Type</th>
                        <th width="15%"><center>Service</th>
                        <th width="10%"><center>Machine</th>
			<th width="10%"><center>Consultant</th> 
			<th width="5%"><center>Qty</th>                       
			<th width="10%"><center>Rate</th>
			<th width="10%"><center>Discount</th>			
			<th width="10%"><center>Total</th>
			<th width="15%"><center>Technition</th>
			<th width="20%"><center>Remarks</th>
			<th width="5%"></th>

                                               
                         <button type="button" class="btn btn-sm btn-success" onclick="BtnAdd()">Add Item</button>                         
                        </th>
</tr>
                    </thead>
                    <tbody id="TBody">
                      <tr id="TRow" class="d-none">



   <td><Select  class="country form-control text-end" name="country[]" id = "country"  required>
<option value=""> Select Type</option>
                <?php
                include('db1.php');
                $query = "select * from country";
                // $query = mysqli_query($con, $qr);
                $result = $con->query($query);
                if ($result->num_rows > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                ?>
                <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
                <?php
                    }
                }     ?>    </select> </td>

              

 <td><Select  class="state form-control text-end" name="state[]" id = "state" required>

<option value="">select Service</option></select></td>
 <td><Select  class="city form-control text-end" name="city[]" id = "city" required onchange="GetDetail(this.closest('tr'))" >

<option value="">Select Machine</option></select></td>
            

 
          			<td><Select class="form-control text-end" name="docname[]" id="iii" required onfocus="Calc(this);">
	<option value="">Select Consult</option>
        <?php
        include('db.php');
      $sql = mysqli_query($con,"SELECT * FROM consultant");
        while($row=mysqli_fetch_array($sql))
         {
         echo '<option value="'.$row['consultant_name'].'">'.$row['consultant_name'].'</option>';
            } ?>  </select></td>
      	<td><input type="text" class="qty form-control text-end" name="qty[]" id="ccc" onfocus="Calc(this);"></td>
	<td><input type="text" class="price form-control text-end" name="price1[]" id="ddd"   onfocus="Calc(this);" readonly style="background-color: #3fbbc0;"></td>
	<td><input type="text" class="discunt form-control text-end" name="discunt[]"  id="eee" onchange="Calc(this);"></td>
   	<td><input type="text" class="tot4 form-control text-end" name="tot4[]" id="fff"  readonly style="background-color: #3fbbc0;"></td>	 			
       <td><Select class="form-control text-end" name="tech1[]" id="ggg" Required onfocus="Calc(this);">
       <option value="">Select Technition</option>
         <?php
         include('db.php');
        $sql = mysqli_query($con,"SELECT * FROM tech1");
         while($row=mysqli_fetch_array($sql))
        {
         echo '<option value="'.$row['techname'].'">'.$row['techname'].'</option>';
          } ?>
           </select></td>
           <td><input type="text" class="form-control text-end" name="remarks3[]"  id="zzz" ><input type="hidden" class="zzz1 form-control text-end" name="zzz1[]"  id="zzz1" ><input type="hidden" class="zzz2 form-control text-end" name="zzz2[]"  id="zzz2" ><input type="hidden" class="zzz3 form-control text-end" name="zzz3[]"  id="zzz3" ><input type="hidden" class="zzz4 form-control text-end" name="zzz4[]"  id="zzz4" ><input type="hidden" class="zzz5 form-control text-end" name="zzz5[]"  id="zzz5" ></td>
		<td class="NoPrint"><button type="button" class="btn btn-success"  style="line-height: 1;" onclick="BtnDel(this)">x</button></td>
			
                      </tr>   </tbody> </table>


             
<div class="row"> 
		<div class="col-lg-4"> <div class="form-group">
		<label>Total Bill:</label> <input type="text" name= "totbil"  id= "totbil" class="form-control" readonly style="background-color: #3fbbc0;">
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Advance Received:</label> <input type="text" name= "adv11"  id= "adv11" value = "0" class="form-control"   onchange="GetTotal()" readonly style="background-color: #3fbbc0;">
		</div> </div> 
<div class="col-lg-4"> <div class="form-group">
		<label>Net Payable:</label> <input type="text" name= "netpayable1"  id= "netpayable1" class="form-control" readonly style="background-color: #3fbbc0;">
		</div> </div>

</div>

<div class="row"> 

<div class="col-lg-4"> <div class="form-group">
		<label>Total Amount Received:</label> <input type="text" name= "payrecved"  id= "payrecved" class="form-control"   Required>
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Balance:</label> <input type="text" name= "netpay1"  id= "netpay1" class="form-control" readonly style="background-color: #3fbbc0;">
</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Operator:</label><input type="text" name="abc" id="abc" readonly class="form-control" value = "<?php echo htmlspecialchars($_SESSION["username"]); ?>"  style="background-color: #3fbbc0;"  >
		</div> </div> </div>



<div class="row"><div class="col-lg-4"> <div class="form-group"><label></div></div>
<div class="col-lg-4"> <div class="form-group"><label></div></div><div class="col-lg-4"> <div class="form-group"><center>
<input type="submit" name="submit" id="submit" value="Submit"  class="btn btn-success submit_btn invoice-save-btm">
</form>	

here is invoice.php which handle database

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit']))
{
// getting all values from the HTML form
   
        $mrno = $_POST['mrno'];

	 $name1 = $_POST['name1'];
        $age1 = $_POST['age1'];
        $mobno = $_POST['mobno'];
        $gender1 = $_POST['gender1'];   
      
        $bilno = $_POST['bilno'];
        $bildate = $_POST['bildate'];
       $totbil = $_POST['totbil'];
        $adv11 = $_POST['adv11'];
	$netpayable1 = $_POST['netpayable1'];	
	
	$payrecved = $_POST['payrecved'];
	$netpay1 = $_POST['netpay1'];
	$abc = $_POST['abc'];
  
	$country = $_POST['country'];
	$state = $_POST['state'];
	$city = $_POST['city'];        
    	$docname= $_POST['docname'];
        $qty = $_POST['qty'];
	$price1 = $_POST['price1'];
	$discunt = $_POST['discunt'];        
    	$tot4= $_POST['tot4'];
        $tech1 = $_POST['tech1'];
        $remarks3 = $_POST['remarks3'];
	$zzz1 = $_POST['zzz1'];
        $zzz2 = $_POST['zzz2'];
	$zzz3 = $_POST['zzz3'];
        $zzz4 = $_POST['zzz4'];
	$zzz5=  $_POST['zzz5'];
	       
}


 // database details
    $host = "localhost";
    $username = "thehospi_root";
    $password = "u1m1a1r1";
    $dbname = "thehospi_hmis2";

    // creating a connection
    $con = mysqli_connect($host, $username, $password, $dbname);

    // to ensure that the connection is made
    if (!$con)
    {
        die("Connection failed!" . mysqli_connect_error());
    }  


    // using sql to create a data entry query
$sqlInsert = "INSERT INTO iap3 (mrno, name1, age1, mobno, gender1,   bilno, bildate, totbil, adv11, netpayable1,  payrecved, netpay1, uname1) 
			 VALUES ('$mrno', '$name1', '$age1', '$mobno', '$gender1',  '$bilno', '$bildate',   '$totbil', '$adv11', '$netpayable1',  '$payrecved', '$netpay1', '$abc')";		
		   $rs = mysqli_query($con, $sqlInsert);

   
  
  	 $last_id = mysqli_insert_id($con);
  
   	 for ($i = 0; $i < count($city); $i++) {
			$sqlInsertItem = "
			INSERT INTO iap4 (pid, country, state, city, docname, qty, price1, discunt, tot4, tech1, remarks3, zzz1, zzz2, zzz3, zzz4, zzz5) 
			VALUES (' $last_id',  '$country[$i]', '$state[$i]', '$city[$i]', '$docname[$i]', '$qty[$i]', '$price1[$i]', '$discunt[$i]', '$tot4[$i]', '$tech1[$i]', '$remarks3[$i]', '$zzz1[$i]', '$zzz2[$i]', '$zzz3[$i]', '$zzz4[$i]', '$zzz5[$i]')";			
			$rs1 = mysqli_query($con, $sqlInsertItem);
		}       	

   
    // close connection
    mysqli_close($con);
?>

Print from the server, or print from a printer connected to the browsing device? I’d expect the latter would be a problem from a security standpoint - at least it would need to be some sort of code on the browser that would instigate it.

1 Like

window.print()

It cant directly print, but it can open the print dialog. You’d need to use some standard HTML/CSS methods of hiding things for printing, but it’s doable.

1 Like

do able how ??? I have to define areas in div which i want to include in print ???

Use @media print rules.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.