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>';
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧