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!

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.

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.

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....

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.

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..

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 database BEFORE 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.

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.

commented: Better explaination than mine, hopefully it'll be understood :) +1
<?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 run ECHO 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...:-)

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.

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.

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.

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 here

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...

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

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.

Again, you have yet to figure it out.

Or is this your idea of a bad joke?

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?

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.

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? :-)

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

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 javascript VARIABLES 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.

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 asshole! 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.

commented: Watch your language, read some more books on php & javascript. You aren't getting the point what kkeith29, cwarn23 and all the others are trying to make. -2
commented: I really don't appreciate being called names when all I tried to do was help. -1

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.

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.

REMEMBER! i am not the one who started to use nasty words!
But if I am triggered I will play hard ball.

First you told me that you are wasting your time the other guy told me that I am ignorant. In the real sense if you are really brainy you will not even throw a glance at my proposition or discovery. But since you answered back that means I have moved you and I can considered my self deep.

It is very clear that the passing of data arguments came from php function and I have shown here that you need to enter some text in the textbox to trigger that data passing by php function to javascript.

Do you understand the title of this thread? It seems that you don't understand what I mean. I can say that I am wasting my time on you. You can only do two things with this proposition you can take it or leave it.

Where done goodbye once again dumbass!

this is a forum to help people. i tried to explain that what you were saying was false. i was just trying to help. you wouldn't open your eyes to see what we were talking about, obviously you were too focused on proving us wrong.

the way you worded your thread made it sound like the javascript was calling a php function. you should of put javascript is calling the output of a php function run only once. which is something most people should already know in order to start using php.

REMEMBER! i am not the one who started to use nasty words!
But if I am triggered I will play hard ball.

First you told me that you are wasting your time the other guy told me that I am ignorant. In the real sense if you are really brainy you will not even throw a glance at my proposition or discovery. But since you answered back that means I have moved you and I can considered my self deep.

The only reason I refrained myself from replying to this thread was because of the amount of stupidity this thread 'possessed'. But, I guess its the right time to knock some sense into you.
rm_daniweb, You need to learn to respect others opinions and answers when it comes from talented and experienced people. You haven't discovered anything supernatural to be proud of. Your discovery is nothing but a 'Hello world' program for most of us here. You don't even have a clue what others are talking about. Nobody hurled abuses at you. You started it with the most common terms which,in fact, could be used to describe you .
You asked everyone if they tested your code. Did you, atleast for once, checked what others are trying to tell you ? I guess not.
This is a public forum and the sole existence of this forum is to help people, which definitely doesn't seem to be on your agenda. Watch your words sonny boy! You might face the wrath of the moderators. Consider this as a warning. Anyone can be arrogant. But we are just being nice to you. :)
Good luck with Javascript, Php, myql or whatever you are dealing with.

<?php
function myfunction(){
echo "<h1><strong>THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!</strong><h1>";
}
?>
<!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.writeln('<?=myfunction();?>');
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
After typing some text! press TAB;<br>
<form action="" method="post"><input name="" type="text" onChange="ILovePHP();"></form><p>
</body>
</html>

Don't tell that you cannot run php function in javascript...because it is server side etc... etc... do you beleive that when programs meets each other they become one?

thank you!! take it or leave it even it is very simple or hello world I beleive that as we go along with the research...there are possibilities to do it. NEVER GIVE UP GUYS!.

Example: You can refresh a new page with php and javascript command on it and I beleived that on that new page...PHP WILL RUN THAT COMMAND.

thank you guys! for accepting the stupidity this thread possessed?! bye....another dumbass!

you are arrogant!!! you are not friendly!! in dealing stupidity of the thread if thereis. I think you are the one stupid!!

Just a note to finnish off. Try viewing in the browser view->source of that code in your previous post and you will probably see what everybody has been talking about.

you are arrogant!!! you are not friendly!! in dealing stupidity of the thread if thereis. I think you are the one stupid!!

Ah! I see. You edited your post just to describe yourself more better! Nice. Every post of yours is helping us to find out more about you (nothing good though! Sigh!).

thank you guys! for accepting the stupidity this thread possessed?!
bye....
another dumbass!

Thank you for accepting who you are.
Bye,
Naveen. :D

Just a note to finnish off. Try viewing in the browser view->source of that code in your previous post and you will probably see what everybody has been talking about.

I don't think that is of any use. He is not going to listen/see. He only wants to hear what he wants to hear. :)

You don't need to be so insulting Nav33n. This is a person relatively new to daniweb.com. I don't know about you but I value our new members 100%. So if you dislike a person that much just chat to another about something different. There is no good reason for insulting new members that could make a difference.

Just a note to finnish off. Try viewing in the browser view->source of that code in your previous post and you will probably see what everybody has been talking about.

I am aware of that.

It runs the php function first right?
is myfunction() is not a php function?
is echo is not php function?
'<?php ?>'

how about if the JAVASCRIPT function is refreshing a new PHP page or loading a NEW PHP page and that page IS FULL OF PHP COMMANDS. What will happen....like location.etc...header() in php....

Just let us explore the possibilitiies...

Cwarn23, If you read all the posts, you will see I have no where insulted him. I am just giving a dose of his own medicine, in a sarcastic way.
As I already said in my previous post, he had no respect whatsoever for the other posters. I don't expect that from a new member (and I am sure most of the people here agree with me).
I also think there is a limit for everything. He (rm_daniweb) has surely crossed it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.