Solved

How to call php function with javascript

Posted on 2010-11-29
11
515 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
  • 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

772 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