如何在php函数中使用javascript变量? [关闭]

do someone have any idea that, how can i use jquery variable ( CIF ) in my php function (myphpfunction();)? please look at the example below. i need to call myphpfunction('1') php function in #mydiv.

<script>
var CIV = parseInt($("#mydiv").attr("data-id"));
if(CIV < 4) { var CIF = CIV += 1; } else { var CIF = 1; }

$("#mydiv").html('<? myphpfunction("' + CIF +'"); ?>');

</script>

This wouldn't work. The jQuery is being executed on the client-side, after the page has been loaded in a browser, after all php processing has completed. By the time jQuery is being processed, php processing has finished.

I would suggest performing the functionality all in javascript/jQuery if possible. If not, consider using jQuery's load function to get the contents of a php script executed with that var.

E.g.: test.php:

<?PHP 
    function myphpfunction($v){
    // Some processing here
    return 1;
    }
echo myphpfunction($_GET['v']); 
t?>

Your jQuery:

<script>
var CIV = parseInt($("#mydiv").attr("data-id"));
if(CIV < 4) { var CIF = CIV += 1; } else { var CIF = 1; }

$("#mydiv").load('test.php?v='+CIF);

</script>

AJAX would be your answer — by listening to a certain trigger, such as keyup, keypress, click or the likes, you can send JS variables to a PHP script, and fetch the output in a specific format (preferably JSON, or JSONP if you require cross-domain application).