Staredit Network > Forums > Technology & Computers > Topic: Calling PHP from JavaScript
Calling PHP from JavaScript
Dec 28 2009, 4:25 pm
By: Biophysicist  

Dec 28 2009, 4:25 pm Biophysicist Post #1



Quick question: Does anyone know of any way to call a PHP function from a JavaScript function BESIDES AJAX? I tried learning AJAX and it confused me incredibly, which is somewhat weird because JS and PHP were easy for me to learn. Alternatively, if someone could explain AJAX as if they were talking to a retarded toddler, I might be able to understand.



None.

Dec 28 2009, 6:00 pm The Starport Post #2



AJAX:
  1. First, understand the concepts of client and server:
    1. Client is the person's computer asking for websites from servers.
    2. The client's browser (i.e. Firefox) calls a server and asks it for a website.
    3. The client's browser renders the site given to it by the server.
    4. Javascript is part of these sites. When the site is rendered, scripts are run. These are client-side only. The server doesn't give a shit about scripts running on the client.
    5. PHP scripts run on the server to give sites to the clients as they ask for them. These are server-side only. Clients couldn't care less what PHP code is on the server beyond what website they get as the result of it calling for it.
  2. Sites that the client gets from the server run whatever scripts (Javascripts) were built into them.
  3. Scripts can call AJAX functions while they run:
    1. AJAX does something special: It sends messages to the server.
    2. Like you asking someone's server for a website by typing in the URL into the address bar, AJAX functions can similarly "ask" the server for things.
    3. AJAX can "ask" for whole websites or just pieces of data (well, it's all just data, really). Lots of options here.
  4. Client has to wait and twiddle their thumbs until the server gets back to it:
    1. A server's PHP script can get called by the client's AJAX.
    2. The PHP scripts also have lots of options with how to handle client AJAX calls.
    3. Generally, though, the server does what its told and gives the client what it asked for. Like a website or a piece of data or something.
  5. When the server finally gives the client what asked for, an AJAX 'event' activates in the website's script (the browser makes this happen).
  6. This event is where the website's Javascript finally gets to use what it got back from the server to have fun with.


That's a very sloppy gist of it. AJAX is very simple, though. It's literally just a handful of Javascript functions that handle the entire send and receive process (well, technically it's more than that, but who gives a shit). If you're coding a site, you'll want to learn:
  1. How the website's Javascript (on the client) sends AJAX messages to the server.
  2. How the website's PHP script (on the server) listens for AJAX messages from the client.
  3. How to reply to the client's AJAX messages with the server's PHP scripts.
  4. How to listen for replies from the server with the aforementioned AJAX event, and then what do with it.


Post has been edited 3 time(s), last time on Dec 28 2009, 6:20 pm by Tuxedo-Templar.



None.

Dec 28 2009, 9:40 pm Doodle77 Post #3



Here's an example of what you're more likely to use in practice: http://doodle77.dyndns.org/sj/



None.

Dec 28 2009, 9:48 pm Devourer Post #4

Hello

This is, how I call it, the "core-code" of AJAX itself.

Quote
var xmlhttp

function whateveryouwant(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="whatever.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}

http://w3schools.com/ajax/default.asp for details.



Please report errors in the Staredit.Network forum.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:05 am]
Vrael -- I won't stand for people going around saying things like im not a total madman
[01:05 am]
Vrael -- that's better
[12:39 am]
NudeRaider -- can confirm, Vrael is a total madman
[10:18 pm]
Vrael -- who says I'm not a total madman?
[2024-5-03. : 2:26 pm]
UndeadStar -- Vrael, since the ad messages get removed, you look like a total madman for someone that come late
[2024-5-02. : 1:19 pm]
Vrael -- IM GONNA MANUFACTURE SOME SPORTBALL EQUIPMENT WHERE THE SUN DONT SHINE BOY
[2024-5-02. : 1:35 am]
Ultraviolet -- Vrael
Vrael shouted: NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
Gonna put deez sportballs in your mouth
[2024-5-01. : 1:24 pm]
Vrael -- NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
[2024-4-30. : 5:08 pm]
Oh_Man -- https://youtu.be/lGxUOgfmUCQ
[2024-4-30. : 7:43 am]
NudeRaider -- Vrael
Vrael shouted: if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
Yeah I'm not a big fan of Westernhagen either, Fanta vier much better! But they didn't drop the lyrics that fit the situation. Farty: Ich bin wieder hier; nobody: in meinem Revier; Me: war nie wirklich weg
Please log in to shout.


Members Online: O)FaRTy1billion[MM], Roy