简体   繁体   中英

how can i use returned values from javascript functions in php?

<script type="text/javascript">
           function mehdi(rno)
           {


               alert(rno);
               return rno * 10;
             }
</script>
    <input type="button" name ="submit" value="ثبت و تایید" onclick= " mehdi('10')">

<?php


?>

how can i use from returned value from mehdi() function?

In this case you can't since PHP is used only to render HTML.

You will have to use AJAX (AHAH) for it: http://en.wikipedia.org/wiki/Ajax_%28programming%29

You can't! Javascript runs on the browser, after your PHP script has finished executing.

You can't PHP is processed first and then page executes javascript.

you can send Ajax request thought to your PHP scripts.

you could do an ajax request within the medi function and the request could sent it via post to an file like "mehdi_js_result.php" :)

var ajaxify = function(obj) {
        var xmlHttp = null;
        try {
            xmlHttp = new XMLHttpRequest();
        }catch(e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e) {
                try {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                }catch(e) {
                    xmlHttp = null;
                }
            }
        }if (xmlHttp) {
            obj.method = obj.method.toUpperCase();
            xmlHttp.open(obj.method, obj.url, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            if(obj.method == 'POST') {
                if(typeof(obj.params) != 'undefined') {
                    xmlHttp.setRequestHeader("Content-length", obj.params.length);
                }
            }
            xmlHttp.setRequestHeader("Connection", "close");
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4) {
                    var json = eval(xmlHttp.responseText);
                    if(json.success) {
                        if(typeof(obj.success) == 'function'){obj.success(xmlHttp.responseText);}
                    }
                    else {
                        if(typeof(obj.failure) == 'function') {obj.failure(xmlHttp.responseText);}
                    }
                }
            };
            if(obj.method == 'POST' && typeof(obj.params) != 'undefined') {
                xmlHttp.send(obj.params);
            }
            else {
                xmlHttp.send(null);
            }
        }
    };
    function ajax(mehdi_result) {
        ajaxify({
            method: 'POST',
            url: 'mehdi_js_result.php',
            params: 'result='+result,
            success: function(response) {
                var json = eval(response);
                alert('success callback function! '+json.data);
            },
            failure: function(response) {
                var json = eval(response);
                alert('failure callback function! '+json.data);
            }
        });
    }

You can't do it directly. You have to use AJAX.

Once code comes to client side and it executes there, your server side script would have terminated already.

If you do need to send JavaScript return values, pass them back to server using AJAX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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