doucai9270 2015-11-19 09:12
浏览 53

如何将主键ID而不是属性值传递到数据库中

I have a input box where the user enters in a zip code. I created an auto-suggest function that recommends zip codes based on the ones i have store in my database. Right now I linked it so the actual value of the zip code rather than the primary key. I am not sure how I can pass my primary key into my PHP file rather than the actual value.

HTML:

<div class="label">Zip Code:</div><br>
    <input type="text" id = "zip_id" class="tftextinput2" autocomplete = "off" name="zip" placeholder="Please enter your Zip Code" onkeyup = "autocomplete()">
    <ul id = "zip_codes_list_id"></ul>

JS:

function autocomplete() 
{
    var min_length = 1; 
    var keyword = $('#zip_id').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'ajax_refresh.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#zip_codes_list_id').show();
                $('#zip_codes_list_id').html(data);
            }
        });
    } else {
        $('#zip_codes_list_id').hide();
    }
}
function set_item(item) 
{
    // change input value
    $('#zip_id').val(item);
    // hide proposition list
    $('#zip_codes_list_id').hide();
}

PHP:

<?php
    //connect to db here
    $keyword = '%'.$_POST['keyword'].'%';
    $sql = "SELECT * FROM zip_codes WHERE zip LIKE (:keyword) ORDER BY zip_codes_id ASC LIMIT 0, 10";
    $query = $pdo->prepare($sql);
    $query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
    $query->execute();
    $list = $query->fetchAll();
    foreach ($list as $rs) 
    {
        // put in bold the written text
        $zip = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['zip']);
        // add new option
        echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['zip']).'\')">'.$zip.'</li>';
    }
?>

From here I could do a POST request in my php file and i would store the actual zip code that the user entered. I however want to store the primary key associated with that zip code.

  • 写回答

1条回答 默认 最新

  • duanfu3884 2015-11-19 09:18
    关注

    Try this:

    foreach ($list as $rs) 
            {
                // put in bold the written text
                $zip = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['zip']);
                // add new option
                echo '<li " onclick="set_item(\''.str_replace("'", "\'", $rs['primary_key_column_name']).'\')">'.$zip.'</li>';
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算