Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to call php function with javascript

Posted on 2010-11-29
11
Medium Priority
?
543 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 200 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 83

Accepted Solution

by:
leakim971 earned 600 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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Assisted Solution

by:blueghozt
blueghozt earned 600 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 83

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 83

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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

876 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