?
Solved

How do I put a PHP variable into a javascript function?

Posted on 2014-02-10
5
Medium Priority
?
347 Views
Last Modified: 2014-02-10
Here is a drastically stripped down code stub:
<!--  ************************************** -->
<!doctype html>
<html lang="us">
<?
$thecounty = $_REQUEST['county'];
require ("getcities.php");
?>
<head>
	<link href="css/smoothness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
	<script src="js/jquery-1.10.2.js"></script>
	<script src="js/jquery-ui-1.10.4.custom.js"></script>
</head>

<body>
<?
   $thecities= cities("$thecounty"); 
   echo $thecities;   // just to show that the string of cities is really returned by the cities function
?>

<br>
<form>
<input id="autocities" size=20 name="icity" />
<input type="submit" value="submit" id="submitForm" >
</form>

</body>
<script>
$(function() {
		
var citiesincounty = [
"thecities", "php", "variable", "needs", "to", "go", "here"
];

$( "#autocities" ).autocomplete({
                    source: citiesincounty
}); 
});
</script>
</html>
<!--  ************************************** -->

Open in new window

What I need to do is take the variable $thecities returned by the php function "cities" and place it into the javascript variable "citiesincounty".  I have confirmed that $thecities is correctly formed for the Javascript array (if I paste it in in place of the fake array, it works.

Please provide your answer by editing my code.  Thanks!
0
Comment
Question by:DMTrump
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39848903
If I am following you correctly

var citiesincounty = <?=json_encode($thecities)?>;

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39848908
Amendment to that after rereading the code.

...
$thecities= cities("$thecounty"); 
// Assuming a comma delimited list
$thecities=explode(",",$thecities);
...

...
<script>
$(function() {
var citiesincounty = <?=json_encode($thecities)?>; 
...

Open in new window


Can you confirm what $thecities is - is it an array or string...?
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 1600 total points
ID: 39848924
// just to show that the string of cities is really returned by the cities function

Could you show we what you see?
What about :

var citiesincounty = "<?php echo $thecities; ?>";

or :

var citiesincounty = <?php echo $thecities; ?>;

or :

var citiesincounty = [<?php echo $thecities; ?>];

depending what you see...
0
 
LVL 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 400 total points
ID: 39848959
leakim971's last answer is the way to go:

var citiesincounty = [ <?php echo $thecities ?> ];

Open in new window

FYI - you might want to consider using full PHP tags instead of the short tags:

<?php instead of <?
<?php echo instead of <?=

Long tags will always work - short tags will sometimes work :)
0
 

Author Closing Comment

by:DMTrump
ID: 39849029
thanks all for helping out - I think I've got to be more careful about checking for typos in my code before I decide that something won't work the way I expect.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question