-2

I don't see how this would be possilbe using AJAX. The variable I am trying to get doesn't come from an html element, but from predetermined variables. After two variables are set, this code executes which is a function:

// javascript function
function writetofile(file_name, api, wellname){
    <?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);
    ?>   
}
macas
  • 67
  • 1
  • 9
  • Typo => `$file_hanlder` => `$file_handler`, unless it's just that; a typo. – Funk Forty Niner Jan 08 '15 at 01:35
  • yes it was, my bad a typo.. fixed it, and no i need to write to a file, and you can't do that using client side javascript. You need to use server side php, that is why i need to transfer the variable to php – macas Jan 08 '15 at 01:36
  • PHP is executed on the server first before the file is sent to the browser, then javascript is executed locally. So no, you can't just chuck php tags into a javascript file and expect it to work. – Christian Jan 08 '15 at 01:36
  • What does it matter where the value comes from? Use ajax and check the jQuery manual. – jeroen Jan 08 '15 at 01:36
  • I don't get it; you're inside JS `// javascript function`? You're doing a function with ``. I'm confused. What you have there is PHP syntax unless there's something I'm not grasping. – Funk Forty Niner Jan 08 '15 at 01:37
  • No undo the possible duplicate, this has the same problem as passing a js var to php, but this situation is completely different. The values aren't found in an html element! So i don't have anything to put in the data: {} Those possible duplicate problems don't help me whatsoever – macas Jan 08 '15 at 01:41
  • Searching here on SO for _"pass javascript variable php"_ turned up over 6,000 results about passing variables back and forth between javascript and php. It is one of the most frequently asked questions I've noticed here. Any searching effort would have found something to help. – Stephen P Jan 08 '15 at 01:43
  • I didn't vote as duplicate, I voted as unclear what you're asking, as per my above comment. Showing us what you're really using would take the guesswork out of things. – Funk Forty Niner Jan 08 '15 at 01:43
  • @StephenP the answers to the problem at hand cant be found with a simple google search. All the solutions require the variable to be passed via
    or ajax.. my javascript variables do not come from html elements... at all. They are calculated and stored, to later be used to write to a file in php.
    – macas Jan 08 '15 at 01:46

1 Answers1

0

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.

Stephen P
  • 14,422
  • 2
  • 43
  • 67