0

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?

<?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>
2
Contributors
4
Replies
9
Views
6 Years
Discussion Span
Last Post by Smudly
0

Eg.

<?php
$var=$_POST['check'];
if($check=="yes"){
/////************/////////////
}
?>
...
<script type="text/javascript">
function sub(){
document.form.check.value="yes"
document.form.submit();
}
</script>
<form action="" method="post" name="form">
<input type="hidden" name="check">
</form>
<input type="button" onclick="sub()" value="OK">

or with ajax!
Content of some file (sf.php)

<?php
////*************/////////
//query......
echo "OK";
?>

javascript/ajax

<script type="text/javascript">
function do(){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=f(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//result (of "echo" in sf.php; "OK")
//in div element will write "OK"
document.getElementById('res').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","sf.php",true);
xmlhttp.send();
}
</script>
<div id='res'></div>
<input type="button" value="Do" onclick="Do">

Edited by ivan3510: supplement

0

Hey thanks for the examples. I am looking through trying to understand. I currently have the code on a couple test files. When clicking the button, nothing happens. I created the sf.php file and types what you have above. You can see it here:
http://www.sheetmusichaven.com/testinterval.php

I'll keep working at it until you are able to reply. Thanks

Edit:

I noticed the Function name: function do(){
was not capitalized like it was in the input tag. I changed that but still not displaying "OK".

Edited by Smudly: n/a

0

Solution:
content of document that contains javascript/ajax...

<html>
<script type="text/javascript">
function da(){
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function f(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById('res').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","sf.php",true);
xmlhttp.send();
}
</script>
<body>
<input type="button" value="da" onclick="da()">
<div id='res'></div>
</body>
</html>

Checked!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.