Solved

How to call php function with javascript

Posted on 2010-11-29
11
530 Views
Last Modified: 2012-05-10
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!
0
Comment
Question by:robthomas09
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Assisted Solution

by:jimkar
jimkar earned 50 total points
ID: 34234040
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 150 total points
ID: 34234213
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
 
LVL 13

Assisted Solution

by:darren-w-
darren-w- earned 150 total points
ID: 34234274
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 6

Assisted Solution

by:blueghozt
blueghozt earned 150 total points
ID: 34234320
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
 
LVL 13

Expert Comment

by:darren-w-
ID: 34234336
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
 
LVL 82

Expert Comment

by:leakim971
ID: 34234388
>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
 
LVL 13

Expert Comment

by:darren-w-
ID: 34234471
cross-browser compatibility

yes that can come in handy when programming on the web?
0
 
LVL 6

Expert Comment

by:blueghozt
ID: 34234538
@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
 
LVL 13

Expert Comment

by:darren-w-
ID: 34234581
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
 
LVL 82

Expert Comment

by:leakim971
ID: 34234590
no worry guys, good luck!
0
 

Author Comment

by:robthomas09
ID: 34234667
Thanks for all the replies everyone - I'll try this all out and get back.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question