scooby545 Posted February 5, 2010 Share Posted February 5, 2010 hi guys, am well confused by this one ... am trying to insert a piece of html code as a variable : $code = "$rbel[mycode]"; $code = str_replace("\n", "", $code); $code = str_replace("\r", "", $code); $code = str_replace("'", "'", $code); $code = str_replace('"', '"', $code); $code = str_replace('<', '<', $code); $code = str_replace('>', '>', $code); echo' <script type="text/javascript"> var code = "'; echo"$code"; echo'"; </script> '; if i do the variable this way it echo's out in the html like so : var code = "<P><TABLE style="BORDER-RIGHT: 0px; PADDING-RIGHT: 5px; BORDER-TOP: 0px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: 0px; WIDTH: 650px; PADDING-TOP: 5px; BORDER-BOTTOM: 0px; BORDER-COLLAPSE: separate" align=center><TBODY><TR><TD style="BORDER-RIGHT: #aaaaaa 1px dashed; BORDER-TOP: #aaaaaa 1px dashed; BORDER-LEFT: #aaaaaa 1px dashed; BORDER-BOTTOM: #aaaaaa 1px dashed"> Single Column Text Element</TD></TR></TBODY></TABLE></P>"; but refuses to work ?? if i type the variable in manually like : $code = "$rbel[mycode]"; $code = str_replace("\n", "", $code); $code = str_replace("\r", "", $code); $code = str_replace("'", "'", $code); $code = str_replace('"', '"', $code); $code = str_replace('<', '<', $code); $code = str_replace('>', '>', $code); $code = "<P><TABLE style="BORDER-RIGHT: 0px; PADDING-RIGHT: 5px; BORDER-TOP: 0px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: 0px; WIDTH: 650px; PADDING-TOP: 5px; BORDER-BOTTOM: 0px; BORDER-COLLAPSE: separate" align=center><TBODY><TR><TD style="BORDER-RIGHT: #aaaaaa 1px dashed; BORDER-TOP: #aaaaaa 1px dashed; BORDER-LEFT: #aaaaaa 1px dashed; BORDER-BOTTOM: #aaaaaa 1px dashed"> Single Column Text Element</TD></TR></TBODY></TABLE></P>"; echo' <script type="text/javascript"> var code = "'; echo"$code"; echo'"; </script> '; it works perfectly, yet echo's out the same in the html ? am sure its somthing to do with the way php is treating the string, but dont know how to fix it any help would be greatly appreciated Scooby Quote Link to comment https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/ Share on other sites More sharing options...
jl5501 Posted February 5, 2010 Share Posted February 5, 2010 You need to be sure there are no linefeeds in the resultant string that you expect javascript to understand Quote Link to comment https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/#findComment-1007345 Share on other sites More sharing options...
scooby545 Posted February 5, 2010 Author Share Posted February 5, 2010 what do you mean by linefeeds ? Quote Link to comment https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/#findComment-1007349 Share on other sites More sharing options...
jl5501 Posted February 5, 2010 Share Posted February 5, 2010 Newlines. Does the line show as one line? Quote Link to comment https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/#findComment-1007351 Share on other sites More sharing options...
oni-kun Posted February 5, 2010 Share Posted February 5, 2010 $code = str_replace("\n", "", $code); $code = str_replace("\r", "", $code); $code = str_replace("'", "'", $code); $code = str_replace('"', '"', $code); $code = str_replace('<', '<', $code); $code = str_replace('>', '>', $code); $code = htmlentities($rbel['mycode'], ENT_QUOTES); Can you not just do that? Quote Link to comment https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/#findComment-1007357 Share on other sites More sharing options...
scooby545 Posted February 5, 2010 Author Share Posted February 5, 2010 yeah all shoes as one line if i echo it out or type it in manually, which is what i cant understand. If i echo the non working variable out and copy it from the html source code into the php to specify it manually it works like a charm ? seems to be somthing to do with pulling the value from a mysql database and formatting it. Quote Link to comment https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/#findComment-1007359 Share on other sites More sharing options...
scooby545 Posted February 5, 2010 Author Share Posted February 5, 2010 thankyou oni-kun that bit of code is very usefull still get the same problem with the code only working if i set the variable manually tho ?? Quote Link to comment https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/#findComment-1007360 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.