Running PHP inside a javascript function?

Running PHP inside a javascript function?

Author
Discussion

BliarOut

Original Poster:

72,857 posts

240 months

Tuesday 10th August 2010
quotequote all
I have the following code which executes when a user prints a page (It's set as the background in a CSS Style Sheet in the @print section).


$BGImage = background-image:url(logit.php?RectNo=$RectNo&DBForLogit=db04)";


What that does is insert a record into my MySQL database logging the fact that a record has been printed.

Is there a way to get JS, a client side app to tell PHP, a server side app to launch a php script?

I need logit.php to fire off inside the print()function rather than during an onload fucntion.

Any help or pointers greatly appreciated.

sonic_2k_uk

4,007 posts

208 months

Tuesday 10th August 2010
quotequote all
That's a novel way of doing it using CSS hehe

In JS AJAX (which became a buzzword a few years back) is generally used for server side requests.

Are you using a js framework such as jQuery or Prototype? These provide neat ways to process AJAX requests.

BliarOut

Original Poster:

72,857 posts

240 months

Tuesday 10th August 2010
quotequote all
I can't take all the credit but it works very well biggrin Unfortunately I'm moving over to PDF creation on the fly and the class that I use doesn't currently support the background-image css command weeping

I only use PHP and JS, I don't really have time to go learn another language for one task, I just need to get JS to tell my script to run. I can pass it PHP variables biggrin but I don't know how to say the following

{{{
function printMe() {
print ()
./logit.php?RectNo=$RectNo&DBForLogit=db04
}
}}}

Can't be that difficult, surely biggrin I'm only calling a url effectively??

sonic_2k_uk

4,007 posts

208 months

Tuesday 10th August 2010
quotequote all
BliarOut said:
Can't be that difficult, surely biggrin
Like anything, not if you know how wink

jQuery and Prototype are not new languages per se, they are javascript, although they do have a particular syntax.

Put it this way - they make using javascript easy rather than a PITA, especially when it comes to browser compatibility.

Should do what you want:

<html>
<head>

<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript">

function printMe ()
{

print ();

$.ajax({
url: '/logit.php?RectNo=$RectNo&DBForLogit=db04',
success: function(data) {
alert('Query Complete');
}
});

}

</script>

</head>

<body>
...
</body>
</html>


Edited by sonic_2k_uk on Tuesday 10th August 18:08

-DeaDLocK-

3,367 posts

252 months

Tuesday 10th August 2010
quotequote all
I always thought jQuery alone was worth a fortune (some of the best things really are free!), but jQuery coupled with Ajax is in a whole different league of usefulness!

BliarOut

Original Poster:

72,857 posts

240 months

Tuesday 10th August 2010
quotequote all
Well I couldn't quite get that to work, but it pointed me in the right direction thumbup

I ended up using a xmlhttp request which means I can pass php variables into js and then use them in my new function biggrin

I can see how that's going to be very useful when I polish my app yes

sonic_2k_uk

4,007 posts

208 months

Tuesday 10th August 2010
quotequote all
-DeaDLocK- said:
I always thought jQuery alone was worth a fortune (some of the best things really are free!), but jQuery coupled with Ajax is in a whole different league of usefulness!
Yeah it completely changed the face of the internet IMO.

BliarOut said:
Well I couldn't quite get that to work, but it pointed me in the right direction thumbup

I ended up using a xmlhttp request which means I can pass php variables into js and then use them in my new function biggrin

I can see how that's going to be very useful when I polish my app yes
Glad you got it working!

AJAX uses the xmlhttp object, but have you tried it in multiple browsers if you've coded it manually?

jQuery will use the correct request object based upon your browser. It saves you alot of work and standardises everything.

BliarOut

Original Poster:

72,857 posts

240 months

Tuesday 10th August 2010
quotequote all
Fortunately my script checks the version of the request and uses activex for older versions of IE. It's been a useful exercise as I'll be moving on to storing options for the site in MySQL soon and AJAX could come in handy for that.

It's not such a leap as I thought thumbup