Jump to content

Calling PHP function on Javascript events e.g onchange


rm_phpfreaks

Recommended Posts

Hi,

 

Some website said php is on server side and javascript is on client side we cannot run php function in javascripts events...

 

here's how...hope you can help me to improve my code! cheers!!

 

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="" name="" type="text" onChange="ILovePHP();"></form><p>

</body>

</html>

 

 

PHP = server side

JS = client side.

 

by the time the JS is ready to run a function php would of finished running.. SO you can't..

you could try ajax that allows you to get results from PHP, so thats probably what your looking for!

I find the quickest way is to create an empty iframe, no one will be seeing this, set it to 1pxx1px.

Make a php file with only your function in it.

Then make a javasctipt function:

function fName(var){
  var objIframe = document.getElementById('Your iframe id');
  var url = 'function.php?variable='+var;
  objIfram.src = url;
}

 

Then in your function.php get your variables.

$variable = $_GET['variable'];

 

Then just call your javascript function.

Of course you can send it many variable and just add those into the function declaration and then also add then into your url:

url= 'function.php?var1='+var1+'&var2='+var etc etc

 

This is only for processing things like database inserts and stuff which does not require display.

 

also your html must be:

<input name="" name="" type="text" onChange="fName(this.value);">

If you do want to display stuff then you can expand the iFrame, but then you using iFrames :(

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.