i make a jquery autocomplete and its data come from mysql database.now i m facing a small problem in this.
i want to show product name in autocomplete as u see in code below & its working good.but after clicking submit button i want to insert product ID instead of product NAME.Below is my code
<?php
require_once "include/config.php";
$sql_product = "select * from tbl_product";
$res_product = mysql_query($sql_product);
?>
<script>
$(function() {
var availableTags = [
<?php
while($rs_product = mysql_fetch_array($res_product))
{
$prod_name = $rs_product['prod_name'];
?>
"<?php echo $prod_name; ?>" <?php echo ", "; ?>
<?php
}
?>
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
<label for="tags">Tags: </label>
<input id="tags" />
You can pass in your values to availableTags source in format like:
[{label:"Prod Name 1", value:1}, {label:"Prod Name2", value:2}]
Create a hidden input element, like
<input type="hidden" name="hidAutoComplete" value="" />
And:
$( "#tags" ).autocomplete({
source: availableTags,
select: function(event, ui) {
var selectedObj = ui.item;
$(this).val(selectedObj.label);
$('input[name="hidAutoComplete"]').val(selectedObj.value);
return false;
}
});
Hope it helps
TRY ( I assume there is prod_id column in your table )
var availableTags = [
<?php
while($rs_product = mysql_fetch_array($res_product))
{
$prod_id[] = $rs_product['prod_id'];
}
echo json_encode($prod_id);
?>
];
update
use json_encode
Related
code:
<script>
$(function() {
$( "#college_name" ).autocomplete({
source: 'search.php',
minLength:2,
select: function(event, ui) {
var x = ui.item.value;
$("#college_name").attr('value',x);
}
});
});
</script>
<script>
$(document).ready(function(){
$("#college_name").select(function(){
alert("hi");
});
});
</script>
html code:
<input type="text" id = "college_name" name = "college_name" value="" placeholder = "College Name" autocomplete="off">
<input type='text' name='college_id' id='college_id'>
In this code when I wrote any college name in college_name input field autocomplete search i.e. search.php are working all college are showing. But when I select any college from autocomplete it does not show any message like (hi) or any thing. So, how can I fix this problem.
Thank You
Add alert message in Select event of autocomplete.
<script>
$(function() {
$( "#college_name" ).autocomplete({
source: 'search.php',
minLength:2,
select: function(event, ui) {
var x = ui.item.value;
$("#college_name").attr('value',x);
alert("hi");
}
});
});
</script>
I am beginner in Jquery. I am using auto complete to suggest data to user in a form text box. Its working properly. But if a user entering a new entry or data in the text box.I need to add that data dynamically to the database by use of a pop up window. After that I need the id of the stored data, for storing the whole form data in other table.
Script:
<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>
Seems like you are doing it right.
Just grab the stored id from the form control.
$("#vessel_id").val();
you can as well keep it as an attribute:
<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>
In while loop $vid and $vname this value not put in "". It will not enter into your database.
while($row = mysqli_fetch_array($result))
{
$vname = $row['vessel_name'];
$vid = $row['vessel_id'];
$vname_list[] =array("key"=>$vid,"value"=>$vname);
}
I have an autocomplete text input:
<script type="text/javascript">
$(function()
{
var availableTags = <?php
$id_list = array("Name 1", "Name 2", "Name 3");
echo json_encode($id_list); ?>;
$("#FacultyName").autocomplete(
{
source: availableTags,
autoFocus:true
});
</script>
<input type="text" id="FacultyName">
Output:
I want do some task when user select on the available tag.
Is there any function like onClick or onChange to do it?
Autcomplete has a select event for this
Try like this
$("#FacultyName").autocomplete({
source: availableTags,
autoFocus: true,
select: function(event, ui) {
alert("select");
}
})
jQuery has the .change() method. Here is the API documentation to help.
https://api.jquery.com/change/
<script>
<?php
mysql_connect('127.0.0.1', 'root', '') or die(mysql_error());
mysql_select_db("Project_Part1")or die("cannot select DB");
$sqlActivity = "SELECT aname FROM Activity";
$resultActivity=mysql_query($sqlActivity);
$aname = array();
while ($row = mysql_fetch_array($resultActivity)) {
$aname[] = $row;
}
?>
$(function() {
var availableTags =[ "<?php echo implode('","',$aname);?>" ];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
Not sure why
var availableTags =[ "<?php echo implode('","',$aname);?>" ];
is not working for the autocomplete.
When I use var availableTags = [ "Rock climbing","Fishing","Kayaking","Underwater bungee jumping" ]; It is fine.
I'm new on PHP. Can anyone help me on this?
Try:
<?php
$DBi = mysqli_connect($hostname, $user, $password, $database);
$aname = array();
$sqlActivity = "SELECT `aname` FROM `Activity`";
$resultActivity = mysqli_query($DBi, $sqlActivity); //Dump mysql_query for
//mysqli_query and don't
//forget the connection bit
while ($row = mysqli_fetch_array($resultActivity)) {
$aname[] = $row;
};
?>
<script>
$(function() {
var availableTags =[ "<?php echo implode('","',$aname);?>" ];
$("#tags").autocomplete({
source: availableTags
});
});
</script>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
It looks like you are missing <?php right after your <script> tag. I moved the PHP SQL bit outside of your <script> tag (which should not matter but does help to keep things a little easier to read in my mind)
While you are at it, stop using mysql_ and switch over to mysqli_ functions as the former are deprecated
Here My code when I type the text box ,auto complete items listed in textbox using below code
$(function() {
var availableTags = [<?php echo $data_list;?>];
$( "#ethnicity" ).autocomplete({
source: availableTags
})
});
<input type="text" size="20" id="ethnicity" name="ethnicity" >
how to list all the list when focus or click the text box