dongzituo5530 2017-04-11 07:30
浏览 41
已采纳

如何使用精选的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.

  • 写回答

1条回答 默认 最新

  • douzhonglong3789 2017-04-11 12:24
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度