简体   繁体   English

在Javascript中调用PHP函数

[英]Calling PHP Function within Javascript

I want to know is it possible to call a php function within javascript, only and only when a condition is true. 我想知道是否有可能仅在条件为真时在javascript中调用php函数。 For example 例如

<script type="text/javascript">
if (foo==bar)
{
phpFunction(); call the php function
}
</script>

Is there any means of doing this.. If so let me know. 有没有这样做的方法。如果是这样,请告诉我。 Thanks 谢谢

PHP is server side and Javascript is client so not really (yes I know there is some server side JS). PHP是服务器端,而Javascript是客户端,所以实际上不是(是的,我知道有一些服务器端JS)。 What you could do is use Ajax and make a call to a PHP page to get some results. 您可以做的是使用Ajax并调用PHP页面以获得一些结果。

No that's not possible. 不,那是不可能的。 PHP code runs before (server-side) javascript (client-side) PHP代码 (服务器端)javascript(客户端) 之前运行

The PHP function cannot be called in the way that you have illustrated above. 不能以上面说明的方式调用PHP函数。 However you can call a PHP script using AJAX, code is as shown below. 但是,您可以使用AJAX调用PHP脚本,代码如下所示。 Also you can find a simple example here . 您也可以在这里找到一个简单的示例。 Let me know if you need further clarification 让我知道您是否需要进一步澄清

Using Jquery 使用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>

Barebones Javascript Alternative 准系统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-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.send(params);
    }
    </script>

The other answers have it right. 其他答案都正确。

However, there is a library, XAJAX , that helps simulate the act of calling a PHP function from JavaScript, using AJAX and a particularly designed PHP library. 但是,有一个库XAJAX ,它可以使用AJAX和专门设计的PHP库来模拟从JavaScript调用PHP函数的行为。

It's a little complicated, and it would be much easier to learn to use $.get and $.post in jQuery, since they are better designed and simpler, and once you get your head around how they work, you won't feel the need to call PHP from JavaScript directly. 这有点复杂,而且在jQuery中使用$ .get和$ .post会更容易学习,因为它们设计得更好,更简单,一旦掌握了它们的工作原理,您将不会感到需要直接从JavaScript调用PHP。

PHP always runs before the page loads. PHP始终在页面加载之前运行。 JavaScript always runs after the page loads. JavaScript总是在页面加载后运行。 They never run in tandem. 他们从不串联。

The closest solution is to use AJAX or a browser redirect to call another .php file from the server. 最接近的解决方案是使用AJAX或浏览器重定向从服务器调用另一个.php文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM