php变量如何得到js函数的返回值

JavaScript09

php变量如何得到js函数的返回值,第1张

可以使用ajax技术传递参数,比如:

function ShowResult()

{

sqlstr=document.getElementById("txtbook").value

selstr=document.getElementById("opt").value

dt="sql=" + sqlstr + "& sel=" + selstr

//window.alert(dt)

xmlhttp=new XMLHttpRequest()

xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("txtres").innerHTML=xmlhttp.responseText

    }

  }

xmlhttp.open("POST","listBook.php",true)

xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded") 

xmlhttp.send(dt)

return

}

和php一样,js中也有个eval,用他就行了.------------------------<script>

eval("alert('hello world')")

</script>------------------------这是个例子,alert()就是js的代码,把他保存为.html文件,你打开看看效果就知道怎么会事了 .

这要看你是什么过程.

一般的过程

直接写 例如

<php ecoh "js代码" ?php>

因为php是服务端加载, js是客户端加载,相互不影响.

AJAX

这种是js先运行, 会访问 PHP,php端运行后 echo 或者  return 回值就行,客户端自己识别的.