I am using this PHP/JQuery code:
<?php
$return_arr = array();
$sql2="SELECT * from contacts where status = 'Contact' and company = '".$_GET["company"]."' ";
$rs2=mysql_query($sql2,$conn);
while($result2=mysql_fetch_array($rs2)) {
$return_arr[] = array('label' => $result2["forename"].' '.$result2["surname"], 'value' => $result2["email"]);
}
$data = json_encode($return_arr);
?>
<script type="text/javascript">
$(document).ready(function(){
var data = <?php echo $data; ?>;
$("#contact_name").autocomplete({
source:data,
change: function(e, ui) {
if (ui.item === null) {
alert("Contact Does Not Exist.\n\nCheck the box below to add the entered details as a new contact of this company");
$("#AddAsContact").css('display', 'block');
$("#AddAsContactLabel").css('display', 'block');
}
},
select: function(e, ui) {
e.preventDefault() // <--- Prevent the value from being inserted.
$("#contact_email").val(ui.item.value);
$(this).val(ui.item.label);
$("#AddAsContact").css('display', 'none');
$("#AddAsContactLabel").css('display', 'none');
$('#AddAsContact').prop('checked', false);
}
});
});
</script>
but the autocomplete function doesn't seem to be working. I am not receiving any results in the auto complete list, there is however data being returned in the data variable
I have created a fiddle here: http://jsfiddle.net/n492uf07/
UPDATE:
The data var has this data currently:
var data = [{"label":"Paul","value":"[email protected]"},{"label":"Dave","value":"[email protected]"}];
I have also tried the code from the JQuery website (http://jqueryui.com/autocomplete/)
http://jsfiddle.net/n492uf07/2/
and thats not working in a fiddle either