0

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

6
  • Can you improve the description as to why it "doesn't seem to be working"? Commented Feb 9, 2015 at 18:26
  • check my update, you could also check the fiddle to see what results you dont get :) Commented Feb 9, 2015 at 18:57
  • It would be better if you provided a minimal and verifiable example that does not require access to external resources. This makes easier for other to provide an answer to your question and helps future searchers that might benefit from your question. See stackoverflow.com/help/mcve Commented Feb 9, 2015 at 19:00
  • You say you don't receive any results on the autocomplete list. Did you check whether the server is returning the expected data to the client? Commented Feb 9, 2015 at 19:00
  • @h7r yes it is returning the data Commented Feb 9, 2015 at 19:18

1 Answer 1

1

In the provided jsfiddle there is no jQueryUI library included. Autocomplete is part of jQueryUI library so you have to include it.

Sign up to request clarification or add additional context in comments.

3 Comments

that's what I was checking in the fiddle site! :D
there is one included?
Note the difference betweeen jQuery and jQueryUI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.