Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2013, 10:17 AM   PM User | #1
typicalalex1
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
typicalalex1 is an unknown quantity at this point
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
typicalalex1 is offline   Reply With Quote
Old 02-27-2013, 10:49 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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; ?>";
devnull69 is offline   Reply With Quote
Old 02-27-2013, 11:00 AM   PM User | #3
typicalalex1
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
typicalalex1 is an unknown quantity at this point
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.
typicalalex1 is offline   Reply With Quote
Old 02-27-2013, 11:26 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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).
devnull69 is offline   Reply With Quote
Old 02-27-2013, 07:14 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,554
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
$row, array, javascript, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:59 AM.


Advertisement
Log in to turn off these ads.