Skip to content Skip to sidebar Skip to footer

Quotes Problem When Passing Value To Javascript

I am using like $myPage .= 'Edit'; where $row['name'

Solution 1:

Try:

$myPage .= "<td><a href='javascript:editProduct({$row['id']},\""
           . htmlentities( $row['name'] )
           . "\")'>Edit</a></td>";

htmlentities default behaviour is to convert double quotes and leave single quotes alone, if you require converting single and double quotes, then call it like this:

htmlentities( $row[ 'name' ], ENT_QUOTES )

Also, using { .. } in "..." strings is the correct way to substitute variables.


Solution 2:

The PHP string

'<a href=\'javascript:editProduct('.$row['id'].',"'.$row['name'].'")\'>';

outputs (assuming some values)

<td><a href='javascript:editProduct(123,"abc")'></td>

Presumably it breaks if $row['name'] contains a " quote. You could replace such quotes with a \" in the string before you output it using str_replace('"', '\"', $row['name'])


Post a Comment for "Quotes Problem When Passing Value To Javascript"