如何使用精选的AJAX完成表单?

I'm trying to complete a form from a database MySQL with AJAX.

For the moment I have this:

Script

function showService(str)
{
    $(function() {
        $("#service").autocomplete({
            source: "getservice.php",
            select: function(event, ui) {
                event.preventDefault();
                $('#Nazwa').val(ui.item.Nazwa);
                $('#DATE').val(ui.item.DATE);
                $('#Lczba').val(ui.item.Lczba);
                $('#Laureaci').val(ui.item.Laureaci);
             }
        });
    });

This is the select:

<select  class="checkonkursu" name="konkursu" onChange="showService(this.value)">
    <option value="">Seleccione un konkursu</option>
    <?php
        $query = mysql_query("SELECT Nazwa FROM  konkursu ORDER BY Nazwa asc", $conexion);

        while($row = mysql_fetch_array($query)) {
            echo '<option value="'.$row['Id_konkursu'].'">'.$row['Nazwa'].'</option>';
        }
    ?>
</select>

And here is the code for call to database:

<?php
    if (isset($_GET['term'])){
        # conectare la base de datos
        $con=@mysqli_connect("localhost", "user", "password", "Namedatabae");

    $return_arr = array();
    /* Si la conexión a la base de datos , ejecuta instrucción SQL. */
    if ($con)
    {
        $fetch = mysqli_query($con,"SELECT * FROM konkursu);
        /* Recuperar y almacenar en conjunto los resultados de la consulta.*/
        while ($row = mysqli_fetch_array($fetch)) {

            $row_array['Nazwa']=$row['Nazwa'];
            $row_array['Lczba']=$row['Lczba'];
            $row_array['DATE']=$row['DATE'];
            $row_array['Laureaci']=$row['Laureaci'];

            array_push($return_arr,$row_array);
        }
    }

    /* Cierra la conexión. */
    mysqli_close($con);

    /* Codifica el resultado del array en JSON. */
    echo json_encode($return_arr);

    }
?>

For sure it is so bad code but I am learning and sorry for it.

If I understand your question correctly. You are simply wanting to fill a HTML select with the value being the ID of records from your database, and the name being the corresponding name.

The jQuery showService function seems redundant here. You can set the value of the select in your echo of the option, which you have attempted to do. However there is an error in

$query = mysql_query("SELECT Nazwa FROM  konkursu ORDER BY Nazwa asc", $conexion);

as Manchary pointed out this should be mysqli_query (as mysql is deprecated). But also you have your arguments mixed up so the line should be :

$query = mysqli_query($con, "SELECT Nazwa FROM konkursu ORDER BY Nazwa ASC")

It common practice to rewrite this as

$sql = "SELECT Nazwa FROM konkursu ORDER BY Nazwa ASC";
$qry = mysqli_query($con, $sql) or die(mysqli_error($con));

So in the event your query should fail, you will receive a (not always useful) error message. But something to point you to the problem at least.

I have written the select with general english language in a hope it is still useful to you while also helping others.

<select  class="foo" name="selector">
<option value="#">Select an Option</option>
<?php

    $con= mysqli_connect("localhost", "user", "password", "db");
    $qry = "SELECT * FROM Table";
    $res = mysqli_query($con, $qry) or die(mysqli_error($con));
    while($row = mysqli_fetch_assoc($res)){
        echo "<option value='".$row['Id']."'>".$row['Name']."</option>";
    }
?>
</select>