0

I want that the following code pass var1,2 and 3 to the autofill.php so i can gather them using $_GET :

<script type="text/javascript">
$(document).ready(function() {
   $( "#name" ).autocomplete({
    source: function(request, response) {
        $.ajax({
            url: 'autofill.php',
            dataType: "json",
            data: {
                term : request.term,
                var : 3,
            },
            success: function(data) {
                response(data);
            }
        });
    },
    select : function(event, ui) {
            //pass var1,var2,var3 to php file
    },
    autoFocus:true,
     minLength: 3
   });
});
    </script>

the "source : function" works very well i can pass the var = 3 that decides which function to execute from autofill.php but I can't manage to send the data I need on select.

2
  • point 1: var is reserved word javascript. in select function is example ui.item.label and ui.item.value Commented Jul 20, 2020 at 22:15
  • You will need to setup a PHP Script first. Will it accept GET or POST? What will it do with the data? Then you can define what the select will do. Commented Jul 20, 2020 at 22:23

1 Answer 1

1

Consider the following.

$(document).ready(function() {
  $("#name").autocomplete({
    source: function(request, response){
      $.getJSON("autofill.php", { term: request.term, "var": 3 }, function(data){
        response(data);
      });
    },
    select : function(event, ui) {
      $.post("adddata.php", ui.item, function(data){
        console.log("Added", ui.item);
      });
    },
    autoFocus:true,
     minLength: 3
   });
});

This performs a GET when populating the Source. If an item is selected, it is then add performs a POST to a PHP script that can do something with it.

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

Comments

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.