iPixel Posted July 30, 2009 Share Posted July 30, 2009 I have to pass text pulled from the database and send it to javascript which will then use innerHTML and embed the text into a div layer. My issue is that the text being passed very likely posseses single quotes and obviously that messes up the javascript, is there anyway around this? I'm not sure if this would be a php side fix or a js side fix. Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 30, 2009 Share Posted July 30, 2009 If you have PHP5, you can use json_encode(): <script type="text/javascript"> var text = <?php echo json_encode('This is a "test" of the JS system'); ?>; </script> Quote Link to comment Share on other sites More sharing options...
iPixel Posted July 30, 2009 Author Share Posted July 30, 2009 that still gives me the Undetermined string constant, besides it looks to me like json_enclode replaces single quotes with double quotes right? that's not what i'm trying to do, i want to allow or trick js to take and use single quotes within the text without messing up it's own syntax. Here's the HTML passing the text >> <a href="#" onmouseover="tnpPOP('<?php echo $pg_txt['0']; ?>');">LINK</a> and the js >> function tnpPOP(POPTXT) { document.getElementById("tnpPOPtxt").style.display = 'block'; document.getElementById("tnpPOPtxt").innerHTML = POPTXT; } I guess if i could parse through the "text" and replace all single quotes ' with \' which would allow JS to do it's job Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 30, 2009 Share Posted July 30, 2009 It replace double quotes with escaped double quotes...but also adds it's own double quotes around the whole thing. You can probably just do an str_replace() to change ' to \' Can you post the code, so I can see the context it's being used in? Quote Link to comment Share on other sites More sharing options...
iPixel Posted July 30, 2009 Author Share Posted July 30, 2009 The above code is really all there is to it. I pass the txt via js function call with php and then use js and insert the passed txt using innerHTML directly into the div layer. There's no other code i could post that would make it any more helpfull. I tried this but still nothing... im not sure why. <a href="#" onmouseover="tnpPOP('<?php echo str_replace("'","\'",$pg_txt['0']); ?>');" onmouseout="tnpHIDE();"> LINK </a> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 30, 2009 Share Posted July 30, 2009 ah, that wasn't there before! in that case you can use: <a href="#" onmouseover="tnpPOP('<?php echo htmlspecialchars(str_replace("'","\\'",$pg_txt['0']),ENT_QUOTES); ?>');">LINK</a> Quote Link to comment Share on other sites More sharing options...
iPixel Posted July 30, 2009 Author Share Posted July 30, 2009 That still gives me Unterminated String Constant Error (javascript). I think it might have something to do with the returns in the text. Like when someone types in text and hits enter like so in order to not exceed a certain width while typing up text. And these enter(returns) might be the reason it's reacting like this. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 30, 2009 Share Posted July 30, 2009 ah, there are new lines too...try this: <a href="#" onmouseover="tnpPOP('<?php echo str_replace(array("'","\"","\n"),array("\\'",""",'\\n'),$pg_txt['0']); ?>');">LINK</a> but i still think this would work better with json_encode and a separate variable: <?php $pg_txt = array( 0 => "This is a 'test' with\nnew lines and \"stuff\"", 1 => "Another one", ); ?> <script> var pg_txt = <?php echo json_encode($pg_txt); ?>; function tnpPOP ( key ) { alert(pg_txt[key]); } </script> <a href="#" onmouseover="tnpPOP('0');">LINK</a> Quote Link to comment Share on other sites More sharing options...
newbiz Posted August 18, 2009 Share Posted August 18, 2009 Hi, i am having a similar issue. I have an array that is being split using the php split() function. I am getting an array for a postage calculation off the Australia Post website (Delivery Rate Calculator). Using the split function i get access to the individual peices of data (charge, days and error message) When this is echo'd to the screen, everything presents fine. However when i try to create a javascript alert screen, i get the unterminated string constant error. I am assuming there is a newline or carriage return character being inserted when the array is being split. I have tried a srt_replace for a carriage return, newline, etc, but no luck. Does anyone know what is causing this error? Below is my code: <?php $myfile = file('http://drc.edeliver.com.au/ratecalc.asp?Pickup_Postcode='.$pickpc.'&Destination_Postcode='.$destpc.'&Country='.$count.'&Weight='.$weight.'&Service_Type='.$service.'&Length='.$length.'&Width='.$width.'&Height='.$height.'&Quantity='.$qty); $ship = split('=',$myfile[0]); $day = split('=',$myfile[1]); $error = split('=',$myfile[2]); print_r($myfile); global $shipping; global $days; global $err; $shipping = $ship[1]; $days = $day[1]; $err = $error[1]; //attempt to remove possible newline character $shipping = str_replace(Chr(21)," ", $shipping); $days = str_replace(Chr(21)," ", $days); $err = str_replace(Chr(21)," ", $err); //returns the shipping figure, i.e. 8.95 echo $shipping; //returns estimated days to deliver, i.e. 2 echo $days; //returns an error or OK message echo $err; ?> <script language="javascript"> alert('Shipping Estimates\nCost: <?php echo $shipping ?>\nDays: <?php echo $days ?>\n<?php echo $err ?>') </script> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 18, 2009 Share Posted August 18, 2009 Can't you just use addslashes or addcslashes? Quote Link to comment Share on other sites More sharing options...
newbiz Posted August 18, 2009 Share Posted August 18, 2009 Yep, addcslashes() worked for me. Thanks Daniel0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.