Page 1 of 1

Calling php function inside javascript function

Posted: Fri Oct 08, 2010 10:35 am
by Smudly
How can I call a PHP Function inside a Javascript Function? This is what I have so far, but I don't think I'm doing it the right way.
Any suggestions?

Code: Select all

<?php
function phpQuery(){

     $query = mysql_query("INSERT INTO mytable VALUES('','name','email')");

}
?>
<script type="text/javascript">
function delayQueries()
{
  timeoutID = window.setTimeout(doQueries, 2000);
}

function doQueries()
{
  var runQuery = "<?php phpQuery(); ?>";    
}
</script>

Re: Calling php function inside javascript function

Posted: Fri Oct 15, 2010 1:47 am
by cpetercarter
You need t start at the beginning and understand that php runs on your server and javascript in the browser. You cannot "call a php function" from javascript - because your browser does not run php.

There is a technique called AJAX, whereby javascript in the browser sends a request back to the server, and does something with the results which the server returns. Trying to do AJAX in raw Javascript has reduced many fine folk to jibbering wrecks. If you want to explore what AJAX can do for you, it is best to use a Javascript library like jQuery.