How to call php function with javascript

Hello experts,

I have a javascript environment and I am trying to count the pages of a PDF file on my server here. Can I call a PHP function to count the page, which would then return the javascript variable with the resulted page number?  Basically, using Javascript to call:

function count_pages($pdfname) {
  $pdftext = file_get_contents($pdfname);
  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
  return $num;
}

Thanks in advance!
robthomas09Asked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
You need to se ajax : http://www.w3schools.com/Ajax/Default.Asp

So something like :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function coutnPageWithPHP(fname) {
		if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", "count_pages.php?filename=" + fname, false);
		xmlhttp.send();
		alert( xmlhttp.responseText );
	}
</script>
</head>
<body>
Put here the pdfname :&nbsp;<input type="text" id="pdfname" /><input type="button" onclick="coutnPageWithPHP(document.getElementById('pdffname').value)" value="Get nb of page" />
</body>
</html>

Open in new window


In the previous code we post silently a request to the server to get the number of page.
The script called is : count_pages.php
The parameter sent is : filename
This parameter is the text in the textbox with the id pdfname

So on the server side you have a php script similar to :

<?PHP

  $pdfname = $_GET["filename"]; // the parameter from the ajax call is filename

  // not sure it's so simple to get the number of page of a PDF... perhaps a new question?
  $pdftext = file_get_contents($pdfname);
  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);

  echo $num; // we return the number of page, not it's an echo/print and not a "return"

?>

Open in new window

0
 
jimkarConnect With a Mentor Commented:
The quick answer is no. Javascript runs on the client side and php executes on the server side.

The longer answer is that you do it through XMLHttpRequest, what people nowdays call Ajax.
http://www.w3.org/TR/XMLHttpRequest/
0
 
darren-w-Connect With a Mentor Commented:
this is using jquery:

<html>
<head>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {
$("cl").click(function(){
$(".result").load("yourfun.php?filename=file.pdf");
});
});
</script>
</head>
<body>
<div id="cl">Click here</div>
<div id="result"></div>
</body>
</html>
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
blueghoztConnect With a Mentor Commented:
Put the contents of your function inside a standalone PHP script e.g. count_pages.php that will echo the result (instead of return as per the function)

contents of count_pages.php
<?php
 $pdftext = file_get_contents($_GET['pdfname']);
  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
  echo $num;
?>

 and then use jQuery (for quickest result and least code) to assign the response of that script to a variable via

$.get("count_pages.php", {pdfname:"name.pdf"}, function(data){
   alert("Number of pages: " + data);
  number_of_pages = data;
 });


0
 
darren-w-Commented:
you will need to capture the $_GET["filename"];  in your phpand pass it to the function,

there is a typo in my above, I missed the dot out in the selector:

so:
$(".cl").click(function(){
0
 
leakim971PluritechnicianCommented:
>for quickest result
wrong, $.get or any jQuery ajax call is slowest than coutnPageWithPHP

>and least code
and a heavier page
and therefore consumption of more bandwidth

the only benefit of using jQuery here is the cross-browser compatibility

if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); is light
0
 
darren-w-Commented:
cross-browser compatibility

yes that can come in handy when programming on the web?
0
 
blueghoztCommented:
@leakim your answer arrived whilst I was composing mine - when I said quickest I didn't mean in terms of performance - I meant in terms of coding and testing results - agreed the jQuery library (minified) is about 26KB of extra load - without seeing the rest of the page or application I would hope it might already be there or be useful elsewhere....

Can't fault the response you posted for sheer speed and lightness.
0
 
darren-w-Commented:
leakim,

what you wrote was fine, I just can't stand the hassle in programming with JavaScript, and all the fuss involved in debugging it with the many browser types, and the extensive coding that can sometimes be required. JQuery just makes it simpler... :)

Darren
0
 
leakim971PluritechnicianCommented:
no worry guys, good luck!
0
 
robthomas09Author Commented:
Thanks for all the replies everyone - I'll try this all out and get back.
0
All Courses

From novice to tech pro — start learning today.