DMTrump
asked on
How do I put a PHP variable into a javascript function?
Here is a drastically stripped down code stub:
Please provide your answer by editing my code. Thanks!
<!-- ************************************** -->
<!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>
<!-- ************************************** -->
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!
Amendment to that after rereading the code.
Can you confirm what $thecities is - is it an array or string...?
...
$thecities= cities("$thecounty");
// Assuming a comma delimited list
$thecities=explode(",",$thecities);
...
...
<script>
$(function() {
var citiesincounty = <?=json_encode($thecities)?>;
...
Can you confirm what $thecities is - is it an array or string...?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Open in new window