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:59 am]
RIVE -- Lots of fun little nods to different works of others in the cheats too.
[05:17 pm]
Oh_Man -- so i was today years old wen i learned rebel yell starcraft mission is named after a billy idol song. makes me wonder wat other missions are named after songs
[2026-6-04. : 3:42 am]
RIVE -- Hellooooo, Nurse
[2026-6-03. : 4:19 pm]
NudeRaider -- Hello World!
[2026-6-01. : 4:03 pm]
IskatuMesk -- no Hello World? No Hello . jpeg? Not even ddosing some government website? smh my head
[2026-5-31. : 10:02 pm]
Symmetry -- I was really just figuring out how to compile
[2026-5-31. : 10:02 pm]
Symmetry -- I didn't make it do anything
[2026-5-31. : 9:17 pm]
Ultraviolet -- hell yeah. did you have trouble making the button functional, or just haven't gotten there yet?
[2026-5-31. : 8:29 pm]
Symmetry -- I made the marine shoot lasers and gave the medic a nonfunctional button
[2026-5-31. : 8:07 pm]
Ultraviolet -- Symmetry
Symmetry shouted: I did my first EUD thing today. Feels like me finding arsenal at age 8 again
haha for sure. in some ways it's even more exciting because you can play EUD maps on regular battle.net, don't need to convince someone to download your mod to play with you. what did you do?
Please log in to shout.


Members Online: NudeRaider