Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<html>
<head>
<title>Exercise 2: Use DOM to Test JavaScript</title>

</head>

<body onload="">
<?php

if (isset($_GET[action])){
    // Retrieve the GET parameters and executes the function
      $funcName  = $_GET[action];
      $funcName();
     } else if (isset($_POST[action])){
      // Retrieve the POST parameters and executes the function
      $funcName  = $_POST[action];
    $funcName();

     } else {
      // If there is no action in the URL, then do this
    echo "<INPUT NAME='btnSubmitAdmin' TYPE='button' ONCLICK='javascript:SayHello()' VALUE='Call Javafunction() which redirects to a PHP

function'>";
   }

function phpFunction(){

$filename = "C:\test01.txt"; //the name of our file.
$content = "Report Name"; //what we will be writing to our file.
$strlength = strlen($content); //gets the length of our $content string.
$create = fopen($filename, "w"); //uses fopen to create our file.
$write = fwrite($create, $content, $strlength); //writes our string to our file.
$close = fclose($create); //closes our file
echo "<BR>function phpFunction<BR><BR>";
}
?>


<script language=javascript type="text/javascript">
<!-- Hide script from non-JavaScript browsers

 // SayHello() prints a message in the document's "hello" area
 function SayHello() {

var url="<?php echo $_SERVER[PHP_SELF];?>?action=phpFunction=";

window.open(url, "_self");


//var fso = new ActiveXObject("Scripting.FileSystemObject");
//var s = fso.CreateTextFile("C:\\Test.txt", true);
//s.WriteLine('Report Name');
//s.Close();

 }
-->
</script>
<div id="hello_area">This text is replaced.</div>
<input type="button" value="click me" onclick="SayHello()"/>
</body>
</html>




I am not getting any new page or any text file craeted in the directory
Posted

1 solution

What's wrong? Everything.

From the standpoint of client-side (and hence JavaScript), PHP does not exist. PHP code exists only on server side; on client side it generates pure HTML pages (or resource of any other content type, but none of them can be PHP).

To see what you have on client side, do the following: load your "PHP page" (quotation intended: you only hage URLs and loaded documents, PHP or any other server-side technology is kept on server-side only) and use "View Page Source" in your browser. What you see is what your JavaScript code can see, no more.

Strictly speaking, "call" is a term applicable only for the same process as the function to be called. In this sense, remote procedure call (RPC) is not a call. Same thing with PHP methods.
You can never call PHP method only indirectly, via URL-encoded request, POST method of HTTP request via a HTML form or AJAX. That's all you can do.

—SA
 
Share this answer
 
Comments
amarasat 18-Oct-11 18:20pm    
All i need is, to have a blank page with a button on it, and when user clicks the button, a text file is created with the text "Hello" in it. I thought of doing this using JavaScript and PHP.

In the above example i used the button, to call a function that reloads the same page and calling a php function. This is done as specified at this forum "http://forums.devarticles.com/javascript-development-22/calling-php-functions-with-javascript-3471.html"

But it is not working. So i am trying to create a JavaScript function which loads a new url/php/html file and executes a php code on the "onload" event of the HTML page and its not working either.

So can you please give me an example or modify my code, so that when user clicks the HTML button, a java script function is executed to call a PHP function which creates a file and puts text in it
Sergey Alexandrovich Kryukov 18-Oct-11 20:05pm    
You don't need to modify this code as the idea is wrong. You can do it totally on client side. You can find code sample in my recent solution: http://www.codeproject.com/Answers/269971/How-to-write-text-or-something-to-html-file-using#answer1.

Now, if you want such thing to work even if JavaScript is disabled, you will have to provide server-side-only solution. Then put your button (and other controls effecting data to be generated) in the Web form with method "POST". Server side should process it and generated a page with desired text. This is described in the standard PHP manual (PHP $_POST variable, see http://www.codeproject.com/Answers/269971/How-to-write-text-or-something-to-html-file-using#answer1, http://php.net/manual/en/reserved.variables.post.php).

All right?
--SA
amarasat 19-Oct-11 9:02am    
The example you are showing is modifying the inner html of a div element, you created a place holder and modified the same html file for an onclick event. But my situation is to create a text file in a folder may be in (C:\Documents\Test.txt) when a button is clicked on a html page. This is a JavaScript and PHP interaction. The idea i have is creating a HTMl file with a button on it.
<pre><html><head>
<script language="javascript" type="text/javascript">
function SayHello()
{ }
</script></head><body>
<input type="button" value="click me" önclick="SayHello()"/>
</body> </html></pre>

This file executes the SayHello function when the button is clicked. Now i have a php function to create a file in the c drive documents directory. <pre>
function CreateFile(){
$filename = "C:/Documents/test.txt";
$content = "Report Name";
$strlength = strlen($content);
$create = fopen($filename, "w");
$write = fwrite($create, $content, $strlength);
$close = fclose($create);}
</pre>

Now how do i call this php function when the user clicks the button in the HTML file (SayHello()) function. How do i achieve this?
amarasat 19-Oct-11 10:46am    
Sorry for troubling you, i got the solution, i have a problem with Apache configuration. I was able to achieve what i got.

The problem is i copied a wrong code from the web and try to make it work than working on my own.

Here is my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900