-3

i have a javascript code for printing process.. what i want is, when i click on print button, it will proceed to print page, and at the same time, it will update my table in mysql. i'm doing like this.. but it only perform printing process.. not both.. any suggestion from you all?

help.. :(

here is the javascript code:

    <script type="text/javascript">

        function printpage()
        {
        document.getElementById('buttonpurchse').style.visibility='hidden'; 
        window.location.reload();  
        window.print();

        $result = mysql_query("UPDATE maklumatimport SET tarikhExpiredEksport = '$date2' WHERE noRujukan = '$noRujukan'");
        $row = mysql_fetch_array($result); 


        }
   </script>

and this is button from form.

   <button id="buttonpurchse" type="button" title="Print Purchse Request" onClick="printpage()"  style="visibility: visible"><img src="img/print.png" height="60" width="100" ></button>
naFa
  • 1
  • 1
  • 1
  • 1
    You're mixing and matching PHP and JavaScript, one is server-side and the other is client-side. – noko Aug 24 '14 at 01:40
  • do you have any possible solution to do both process without combine it inside javascript function? – naFa Aug 24 '14 at 01:45

1 Answers1

0

you cant have a mysql query inside javascript function, you can use ajax to do this, example

function some(a, b) {
    $.ajax({
        type: "POST",
        url: "someFile.php",
        data: {
            "a" : a,
            "b" : b
        },
        success: function (r) {
            if (r == 1) {
                window.location = "home.php";
            } else {
                alert("something");
            }
        }
    });
}

in your "someFile.php" file you can perform your query

iconer
  • 287
  • 1
  • 4
  • 13
  • since my function name "printpage".. thus.. at success : function (r), i should replace "r" with "printpage"? – naFa Aug 24 '14 at 02:02
  • No, in your printPage function, after window.print(); you need to call the 'some' function, that funcion call a php file, in that file you can perform the query to the database, example: function printpage() { document.getElementById('buttonpurchse').style.visibility='hidden'; window.location.reload(); window.print(); some(someData, otherData); } I recommend to you read about AJAX to understand what is going on – iconer Aug 24 '14 at 02:43