Jump to content

Is there a function for this?


iPixel

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

  • 3 weeks later...

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.