0

I have a Print button which I want to do 2 actions when clicked:

  1. Updating my database.
  2. Printing the HTML page.

This is what I've done so far, but it's not working:

   <form action="" method="POST">
<body >
  <?php

    $n=$_POST['ID'];
    $a=implode("</br>",$n);

    list($add, $ward) = explode("(!@!)", $a);
    ?> 
    <div id="container">
        <p id="address">
        <?php echo"$address";?>
        </p>
        <p id="ward">
        <?php echo"$ward";?>
        </p>
    </div>
     <input type="submit" name="Print" value="Print" />
     <?php
     if(isset($_POST['Print']))
     {  
     ?><script>javascript:window.print()</script><?php
        mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
    }?>
 <div id="footer">
</div>
</form>

After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).

Can anyone please help me print and update at same time with this Print button?

Lilly Shk
  • 147
  • 1
  • 21
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Arnold Daniels Feb 18 '15 at 09:05
  • Check any errors in the update statement. make sure it works fine. – Stasel Feb 18 '15 at 09:06
  • The update query doesn't have any errors @Stasel – Lilly Shk Feb 18 '15 at 09:10
  • @LillyShk you have an error in update query remove `;` from end of query – Rakesh Shetty Feb 18 '15 at 09:16
  • I am sorry if u all dint get my question.I have edited my question,please have a look – Lilly Shk Feb 18 '15 at 09:41
  • 1) invalid HTML 2) where is the ``? 3) remove `javascript:` – mplungjan Feb 18 '15 at 09:43

3 Answers3

1

If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql

<form action="" method="POST">
  <input type="submit" name="Print" value="Print" />
</form>

 <?php
 if(isset($_POST['Print'])) {
   mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1"); 
   ?><script>window.print();</script>
 <?php } ?>

UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.

<?php 
  $n=$_POST["ID"]; 
  $a=implode("</br>",$n); 
  list($add,$ward)=explode("(!@!)", $a); 
?>
<body>
    <div id="headerbg">
        <div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
        </div>
        <div id="header-e3"><a align="right" href="logout.php">Logout </a>
        </div>
         <h1><p>Issue Notice</h1>

    </div>
    <div id="container">
        <p id="address">
            <?php echo "$address";?>
        </p>
        <p id="ward">
            <?php echo "$ward";?>
        </p>
    </div>
    <form action="" method="POST">
        <input type="submit" name="Print" value="Print" />
    </form>
    <?php if(isset($_POST[ 'Print'])) { 
       mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1"); 
    ?>
    <script>
        window.print();
    </script>
    <?php } ?>
    <div id="footer"></div>
</body>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.

Make sure your form method is POST

 mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
Asif
  • 350
  • 2
  • 10
0

Make sure you are using input type button inside the form tag. And use post method for form.

Raghav Singh
  • 53
  • 2
  • 11