Forums

Autocompleter: save id in the table

giusebos 03 May, 2016
Hi!
I followed this tutorial
http://www.icagenda.it/chronoforms-autocompleter-dati-da-tabella-db.html

It works great, I added a db save action to save the result, and this works well
But I would save my id field and not text
maybe there is something to be changed in the script, but I can not

then I ask for help.

thank you

<?php
        $lista_comuni = array();
        if ( !$form->data['Datacom'] || count($form->data['Datacom']) < 1 ) {
          // no result was found
          $lista_comuni[] = 'Nessun comune è presente nella lista';
        } else {
          foreach ( $form->data['Datacom'] as $d ) {
            $lista_comuni[$d['idComune']] =  $d['idComune'];
            $lista_comuni[$d['nomeComune']] =  $d['nomeComune'];
          }
        }
        foreach($lista_comuni as $un_comune){
         if(!empty($form->data['tag']) AND stripos($un_comune, $form->data['tag']) === false){
          continue;
         }
         $json[] = array('id' => $un_comune, 'text' => $un_comune);
        }
        echo json_encode($json);
        ?>
GreyHead 04 May, 2016
Hi giusebos,

Sorry, I don't understand what the problem is exactly :-( Please explain a bit more.

Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here.

Bob
giusebos 04 May, 2016
I take a complete example.
My table:
libri
autori, fields:idAutore, nomeAutore
generi, fields:idGenere, nomeGenere
editori, fields:idEditore, nomeEditore

data array

Array
(
    [option] => com_chronoforms5
    [chronoform] => alexandria_libro_autocompleter
    [event] => submit
    [idLibro] => 16
    [titoloLibro] => una mela rossa
    [ksAutore] => Giuseppe Verdi-------------------------<<this should be idAutore of the autori (authors) table
    [ksGenere] => fantasy-------------------------<<this should be idGenere of the generi (genre) table
    [ksEditore] => Mondadori-------------------------<<this should be idEditore of the editori (publishers) table
    [desLibro] => questo è un libro......
    [user_id] => 
    [state] => 1
    [salvaTitolo] => Salva
)

debug info

Array
(
    [18] => Array
        (
            [DB Save] => Array
                (
                    [Queries] => Array
                        (
                            [0] => INSERT INTO `ax_alexandria_libro` (`idLibro`, `titoloLibro`, `ksAutore`, `ksGenere`, `ksEditore`, `desLibro`, `user_id`, `state`, `created`) values ('', 'una mela rossa', 'Giuseppe Verdi', 'fantasy', 'Mondadori', 'questo è un libro......', '114', '1', '2016-05-04 16:50:51');
                        )

                )

        )

)
GreyHead 05 May, 2016
Hi guisebos,

OK - I think I understand. As far as I can see the CF Autocompleter doesn't support submitting the ID, though it could possibly be hacked to do that.

I can see two 'simpler' solutions.

a. Use some DB queries in the form On Submit event to look up the IDs from the submitted names.

b. Try using the JQuery UI AutoCompleter instead of the CF one as that appears to support having a value (the ID) and a name.

Bob
This topic is locked and no more replies can be posted.