Javascript execute PHP

A

Angelos

Does anyone knows how to execute a PHP function using javascript..
What I want is to do that in the line bellow:
<a href="" target="_blank" onClick="<? add_product_category(); ?>" >add
Category</a>

either by calling a javascript or doing it inline as above.

Thanks !!
 
E

Evertjan.

Angelos wrote on 04 jul 2005 in comp.lang.javascript:
Does anyone knows how to execute a PHP function using javascript..
What I want is to do that in the line bellow:


either by calling a javascript or doing it inline as above.

you cannot. serverside code is finished once the client comes into action.
 
C

Christopher J. Hahn

Angelos said:
Does anyone knows how to execute a PHP function using javascript..
What I want is to do that in the line bellow:
<a href="" target="_blank" onClick="<? add_product_category(); ?>" >add
Category</a>

either by calling a javascript or doing it inline as above.

Thanks !!

Search the group archives at
http://groups.google.com/groups/comp.lang.javascript

Check out FAQ#4.34 at
http://www.jibbering.com/faq/#FAQ4_34

And Jim's intro linked therefrom, to
http://jibbering.com/2002/4/httprequest.html
 
R

Randy Webb

Danny said:
Well, the client gets what the server serves, the server parses out
any code in the page, when done, it sends the stdout to the client,
usually a page. Now, js can't make a single-function call to php on
server over the wire, it can call a .php with some arguments over the
$_GET[] array for PHP to use them in a function accordingly, but it'll
require a page refresh or a new server transaction for stdout. PHP
however, can use ECHO to output some js code with some values to be
used, on the client machine, That, you can do.


Danny

It is not even that difficult if you understand how to do it. And yes, I
can cause javascript to (indirectly) execute a PHP script.
 
R

Razzbar

Angelos said:
Does anyone knows how to execute a PHP function using javascript..
What I want is to do that in the line bellow:
<a href="" target="_blank" onClick="<? add_product_category(); ?>" >add
Category</a>

You can't do it "inline" like that. PHP scripts run on the server,
JavaScript runs on the browser.

You can link to a page created by a PHP script <a
href="somescript.php">zoop</a>

You can submit a form to a PHP script <form action="somescript.php"...

You can load a PHP page into an iframe.

You can do an xmlHTTPrequest, too.

You have to understand where the code executes!
 
A

Angelos

Thanks aeverybody... the result ... is that I must understand better how and
where the programs are executed !!!
 
R

Razzbar

Angelos said:
Thanks aeverybody... the result ... is that I must understand better how and
where the programs are executed !!!

Exactly. And it's probably the biggest difference between client-server
progamming and stand-alone programming. Not only when, but where.

A very common approach is to create a PHP 'script' that has more than
one 'incarnation'. Two or three 'pages' from the client's point of
view, but one file on the server.

The PHP file is basically a big 'switch' statement, with the case limb
determined by the presense, or value of a $_POST variable. The first
request from the client will not set the variable, so the default case
is executed, which generates a page with a form.

Next, the user fills out the form and submits it. The same PHP file is
called again, this time seeing posted values, it takes a different
branch of the switch statement, and generates a 'thank you' page or
something.

Back to your original question, how to call PHP from Javascript, you do
it by submitting a form.

Good luck,
 
R

Randy Webb

Back to your original question, how to call PHP from Javascript, you do
it by submitting a form.

But that is far from the only way to call a PHP script from Javascript.
 
R

Randy Webb

Stan said:
Don't use the .com version, use .dk or most other non english speaking
country domain suffix google has.

Any particular reason for that asinine suggestion? Executing a PHP
script from javascript is as simple as setting the src of an Image()
object to the PHP script.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top