Jump to content

[SOLVED] Getting PHP to execute inside Javascript.


BliarOut

Recommended Posts

I'm building an app in PHP and I need to audit whenever anyone prints. IE 6 & 7 support the onbeforeprint() and onafterprint() function so I can trap whenever anyone prints even if it's not from a button that I've attached code to. The code below fires exactly when I want but I'd like to replace the alert with a MySQL insert that puts an entry in my log table.

 

<script language="javascript" type="text/javascript">
function window.onbeforeprint()
{
alert('This fires before printing!');

}
</script>

 

Should the following work in theory (assuming all my variables are already declared etc)?

 

<script language="javascript" type="text/javascript">
function window.onbeforeprint()
{
<!--Replace this 
alert('This fires before printing!'); -->

<!--With This -->
<?php mysql_query("INSERT INTO log (UserID, EventType, IPAddress) VALUES ('$_SESSION[FullName]', 'Permit no " ."$NewPermitID". " Printed', '$_SESSION[iP]')") or die(mysql_error()); ?>

}
</script>

Link to comment
Share on other sites

I don't think you can do php inside of javascript. There might be a solution using AJAX, but I only know a little Ajax so I can't help you there. The php code is processed by the server before the browser ever gets the document containing the HTML and javascript.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.