passing xml string to javascript function. Not successful.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jdoe
Forum Newbie
Posts: 14
Joined: Sun Sep 12, 2010 8:50 pm

passing xml string to javascript function. Not successful.

Post by jdoe »

Hi.

I am trying to pass xml data string from a clicked link to a javascript function.
Eventually, I wish to have a javascript function to pass this info back to php. (For AJAX purpose)

//this is my xml data string, which DIDN'T WORK.
$myXMLdata = "<?xml version="1.0"?> <archive><articleid>6</articleid></archive>";

//this modified xml data string, which WORKED.
$myXMLdata = " <archive><articleid>6</articleid></archive>";

$option_link = "<a href=\"javascript:;\" onclick=\"return showAlert('$myXMLdata ');\">";

When I click on the link, the javascript function below will be called, and the text will be displayed in a pop up window.

function showAlert(myText) {
alert('The text is ' + myText + '.');
}

Could you please help to explain why the $myXMLdata with "...<?xml version="1.0"?>..." will not fire up the showAlert function, but the $myXMLdata without "...<?xml version="1.0"?>..." will call the showAlert Function.

Thanks.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: passing xml string to javascript function. Not successfu

Post by cpetercarter »

You have placed double quotation marks inside a double-quoted string.

Code: Select all

$myXMLdata = "<?xml version="1.0"?> <archive><articleid>6</articleid></archive>"; 
Try:

Code: Select all

$myXMLdata = '<?xml version="1.0"?> <archive><articleid>6</articleid></archive>'; 
jdoe
Forum Newbie
Posts: 14
Joined: Sun Sep 12, 2010 8:50 pm

Re: passing xml string to javascript function. Not successfu

Post by jdoe »

hi cpetercarter,

thanks for your reply. however, it didn't work.
$myXMLdata = '<?xml version="1.0"?> <archive><articleid>6</articleid></archive>';

these works: (removing the double-quotes around the 1.0)
$myXMLdata = '<?xml version=1.0?> <archive><articleid>6</articleid></archive>';
$myXMLdata = "<?xml version=1.0?> <archive><articleid>6</articleid></archive>";

Anyway, i have found a workaround solution.

Instead of using the new DOMDocument('1.0') to create my XML data string, I am just using simple text $myXMLData = "<archive><articleid>6</articleid></archive>". This will not have the <?xml version="1.0"?>

Tqvm.
Post Reply