Autocomplete jquery text field from mysql (use the same field several times to add more options) [closed]

0

I do not have much idea of jquery. I need to make an autocompletable text field, look for what I have in the database, that part I have covered more or less with this code here: link

But what I need is something more complex and I do not even know where to start it.

When entering some letters or words in the text area, I get the ones that coincide partially, or totally, but I want to click on one of the "suggestions" the data of this is put into an array (the id , price, name of an item, for example) and that is reflected in some way in the html code, that is to say, it is visual for the end user. After selecting one of the items in the table, I need the text field used before to search to be "reset" and can be used again in the same way, that is to find another word and when clicking it is added to the array, to save that data and send it in a form.

I do not know if I have explained myself correctly and suggestions are accepted to change the question or the title since I do not know how to correctly describe what I am looking for ...

Thank you.

EDIT:

At first you do not know how many items are going to be selected, as much as 1 can be 20.

    
asked by Pavlo B. 16.01.2017 в 17:12
source

1 answer

1

It seems to me from what you see in the example of the link that you put, that you are using the autocomplete of Jquery. So I found the following link with an example that fits with your request:

Example of Jquery Autocomplete

To make it work you need two parts, the example that you put tells you how to get the data from the MySQL database, so instead of the object that brings the example of the JQuery site, the Javascript variable projects

var projects = [
  {
    value: "jquery",
    label: "jQuery",
    desc: "the write less, do more, JavaScript library",
    icon: "jquery_32x32.png"
  },
  {
    value: "jquery-ui",
    label: "jQuery UI",
    desc: "the official user interface library for jQuery",
    icon: "jqueryui_32x32.png"
  },
  {
    value: "sizzlejs",
    label: "Sizzle JS",
    desc: "a pure-JavaScript CSS selector engine",
    icon: "sizzlejs_32x32.png"
  }
];

This should be deleted and replaced by what you bring from the database.

So you would have to change the sample code that you put, so that when the JSON Encode is done, bring inside the objects with all the info you want, I'll give you an example of how it could be:

//get matched data from skills table
$query = $db->query("SELECT * FROM skills WHERE skill LIKE '%".$searchTerm."%' ORDER BY skill ASC");
$i = 0;
while ($row = $query->fetch_assoc()) {
    $data[$i]['id']     = $row['id'];
    $data[$i]['image']  = $row['image'];
    $data[$i]['name']   = $row['name'];
    $data[$i]['value']  = $row['value'];
    $i++;
}

//return json data
echo json_encode($data);

When doing the json_encode you will see that it already takes a form more similar to the javascript array projects of the example the JQuery page that I put you.

Editing: To make the data of the selection pass to another page of PHP you can do several things, in the example of the link that I previously put:

 select: function( event, ui ) {
    $( "#project" ).val( ui.item.label ); //Como puedes ver en el objeto ui.item vienen los valores que vienen del JSON que mando el php que se creo anteriormente. En este caso #project podría ser el objeto hidden
    $( "#project-id" ).val( ui.item.value );
    $( "#project-description" ).html( ui.item.desc );
    $( "#project-icon" ).attr( "src", "images/" + ui.item.icon );

    return false;

In this case, as mentioned in the following article from Stack Overflow in English , you can make a single element type <input type ="hidden"> on which using JSON.stringify becomes another JSON object that you can easily manipulate when sending to your PHP target page.

The entire script should look something like this:

 <script>
$( function() {


$( "#project" ).autocomplete({
  minLength: 0,
  source: paginaFuente.php,
  focus: function( event, ui ) {
    $( "#project" ).val( ui.item.label );
    return false;
  },
  select: function( event, ui ) {
     //GENERAR UN SOLO ARRAY, CON LOS VALORES QUE VIENEN DE LA BASE DE DATOS
    var elems = [];
    elems.push(ui.item.label);
    elems.push(ui.item.value);
    elems.push(ui.item.picsrc);

    //GUARDAR EN CAMPO HIDDEN TODO; HECHO JSON usando JSON.stringify
    $('#input_hidden_field').val(JSON.stringify(elems)); //store array

    var value = $('#input_hidden_field').val(); //retrieve array
    value = JSON.parse(value);

    return false;
  }
})
.autocomplete( "instance" )._renderItem = function( ul, item ) {
  return $( "<li>" )
    .append( "<div>" + item.label + "<br>" + item.desc + "</div>" )
    .appendTo( ul );
};
} );
</script>

//LUEGO EN ALGUNA PARTE DEL BODY
<form id="test"  action="otherPhpPage.php">
<input type="hidden" id="input_hidden_field" value="" />
</form>

I hope this answers your question.

    
answered by 16.01.2017 в 17:43