0

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

Charles
  • 50,943
  • 13
  • 104
  • 142
umar
  • 3,073
  • 5
  • 35
  • 45
  • 3
    Try changing your function head from `function addRowToTable(var a)` -> `function addRowToTable(a)`, does a contain now your value? – Nick Weaver Mar 22 '11 at 22:40

3 Answers3

2

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.

Mike Park
  • 10,845
  • 2
  • 34
  • 50
2

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2

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

Community
  • 1
  • 1
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113