views:

50

answers:

5

Hello all,

I am trying to write some information into my database when I activate a javascript function.

I use PHP and MySQL. How can I open the .php file, execute it and return to .js file in order the function to continue its operation?

Thanks in advance.

A: 

You will need AJAX, there http://www.ajaxf1.com/tutorial/ajax-php.html a simple tutorial for AJAX using PHP server

Dominique
A: 

look up AJAX... also think about using jQuery it has a simple and easy to use ajax() function.

Thomas Clayson
+2  A: 

I think you may be a bit confused. Javascript runs in the browser, on the client's computer. Php/MySQL runs on the server, responds to HTTP requests, and creates the content for the browser to display/run.

In order to get the two to communicate dynamically, you need to look at how to send/receive HTTP requests from javascript on the client to your php script on the server. You'll also need to be able to process responses in javascript. This practice is known as AJAX. The simplest way to do this is in my experience to use JSON and jQuery, http://api.jquery.com/jQuery.getJSON/

Doug T.
A: 

If you're not already using an AJAX enabled framework (e.g. jQuery), you could just use a really lightweight XHR implementation to make a HTTP request. This request could have any PHP resource (performing the desired DB updates) as destination.

The smallest code I know of is found here: http://dengodekode.dk/artikler/ajax/xmlhttprequest_wrapper.php (Danish, sorry)

<script type="text/JavaScript">(function(){if(window.XMLHttpRequest)return;var o=null,s,
a=["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0,j=a.length;i<j;s=a[i],i++){try{if(o=new ActiveXObject(s))break}
catch(e){}}window.XMLHttpRequest=o?function(){return new ActiveXObject(s)}:null;o=null})()</script>

And the request:

var oHttp = new XMLHttpRequest();
oHttp.open("post", "http://www.domain.dk/page.php", true);
oHttp.onreadystatechange = function(){ myCallBack(oHttp) };
oHttp.send("id=123&noget=andet");
jensgram
+1  A: 

First of all, it is not possible to call PHP functions directly from JavaScript, or vice versa. This is because PHP is a server-side script, running on the server, and JavaScript is a client-side script, running on the browser.

But there is a solution, however, using a technique called "AJAX" (Asynchronous JavaScript and XML), which can be used to send a request to a server from JavaScript.

For instance, using a "user" page that the user sees, and a "request" page that is called from the JavaScript code, I could write the following code:

userpage.php:

<!-- JavaScript code -->
<script type="text/javascript">
function sendRequestToServer()
{
  // The XMLHttpRequest object is used to make AJAX requests
  var ajax = new XMLHttpRequest();
  // The onreadystatechange function will be called when the request state changes
  ajax.onreadystatechange = function()
  {
    // If ajax.readyState is 4, then the connection was successful
    // If ajax.status (the HTTP return code) is 200, the request was successful
    if(ajax.readyState == 4 && ajax.status == 200)
    {
      // Use ajax.responseText to get the raw response from the server
      alert(ajax.responeText);
    }
  }
  // Open the connection with the open() method
  // (the third parameter is for "asynchronous" requests, meaning that
  //   JavaScript won't pause while the request is processing).
  ajax.open('get', 'requestpage.php', true);
  // Send the request using the send() method
  ajax.send();
}
</script>
<!-- HTML code -->
<button onclick="sendRequestToServer();">Send request!</button>

requestpage.php (the output of this page will be returned to your JavaScript code):

<?php
echo "Hello World!";
?>

This example would, when the button is pressed, send a HTTP request to the server requesting requestpage.php, where the server would execute some server-side code and echo the result. The browser would then take the data it received from the server and use it in the script - in this case, alert() it.

Some resources:

You might also want to check out JSON encoding, which is very common method of sending objects and arrays between clients and servers (especially when using AJAX):

(Sorry for such a long answer, hope it helped though)

Frxstrem