简体   繁体   English

PHP函数不会将值返回给javascript变量

[英]php function doesn't return value to javascript variable

I'm trying to call php function "funct1()" from javascript. 我正在尝试从JavaScript调用php函数“ funct1()”。 I'm calling javascript function "clicked()" from button onclick. 我从按钮onclick调用javascript函数“ clicked()”。 The problem is when i click the button in php function doesn't return value to "someVariable". 问题是当我单击php函数中的按钮时,返回的值未返回“ someVariable”。

<?php
function funct1()
{
    if(isset($_GET['cmbcode']))
    {
        $name = $_GET['cmbcode']; 
        echo $name; 
    }
}
?>
<script type='text/javascript'>
    function clicked() 
    {
        var someVariable="<?php echo funct1(); ?>";
        alert(someVariable);

    }
</script>

Since you are not returning the $name variable in your funct1() function it will not work with echo because it acts towards it as a variable. 由于您没有在funct1()函数中返回$name变量,因此它无法与echo因为它将作为变量起作用。

Change your code to either return $name and then echo it using echo funct1() or just run funct1() without the echo ( var someVariable="<?php funct1(); ?>"; ) 你的代码更改为返回$name ,然后echo使用它echo funct1()或只运行funct1()echovar someVariable="<?php funct1(); ?>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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