Mahdi_Nine
Mahdi_Nine

Reputation: 14751

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?

Upvotes: 0

Views: 526

Answers (5)

GorillaMoe
GorillaMoe

Reputation: 4134

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);
            }
        });
    }

Upvotes: 0

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22064

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.

Upvotes: 0

Headshota
Headshota

Reputation: 21449

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

you can send Ajax request thought to your PHP scripts.

Upvotes: 1

Jeroen Versteeg
Jeroen Versteeg

Reputation: 353

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

Upvotes: 1

Motiejus Jakštys
Motiejus Jakštys

Reputation: 2979

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

Upvotes: 1

Related Questions