The PHP code in the .php
file that contains the javascript function is run on the server and is never sent to the client. The javascript code; the function itself, is run on the client (web browser) where there is no PHP.
Since, on the server, there are no file_name, api, and wellname parameters the PHP is sure to fail.
// javascript function
function writetofile(file_name, api, wellname) {
// The stuff here in the php block gets run on the server
// before anything is ever sent to the web browser.
// This opens a file (on the server), writes something to it,
// and closes the file. It produces NO output in the page.
// The PHP itself is never sent to the browser.
<?php
//something along the lines of this:
$file_handler = fopen(file_name, "r");
$api = api;
$wellname = wellname;
$result = $api." : ".$wellname;
fwrite($file_handler, $result);
$fclose($file_handler);
?>
}
This is what gets sent to the browser:
// javascript function
function writetofile(file_name, api, wellname) {
}
Clearly, if you call that function from within the browser, nothing happens, because there is no function body.
If you want to use the file_name, api, and wellname that were (somehow) specified on the client browser to run some PHP on the server, you must send those variables to the server, probably with an AJAX POST
request to a url like example.com/php_process/dostuff.php
, where "dostuff.php" would read the POST variables (like with any form) and do something with them. It should then respond with the results, or at least a status indicator.
How to do an AJAX POST from Javascript is another question, which has lots of answers on SO already.