Solved

How to call php function with javascript

Posted on 2010-11-29
11
493 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 6

Assisted Solution

by:blueghozt
blueghozt earned 150 total points
Comment Utility
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-
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>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-
Comment Utility
cross-browser compatibility

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

Expert Comment

by:blueghozt
Comment Utility
@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-
Comment Utility
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
Comment Utility
no worry guys, good luck!
0
 

Author Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now