Reputation: 834
I'm passing PHP variable value like this:
<input type="button" name="delete" value="Remove" onclick="del(<?echo $arr1[0]?>);"/>
del()
is a Javascript function and Eg:$arr1[0]=234-675-90
the value of $arr1[0] is obtained from MYSQL database and its data-type in mysql is varchar.
I'm fetching value using mysql_query.
The problem I'm facing is that when value is passed to JavaScript function it takes it wrongly(eg:instead of 234-675-99 as -876).Is there any casting is to be done to pass value from PHP to JavaScript?
Any help is greatly appreciated.
Upvotes: 0
Views: 2224
Reputation: 32286
You should pass the value as string:
<input type="button" name="delete" value="Remove" onclick="del('<?echo $arr1[0]?>');"/>
Upvotes: 5
Reputation: 318508
Use json_encode()
to convert the value into value JavaScript:
<input type="button" name="delete" value="Remove" onclick="del(<?=htmlspecialchars(json_encode($arr1[0]))?>);"/>
Upvotes: 2