I am getting this error, 'Unterminated String Constent' in javascript error console. In google chrome it tells me the value I am supplying is invalid? Could someone help me find out why it is not working? Thank you. PHP code: //serialize data for javscript if(isset($media)) { $jmedia = json_encode($media); } Code (markup): Javascript Code: //populate the array function poparray() { if(document.getElementById('jmediai')) // jmediai text box contains the php array { jmedia = document.getElementById('jmediai').value; window.warray = eval('(' + jmedia + ')'); // window.warray should contain the javascript array but it is the line I am getting an error with clearInterval(ti); } else { ti = setInterval('poparray()', 1000); } } Code (markup): Thank you. If you need any more info please tell me.
try that and let me know the result please.. <script> //populate the array function poparray() { <?php if(isset($media)): ?> var jmedia = new Array(); <?php foreach($media as $key => $value): echo 'jmedia['.$key.']="'.$value.'"'; endforeach; ?> window.warray = eval('(' + jmedia + ')'); // window.warray should contain the javascript array but it is the line I am getting an error with clearInterval(ti); <?php else: ?> ti = setInterval('poparray()', 1000); <?php endif; ?> } </script> Code (markup):
It still gave me an error... I looked more into my code and I think I have found the problem. 'Unterminated String Constent' means that I have, in the array, a quote mark, right? So all I need to do is escape the quote mark for javascript from php. How would I do that using preg_replace from php? Also are there any other stuff I should preg_replace which could cause an 'Unterminated string constant' like a semi colon or brackets? Thanks.
please try that, I forgot ; in the previous one.. <script> //populate the array function poparray() { <?php if(isset($media)): ?> var jmedia = new Array(); <?php foreach($media as $key => $value): echo 'jmedia['.$key.']="'.$value.'";'."\n"; endforeach; ?> window.warray = eval('(' + jmedia + ')'); // window.warray should contain the javascript array but it is the line I am getting an error with clearInterval(ti); <?php else: ?> ti = setInterval('poparray()', 1000); <?php endif; ?> } </script> Code (markup):
I'm still getting the same error. I know why I'm getting the error now. It is because of a single quote mark in the array variable. I need to escape it through preg replace but it is not working. Here is what I was trying: $value = preg_replace('/\'/', '\'', $value); Code (markup): Could someone help me fix it. (preg_replace a single quote to an escaped single quote)
<?php $media = array('test', 'xxx', 'gapz', 'key' => 'value'); $jmedia = ''; //serialize data for javscript if(isset($media)) { $jmedia = json_encode($media); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test</title> <script type="text/javascript"> var ti; //populate the array function poparray() { if(document.getElementById('jmediai')) // jmediai text box contains the php array { jmedia = document.getElementById('jmediai').value; window.warray = eval('(' + jmedia + ')'); // window.warray should contain the javascript array but it is the line I am getting an error with alert(window.warray); if(ti) { clearInterval(ti); } } else { ti = setInterval('poparray()', 1000); } } </script> </head> <body> <input type="text" name="" value="<?php echo htmlentities($jmedia); ?>" id="jmediai" size="60" /> <input type="button" onclick="poparray();" value="click" /> </body> </html> PHP:
I can get my code to work if I str_replace the single quotes in the array with spaces. I was wondering if there was a way to str_replace the single quotes with escaped single quotes. I'm trying this but it won't work: $value = '\''; str_replace('\'','\\\',$value); //needs to return an escaped single quote Code (markup): Thanks!