不能以上面说明的方式调用PHP函数。但是,您可以使用AJAX调用PHP脚本,代码如下所示。您也可以在这里找到一个简单的示例。让我知道您是否需要进一步澄清
使用jQuery
<script type="text/javascript" src="./jquery-1.4.2.js"></script><script type="text/javascript">function compute() { var params="session=123"; $.post('myphpscript.php',params,function(data){ alert(data);//for testing if data is being fetched var myObject = eval('(' + data + ')'); document.getElementById("result").value=myObject(addend_1,addend_2); }); }</script>
准系统Javascript替代
<script type="text/javascript"> function compute() { var params="session=123" var xmlHttp; var addend_1=document.getElementById("par_1").value; var addend_2=document.getElementById("par_2").value; try { xmlHttp = new XMLHttpRequest(); } catch (e) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("No Ajax for YOU!"); return false; } } } xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { ret_value=xmlHttp.responseText; var myObject = eval('(' + ret_value + ')'); document.getElementById("result").value=myObject(addend_1,addend_2); } } xmlHttp.open("POST", "http://yoururl/getjs.php", true); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlenpred"); xmlHttp.setRequestHeader("Content-length", params.length); xmlHttp.setRequestHeader("Connection", "close"); xmlHttp.send(params); } </script>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)