Calling PHP Function within Javascript

前端 未结 5 1707
有刺的猬
有刺的猬 2020-12-11 19:22

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



        
相关标签:
5条回答
  • 2020-12-11 19:28

    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.

    0 讨论(0)
  • 2020-12-11 19:35

    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>
    
    0 讨论(0)
  • 2020-12-11 19:35

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

    0 讨论(0)
  • 2020-12-11 19:41

    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.

    0 讨论(0)
  • 2020-12-11 19:42

    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 讨论(0)
提交回复
热议问题