开发者

Calling PHP Function within Javascript

开发者 https://www.devze.com 2023-01-25 11:00 出处:网络
I want to know is it possible to call a php function within javascript, only and only when a condition is true. For example

I want to know is it possible to call a php function within javascript, only and only when a condition is true. For example

<s开发者_StackOverflow中文版cript 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). What you could do is use Ajax and make a call to a PHP page to get some results.


The PHP function cannot be called in the way that you have illustrated above. However you can call a PHP script using AJAX, code is as shown below. Also you can find a simple example here. Let me know if you need further clarification

Using 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

   <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>


No that's not possible. PHP code runs before (server-side) javascript (client-side)


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.

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.


PHP always runs before the page loads. JavaScript always runs after the page loads. They never run in tandem.

The closest solution is to use AJAX or a browser redirect to call another .php file from the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号