大家好,我有一个PHP文件和以下代码:

<?php echo "Hello World"; ?>
<script type="text/javascript">
    $(document).on('click', '.order-submit', function() {
        dataName = $(this).data('name');
        var company = <?php echo json_encode(get_Comp( /* dataName here */)); ?>;
    });
</script>


如何将javascript变量“ dataName”传递给我的php函数“ get_Comp”?
如果有人帮助,我将感到非常高兴。谢谢!

最佳答案

这是不可能的,您将需要使用ajax实现此目的:

PHP的getcomp.php

<?php
   //put here your get_Comp() function

   $dataName = $_POST['dataName'];
   echo json_encode(get_Comp($dataName));
?>


JAVASCRIPT

<?php echo "Hello World"; ?>
<script type="text/javascript">
    $(document).on('click', '.order-submit', function() {
        dataName = $(this).data('name');
        var company;
        $.post('getcomp.php', {dataName: dataName}, function(data) {
            company = data; //company should be equal to json_encode(get_Comp($dataName));
        });
    });
</script>

10-02 16:37