我是Jquery的初学者。我正在使用自动完成功能在表单文本框中向用户建议数据。它工作正常。但是如果用户在文本框中输入新条目或数据。我需要使用弹出窗口将该数据动态添加到数据库中。之后我需要存储数据的id,用于将整个表单数据存储在其他表中。
脚本:
<script type="text/javascript">
$(function() {
var availableTags = <?php include('vname.php'); ?>;
$("#vessel_name").autocomplete({
source: availableTags,
autoFocus:true,
minLength:2,
select: function(event, ui) {
$("#vessel_name").val(ui.item.value);
$("#vessel_id").val(ui.item.key);
}
});
});
</script>
PHP:
$connection = mysqli_connect("localhost","root","","db") or die("Error " . mysqli_error($connection));
//fetch vessel names from the vessel table
$sql = "select vessel_id,vessel_name from vessel";
$result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));
while($row = mysqli_fetch_array($result))
{
$vname = $row['vessel_name'];
$vid = $row['vessel_id'];
$vname_list[] =array("key"=>"$vid","value"=>"$vname");
}
echo json_encode($vname_list);
HTML:
<div class="form-group">
<label>Vessel Name</label>
<input type="text" id="vessel_name" placeholder="Vessel Name" class="form-control" name="nam">
</div>
<div class="form-group">
<label>Vessel Name</label>
<input type="text" id="vessel_id" placeholder="Vessel Name" class="form-control" name="nam">
</div>
答案 0 :(得分:0)
好像你做得对。 只需从表单控件中获取存储的 id 即可。
$(&#34;#vessel_id&#34)。VAL(); 你可以把它作为属性保存:
<script type="text/javascript">
$(function() {
var availableTags = <?php include('vname.php'); ?>;
$("#vessel_name").autocomplete({
source: availableTags,
autoFocus:true,
minLength:2,
select: function(event, ui) {
$("#vessel_name").val(ui.item.value);
$("#vessel_id").val(ui.item.key).attr('data-id') = ui.item.key;
}
});
});
</script>
答案 1 :(得分:0)
在while循环$ vid和$ vname中,此值未放入&#34;&#34;。它不会进入您的数据库。
dt = dt.DefaultView.ToTable();