Need help transfering an array from PHP to javascript...

Discussion in 'PHP' started by Imozeb, Jun 4, 2010.

  1. #1
    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.
     
    Imozeb, Jun 4, 2010 IP
  2. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #2
    nabil_kadimi, Jun 4, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No. The PHP variable needs to be a php array. (transfered to a javascript array)
     
    Imozeb, Jun 5, 2010 IP
  4. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    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):
     
    mehmetm, Jun 5, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    Imozeb, Jun 6, 2010 IP
  7. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #7
    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):
     
    mehmetm, Jun 6, 2010 IP
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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)
     
    Imozeb, Jun 6, 2010 IP
  9. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Please help!
     
    Imozeb, Jun 7, 2010 IP
  10. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #10
    
    
    <?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:
     
    gapz101, Jun 7, 2010 IP
  11. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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!
     
    Imozeb, Jun 7, 2010 IP
  12. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #12
    http://php.net/manual/en/function.addslashes.php
     
    mehmetm, Jun 7, 2010 IP