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.
[10:34 am]
NudeRaider -- SEN doesn't rely on spammers initiate its sleep cycle. It hat fully automated rest and clean-up phases. Please understand that this is necessary for the smooth operation of the site. Thank you.
[03:45 am]
Sylph-Of-Space -- Does the shoutbox get disabled when there's spammers?
[2024-5-17. : 6:47 am]
NudeRaider -- lil-Inferno
lil-Inferno shouted: nah
strong
[2024-5-17. : 5:41 am]
Ultraviolet -- 🤔 so inf is in you?
[2024-5-17. : 4:57 am]
O)FaRTy1billion[MM] -- my name is mud
[2024-5-17. : 4:35 am]
Ultraviolet -- mud, meet my friend, the stick
[2024-5-16. : 10:07 pm]
lil-Inferno -- nah
[2024-5-16. : 8:36 pm]
Ultraviolet -- Inf, we've got a job for you. ASUS has been very naughty and we need our lil guy to go do their mom's to teach them if they fuck around, they gon' find out
[2024-5-16. : 5:25 pm]
NudeRaider -- there he is, right on time! Go UV! :D
[2024-5-16. : 5:24 pm]
lil-Inferno -- poopoo
Please log in to shout.


Members Online: 4jasminee571yc3, Roy, 6camilac933eM4