Skip to content Skip to sidebar Skip to footer

How To Pass Php Variable To JavaScript Function As Parameter

I want to pass php image url as javascript function parameter. Please let me know how can I do this. In javascript I am doing some editing work on image . How can I pass this p

Solution 1:

You need to enclose the PHP code within quotes like as

<button type="submit" onclick="demo('<?php echo $imageurl;?>');" >
                                  //^^                      ^^

Within your Javascript as you were passing value within function but not capturing that value within your function demo()

function demo(obj)
{           //^^^ Capturing value within function using variable name obj
   alert(obj);
}

Solution 2:

You have to wrap it with quotes and you have to close the button

<button type="submit" onclick='demo("<?php echo $imageurl?>")' ></button>

Solution 3:

<button type="submit" onclick="demo('<?php echo $img;?>');" >

Solution 4:

I found $img is your variable and in code you are trying to send $imageurl

<html> 
<?php
$image=$row['image'];
$img=$loc.'user/'.$image;
?>

<script>
function demo(val1)
{
      alert(val1);
}
</script>
<body>
<button type="submit" onclick=demo('<?php echo $img?>') >Click Me</button>
</body>
</html>

Post a Comment for "How To Pass Php Variable To JavaScript Function As Parameter"