开发者

passing value to javascript

hello plea开发者_开发百科se help me out regarding this dunction i want to pass value from the textbox to javascript function and over there i want to store it for other text box . here is the sample code '

<input type="button" value="Add" onClick="addRowToTable(<?php echo $data['pno'];?>);" />

here is the javascript function

function addRowToTable(var a)
{
  ........................
.......................

  b.type = 'text';
  b.value ='over here how can i get this value of a ';  
}

Thanks


Use json_encode() : Pass a PHP string to a JavaScript variable (and escape newlines)


IF your value for $data['pno'] is a string, and not a number type, you need to do this :

<input type="button" value="Add" onClick="addRowToTable('<?php echo $data['pno'];?>');" />

because you are still passing the value to addRowToTable as a string literal.

Also, what Nick Weaver said--nix the var in the function signature.


Let json_encode() take care about turning your variable in a JavaScript-compatible string:

<input type="button" value="Add" onclick="addRowToTable('<?php echo htmlspecialchars(json_encode($data['pno'])); ?>');" />

PS: If you are using XHTML it's onclick, not onClick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜