当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - passing primary key instead of attribute on submit

问题描述:

I have an input tag that takes a users input that calls an AJAX dynamically outputs suggestions from my database. The issue is I want to store the primary key associated with that attribute.

I have figured out a way set it to the primary key when the user selects a value; however I would rather only have the attribute displayed on the front end. Essentially what I was thinking about doing was using the option tag and setting the value to the primary key, but after reading the documentation for it, that doesnt look like it would work.

HTML:

 <input type="text" id = "zip_id" class="tftextinput2" autocomplete = "off" name="zip" placeholder="Zip Code" onkeyup = "autocompleter()">

<ul id = "zip_codes_list_id"></ul>

JS:

function autocompleter()

{

var min_length = 1; // min caracters to display the autocomplete

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();

}

}

// set_item : this function will be executed when we select an item

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>'; (this one only passes the attribute)

echo '<li " onclick="set_item(\''.str_replace("'", "\'", $rs['zip_codes_id']).'\')">'.$zip.'</li>';

//this one passes the attribute but changes the displayed value to the primary key.

}

?>

As you can see from the PHP file, what I am trying to do is pass in the primary key value but keep the displayed value the attribute. I am not sure how to do that. Should I be using the UL tag?

网友答案:

The issue in your code is that you try to the zip_id value for the input, but this input contains the zip field value - I assume it's the textual representation. There are a few ways how you could save the zip_id on the frontend - either store it in the model (if you're using some MVC framework, but I gues it's not the case) or simply add a hidden input field:

<input type="hidden" id="actual_zip_id" name="zip_id">

And

function set_item(item) 
{
    // change input value
    $('#actual_zip_id').val(item);
    // hide proposition list
    $('#zip_codes_list_id').hide();
}

UPD

Speakng about the entire idea of autocompleting zip codes, it looks pretty nasty, as pointed by Darren Gourley (check the comments).

So you'd rather validate it with regex first, and then do your db-related logic like that:

$('#zip_id').on('change', function(){
  // your stuff
})

Best regards, Alexander

分享给朋友:
您可能感兴趣的文章:
随机阅读: