Reputation: 81
I've simplified the problem down as much as possible, I feel like this should work. The below script controls the autocomplete function and works as expected (if a user types 't', 'test' appears as a suggested input).
<script type="text/javascript">
$(function() {
var availableClients = ['test'];
$("#addClient").autocomplete({
source: availableClients,
});
});
</script>
However, if I set a PHP variable $test = "'test'" and replace 'test' in the original script with <?php echo $test; ?>
the autocomplete script no longer works. Is there another way I can use a PHP variable as the autocomplete source or am I missing something?
EDIT: Here is the script with the PHP in it.
<script type="text/javascript">
$(function() {
var availableClients = [<?php echo $test; ?>];
$("#addClient").autocomplete({
source: availableClients,
});
});
</script>
And the only relevant PHP code is this.
$test = "'test'";
Upvotes: 0
Views: 1279
Reputation:
The issue is probably that you aren't including the quotes when echoing the value of the variable. Try this:
var availableClients = ['<?php echo $test; ?>'];
As it turns out, the issue was actually that you were not defining $test
before you were echo
ing it. This:
$test = "'test'";
needs to occur before this:
<script type="text/javascript">
$(function() {
var availableClients = [<?php echo $test; ?>];
$("#addClient").autocomplete({
source: availableClients,
});
});
</script>
Upvotes: 1