tags:

views:

87

answers:

3

Hi,

I am using fusion maps in one of my application.

In one of the example i have to pass the value from one map to another charts,

I am facing one problem if the data passed is numeric its displaying alert message correctly but if it is a string it generates an error:

NM is not defined

javascript:alert(NM)()

My code is as below:

$strXML .= "<entity id='" . $rs1['Internal_Id'] . "' value='" . round((($rs1['datap'] /  $sumdata) * 100),2) . "' link='javascript:alert(".($rs1['Internal_Id']) . ")'  />";

If i change the link part (passing single quotes in alert)that is:

$strXML .= "<entity id='" . $rs1['Internal_Id'] . "' value='" . round((($rs1['datap'] /  $sumdata) * 100),2) . "' link='javascript:alert('".($rs1['Internal_Id']) . "')'  />";

It displays invalid xml data.

Please help me on this

Thanks

Pankaj

+1  A: 

Use \" rather than ' to surround the JavaScript string.

$strXML .= "<entity id='" . $rs1['Internal_Id'] . "' value='" . round((($rs1['datap'] /  $sumdata) * 100),2) . "' link='javascript:alert(\"".($rs1['Internal_Id']) . "\")'  />";

What is happening is that the xml produced is like so:

<entity id='NM' value='1' link='javascript:alert('NM')'/>

Which as you should be able to see from SOs syntax highlighting ends the value for the link attribute after javascript:alert(' as you are using the same quotes for the javascript as you are using for surrounding the attribute values.

Using a different quote (" rather than ') doesn't end the attribute value (again see the syntax highlighting)

<entity id='NM' value='1' link='javascript:alert("NM")'/>


In PHP we have to escape the quote (Using \) so it isn't interpreted as a special character by the php interpreter and used to end the string, which is why in php you have to write \"

Yacoby
Oh, I posted the same you already did
DKSRathore
Great minds think alike ;)
Yacoby
A: 

You should change your

ink='javascript:alert('".($rs1['Internal_Id']) . "')'

by

ink='javascript:alert(\"".($rs1['Internal_Id']) . "\")'
DKSRathore
Hi, Thanks for reply but now it shows this error missing ) after argument list
Pankaj Khurana
Seems you $rs1['Internal_Id'] has some non word character like " or , or )
DKSRathore
If u don't mind, can you let us know what is your $rs1['Internal_Id'] value
DKSRathore
Hi,It has a string value like 'AK', 'UK'Even if i am giving static text value its generating an error.
Pankaj Khurana
Pankaj have you seen by reversing the ' with " like 'javascript:alert(\"".($rs1['Internal_Id']) . "\")'by link="javascript:alert('".($rs1['Internal_Id']) . "');\"
DKSRathore
A: 

Try:

$strXML .= "<entity id='" . $rs1['Internal_Id'] . "' value='" . round((($rs1['datap'] /  $sumdata) * 100),2) . "' link='javascript:alert(\"".($rs1['Internal_Id']) . "\")'  />";

Basically escaping your alert quotation marks :)

Robert Grant
Hi,Thanks for reply but now it shows this error missing ) after argument list
Pankaj Khurana