0

I'm trying to call php function "funct1()" from javascript. I'm calling javascript function "clicked()" from button onclick. The problem is when i click the button in php function doesn't return value to "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>
1
  • 1
    you should return the $name and not echo inside the function
    – Yair.R
    May 8, 2016 at 6:49

1 Answer 1

3

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.

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(); ?>";)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.