954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Calling PHP function on Javascript events e.g onchange

Hi All,

I just want to share my code.

A lots of website said that php is on server side and javascript is on client side...they said we cannot run php on javascript...

here's how we can run php function when you click call the onchange events on javascript.

function myfunction(){
$x="I love PHP ! by Arman de Guzman de Castro :-)";
return $x;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
function ILovePHP() {
b = "<?=myfunction();?>";
alert(b);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post"><input name="armandecastro" name="armandecastro" type="text" onChange="ILovePHP();"></form><p>
</body>
</html>


try to type something on the inputbox....
hope you can help me to improve this code of mine..

cheers!

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

That's not actually running the PHP function onchange. It's running a Javascript function onchange. The PHP function only runs once when the page is loaded.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

The code you put would be exactly the same as typing

<script language="javascript" type="text/javascript">
function ILovePHP() {
b = "I love PHP ! by Arman de Guzman de Castro :-)";
alert(b);
}
</script>


As ShawnCplus said, PHP is not being run in the onChange event.

To 'run' PHP code in the onChange event, look into ajax to send requests to your PHP script and get the results without the requirement of a page reload.

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

The code you put would be exactly the same as typing

<script language="javascript" type="text/javascript">
function ILovePHP() {
b = "I love PHP ! by Arman de Guzman de Castro :-)";
alert(b);
}
</script>

As ShawnCplus said, PHP is not being run in the onChange event.

To 'run' PHP code in the onChange event, look into ajax to send requests to your PHP script and get the results without the requirement of a page reload.


Let us use some functions and define constants in our code.... <?php
function myfunction(){
define(MYTEXT1,'hello');
define(MYTEXT2,'world');
$a = ucfirst(MYTEXT1);
$b = ucfirst(MYTEXT2);
$c = $a.' '.$b;
return $c;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
function ILovePHP() {
b = "<?=myfunction();?>";
alert(b);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post"><input name="" name="" type="text" onChange="ILovePHP();"></form><p>
</body>
</html>

see the define constants and the function...it works!!! meaning it is not just typing because it execute the php functions...

you can try also...some MYSQL Connection through database if you want....

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

Yes, this will work for static pages, but the PHP is being processed server side BEFORE it is sent to the browser, look at the source code in your browser, no PHP there.

PHP = Server Side
Javascript = Client Side

You can use pre-defined PHP variables in Javascript, but you cannot /do/ anything with PHP after the script has been processed and sent to the client.

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

Yes, this will work for static pages, but the PHP is being processed server side BEFORE it is sent to the browser, look at the source code in your browser, no PHP there.

PHP = Server Side Javascript = Client Side

You can use pre-defined PHP variables in Javascript, but you cannot /do/ anything with PHP after the script has been processed and sent to the client.


Like what you said, "Yes, this will work for static pages...." I am not after of running the whole program in pure javascript...I am just want to prove that the php function is working and can run in javascripts events. Meaning if there are some code on your web that you think it will help you in some return functions...like after clicking the button open your database connection or anything else maybe it can help...You know that javascript can run in php...so using the code vice versa I hope we can create some techniques in the future...

the question is it works or not..

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

Ok, in the other thread you started, you posted this example:

<?php
function myfunction(){
mysql_query('INSERT INTO `table` SET `column`="value"') or die(mysql_error());
return 0;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
function ILovePHP() {
b = "<?=myfunction();?>";
alert(b);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post"><input name="" name="" type="text" onChange="ILovePHP();"></form><p>
</body>
</html>

Now, load a page with that script in, then check your databaseBEFORE you click the textbox or do anything, I can guarantee you that the new entry will be in the database, meaning that it has been executed BEFORE you trigger the onChange event, meaning that the onChange WILL NOT update the database, it has already been done when the script loads.

You have been told the same in 2 threads on Daniweb, and you say that you found the same answer on other sites. This should indicate that Javascript cannot interact with PHP in the way you are claiming it does.

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

And also to test your theory below is some code the will prove that javascript cannot communicate with php:

<?
function myfunction($var){
return $var;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
function ILovePHP(var1) {
b = "<?=myfunction('var1');?>";
alert(b);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post"><input name="armandecastro" name="armandecastro" type="text" onChange="ILovePHP('this is a test string');"></form><p>
</body>
</html>

Now in the above example, the alert box should contain the text 'this is a test string' but instead it just has var1 because it cannot pass variable 1 to php. So try entering the text in the javascript function and getting php to process it and you will get the above results.


Edit:
By saying that, there is nothing stoping you from linking to an invisible iframe that will do php processing 4 you.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

<?php
function myfunction(){
echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
function ILovePHP() {
window.document.write('<?=myfunction();?>');
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post"><input name="" type="text" onChange="ILovePHP();"></form><p>
</body>
</html>


this is how to runECHO COMMAND in php...to run in javascript....
I hope you can find my discovery useful......

thanks a lot guys....what can you say about that....:-)
I told you before you can use it vice versa.....

any comment? accept the TRUTH guys...:-)

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

that doesn't do anything. php is putting that to the browser, not the javascript.

view the source of the page. there will be nothing in the write function of the javascript.

how about you accept the truth. its how php works. you would think if that was possible you wouldn't be the first one to figure it out. you really need to explore php more to understand it. then you wouldn't of even posted this in the first place.

javascript CANNOT run a php function, period. you can have the output of a php function in javascript. i think you have this confused. read a php book, and it will show you.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

that doesn't do anything. php is putting that to the browser, not the javascript.

view the source of the page. there will be nothing in the write function of the javascript.

how about you accept the truth. its how php works. you would think if that was possible you wouldn't be the first one to figure it out.


I am not saying that I am the first one who figure it out....

I hope you understand the topic.....is it very clear....that it works!
that when you trigger the onchange...it will run the php function...

even with or without code on view source....the output came from php function bro. if you want source code...RETURN THE WHOLE PAGE WITH HEADER....using heredoc.

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

wow, you are really misunderstanding here. if you look at the source the output of "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!" will be where the php is. javascript did not write that to the page. it was already there before you TRIED to call it in javascript.

i think you are not understanding how php works. everyone already knows that php can put information into a page. that is what you are doing. you just don't get the fact that javascript did not call the php function directly.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 
I am not saying that I am the first one who figure it out....


No, you have yet to figure it out, you seem to be deliberately missing the point being made hereI hope you understand the topic.....is it very clear....that it works!
that when you trigger the onchange...it will run the php function...
No, the text in the PHP function is being parsed on the server, it has nothing to do with Javascript or onChangeeven with or without code on view source....the output came from php function bro. if you want source code...RETURN THE WHOLE PAGE WITH HEADER....using heredoc.
Again, you have yet to figure it out.

Or is this your idea of a bad joke?

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

wow, you are really misunderstanding here. if you look at the source the output of "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!" will be where the php is. javascript did not write that to the page. it was already there before you TRIED to call it in javascript.

i think you are not understanding how php works. everyone already knows that php can put information into a page. that is what you are doing. you just don't get the fact that javascript did not call the php function directly.

guys! the echo command is inside a function...you can not run the function...without calling it.

function myfunction(){
echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!";
}

If it is present already... when you run the page this will show already the echoed output text without calling this function?

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

are you deliberately wasting our time? you would think if this was possible, someone like me who has been developing with php for years would be agreeing with you. here is an example of what you are saying is possible with php and javascript:

<?php
function display( $text ) {
  echo $text;
}
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="somepage.php" method="post">
<input type="text" name="text" onchange="display(this.value)" />
</form>
</body>
</html>


php would not echo that value in the form. not possible.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

No, you have yet to figure it out, you seem to be deliberately missing the point being made here

No, the text in the PHP function is being parsed on the server, it has nothing to do with Javascript or onChange

Again, you have yet to figure it out.

Or is this your idea of a bad joke?

ok don't type anything on the textbox ........if you can see the result.. are you kidding? :-)

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

Ok, my final response, you are clearly going to continue to be ignorant.

Taken from php.net (The people who manage PHP, they have much more knowledge of PHP than you)
How can I pass a variable from Javascript to PHP?

Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables. Link to PHP.net article

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

Ok, my final response, you are clearly going to continue to be ignorant.

Taken from php.net (The people who manage PHP, they have much more knowledge of PHP than you)

Link to PHP.net article

What is the value of research if only rely on what you read you are bookist man you need to go deeper. Don't you think Einstein rely on the book he made his invention and discovery through experiment.

Why is it javascriptVARIABLES can run in php or vise versa if they cannot share. Do you know what variable means?

if I am an ignorant you are a cyber stupid idiot, an amature playing to be a pro in this field. try me and I will smash your bubble head EGO out of your brain. I am not "Mr. Gentleman" I am for real and I say what I want.

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

are you deliberately wasting our time? you would think if this was possible, someone like me who has been developing with php for years would be agreeing with you. here is an example of what you are saying is possible with php and javascript:

<?php
function display( $text ) {
  echo $text;
}
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="somepage.php" method="post">
<input type="text" name="text" onchange="display(this.value)" />
</form>
</body>
</html>

php would not echo that value in the form. not possible.


try this! to return the ECHO COMMAND ARGUMENTS!. <?php
function myfunction(){
echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
function ILovePHP() {
window.document.write('<?=myfunction();?>');
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post"><input name="" type="text" onChange="ILovePHP();"></form><p>
</body>
</html>

is actually up to you if you want your time to be waisted that is one stupid question! It is not a matter of long years of research nor intelligence it is a matter of tactics man...remember that Goliath was killed by David. I am no David...but you are actually a Goliath...because you are a dumb #######! Why don't you try it and you will see your time is not waisted. I won't say this without any basis I am for real.I did not say that you run your program entirely using my code. There are times that you will need this code even in simple return values.

rm_daniweb
Junior Poster
165 posts since Jan 2007
Reputation Points: 13
Solved Threads: 12
 

i think you are trying to point out the most basic part of php like i said before. you are just putting data into a javascript function. you are not calling a php function from javascript.

you just don't understand exactly what your code does, and you call me a dumbass.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You