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

Posted on 2014-02-10
Last Modified: 2014-02-10
Here is a drastically stripped down code stub:
<!--  ************************************** -->
<!doctype html>
<html lang="us">
$thecounty = $_REQUEST['county'];
require ("getcities.php");
	<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>

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

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

$(function() {
var citiesincounty = [
"thecities", "php", "variable", "needs", "to", "go", "here"

$( "#autocities" ).autocomplete({
                    source: citiesincounty
<!--  ************************************** -->

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!
Question by:DMTrump
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 58

Expert Comment

ID: 39848903
If I am following you correctly

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

Open in new window

LVL 58

Expert Comment

ID: 39848908
Amendment to that after rereading the code.

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

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

Open in new window

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

Accepted Solution

leakim971 earned 400 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...
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 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 :)

Author Closing Comment

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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I split a variable 19 45
Google Recaptcha 13 51
How does this modal gets closed? 6 41
How do I init a PHP page jusing JQuery? 8 29
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

734 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