I want to pass php image url as javascript function parameter.
Please let me know how can I do this.
<html>
<?php
$image=$row['image'];
$img=$loc.'user/'.$image;
?>
<script type=text/javascript>
function demo()
{
some code here;
}
</script>
<body>
<button type="submit" onclick=demo(<?php echo $img?>) >
</body>
</html>
In javascript I am doing some editing work on image .
How can I pass this php variable (i.e. image url as javascript function parameter)
asked Nov 4, 2015 at 11:52
Rahul
7261 gold badge12 silver badges49 bronze badges
-
after wrapping, you havr to close the button as well. Check my answer.Niranjan N Raju– Niranjan N Raju2015年11月04日 11:58:59 +00:00Commented Nov 4, 2015 at 11:58
4 Answers 4
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);
}
answered Nov 4, 2015 at 11:54
Narendrasingh Sisodia
21.4k6 gold badges51 silver badges54 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Rahul
sir, I tried this solution also. but still not passing value in javascript function
You have to wrap it with quotes and you have to close the button
<button type="submit" onclick='demo("<?php echo $imageurl?>")' ></button>
answered Nov 4, 2015 at 11:57
Niranjan N Raju
12k4 gold badges24 silver badges41 bronze badges
1 Comment
Niranjan N Raju
@Saty question is edited by OP, see this, stackoverflow.com/posts/33521183/revisions
<button type="submit" onclick="demo('<?php echo $img;?>');" >
answered Nov 4, 2015 at 11:56
Shailesh Katarmal
2,7931 gold badge15 silver badges16 bronze badges
Comments
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>
answered Nov 4, 2015 at 11:59
AkshayP
2,1592 gold badges19 silver badges27 bronze badges
Comments
default