Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP MySQL array to Javascript

    Hi guys

    Im trying to pass a PHP array to JavaScript but i am unable to figure out how.
    The array is created from a MySQL query:

    Code:
    $sql="SELECT * FROM tblEmployees WHERE employeeID = '".$q."'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    I have no problem passing variables and other arrays to Javascript, but passing $row doesnt work.

    Code:
    $str = $row['firstName'];
    echo $str;
    $str displays the value of $row['firstName'] correctly.
    But it doesn't pass the value to JavaScript, it just shows up blank:

    This code gets the value of $str and tries to display it in a text box called txtTitle.
    Code:
    <script type="text/javascript">
    var val = <?php echo json_encode($str); ?>;
    document.getElementById("txtTitle").value = val;
    </script>
    Im pulling my hair out with this. I have searching through forums and i havent found any help.
    Thanks in advance

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Why would you want to json_encode() a simple string?

    Just output it. In Javascript you need to make sure that it's surrounded by quotes, though.
    Code:
    var val = "<?php echo $str; ?>";

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Why would you want to json_encode() a simple string?

    Just output it. In Javascript you need to make sure that it's surrounded by quotes, though.
    Code:
    var val = "<?php echo $str; ?>";
    That was just the last thing i tried. Your suggestion should work and i have tried it before but no luck.
    This is the code minus the SQL
    Code:
    	<?php
    	$str = $row['firstName'];
    	?>
    
    	<script type="text/javascript">
    	var val = "<?php echo $str; ?>";
    	alert(val);
    	document.getElementById("txtTitle").value = val;
    	</script>
    If i Echo $str in PHP, the value is displayed on screen.
    I put alert(val); in there just to test it, but it shows a blank message.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Please check the resulting HTML in the browser development tool of your choice (built-in to IE, Opera and Chrome ... use Firebug for Firebox) by pressing F12 and then on the HTML tab. You should see the output of $str at both places, so the problem must be somewhere else (maybe the error console on the tab "Console" of the development tools will show you).

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by devnull69 View Post
    Why would you want to json_encode() a simple string?
    because it turns it into a guaranteed valid ascii JS string, regardless of any nested quotes or apos chars.

    It's a waste for numbers, but it can help prevent a few common escaping issues when shuffling strings around.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •