Calling php function inside javascript function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Calling php function inside javascript function

Post 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>
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Calling php function inside javascript function

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