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

<buttontype="submit"onclick="demo('<?phpecho$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 objalert(obj);
}

Solution 2:

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

<buttontype="submit"onclick='demo("<?phpecho$imageurl?>")' ></button>

Solution 3:

<buttontype="submit"onclick="demo('<?phpecho$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>functiondemo(val1)
{
      alert(val1);
}
</script><body><buttontype="submit"onclick=demo('<?phpecho$img?>') >Click Me</button></body></html>

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