day_dreamer
day_dreamer

Reputation: 834

Passing php variable values to javascript function

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

Answers (2)

Alexander Pavlov
Alexander Pavlov

Reputation: 32286

You should pass the value as string:

<input type="button" name="delete" value="Remove" onclick="del('<?echo $arr1[0]?>');"/>

Upvotes: 5

ThiefMaster
ThiefMaster

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

Related Questions