2015-11-04 225 views
1

我想將php圖像url作爲javascript函數參數傳遞。如何將php變量作爲參數傳遞給JavaScript函數

請讓我知道我該怎麼做。

<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>

在javascript中我做的一些圖像編輯工作。

如何傳遞這個PHP變量(即圖像的URL作爲JavaScript函數參數)

+0

,你havr關閉按鈕。檢查我的答案。 –

回答

2

您需要內quotes封閉PHP代碼等作爲

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

在你的Javascript爲你傳遞價值內的功能,但沒有捕捉在您function demo()

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

先生,我也試過這個解決方案。但仍然沒有在JavaScript函數中傳遞價值 –

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

你必須用引號把它包起來,你必須關閉button

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

@Saty問題由OP編輯,看到這個,http://stackoverflow.com/posts/33521183/revisions –

+0

Okk得到它..我沒有看到問題被編輯! – Saty

+0

即使我很困惑,我查看了OP編輯的內容。 –

0

我發現$img是你的變量,並在代碼中你想包裝後發送$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> 
相關問題