1

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)

1
  • after wrapping, you havr to close the button as well. Check my answer. Commented Nov 4, 2015 at 11:58

4 Answers 4

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

sir, I tried this solution also. but still not passing value in javascript function
2

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

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

1 Comment

@Saty question is edited by OP, see this, stackoverflow.com/posts/33521183/revisions
0
<button type="submit" onclick="demo('<?php echo $img;?>');" >

Comments

0

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>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.