I have made a autocomplete control as given below.
var PrimaryPhy = <?php include('getdata.php'); ?>;
$("#PatientID").autocomplete({
source: PrimaryPhy,
autoFocus:true
});
getdata.php
<?php
$con = mysql_connect("localhost","***","***");
if(!$con){
die("Error : ".mysql_error());
}
mysql_select_db("DBname",$con);
$patient_info = array();
$result = mysql_query("select * from tablename");
while($row = mysql_fetch_array($result)){
$patient_info[] = $row['Name'];
}
echo json_encode($patient_info);
?>
Here I have brought all the Name information of all records from the table. But i wanted to do like Dropdown with option values.
For example :
<option value="1">Item 1</option>
Likewise i need to bring the primary key ID of the table as a value for that item. How can i achieve that?
And also If i place one more autocomplete control next to this control, How can i make that first autocomplete's output as a input to the second autocomplete's input?
You can accomplish it by <datalist>.
The <datalist> element, a new addition in the HTML5 specification, allows developers to create native autocomplete dropdowns for their web applications.
The <datalist> element is used to specify all of the possible values for the autocomplete list like below:
<datalist id="languages">
<option value="HTML">
<option value="CSS">
<option value="JavaScript">
</datalist>
For detailed example, you can check here
Related
I'm building a system that tracks contact lenses. I'm storing the contact lens info in a database as sometimes prices/availabilities change and i access this info from multiple points in the program. I'm trying to interface with this list using a dropdown by doing "SELECT * FROM contacts" as a query. my code looks like this :
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
Then I echo that list out in a while loop using PHP to populate the options in the dropdown.
My question is this: I have these dropdowns for each eye on the same form. So it's "Brand Right Eye"....other miscellaneous info about the right eye....then "Brand Left Eye". But ONLY the right eye is populating with the brand info because it appears first in the code. What i'm having to do is copy/paste the exact same query and do
$contact_list2 = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
then later if I need the dropdown again, I need to do $contact_list3..and so on. Why can i not generate a drop down using the same variable? Why does it stop responding to calling the variable after the first execution of it and is there any work around that I can implement that would allow me to not have to copy/paste the same query with a different variable association each time?
just for refernce, my php while code is this:
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
while($row = mysqli_fetch_array($contact_list))
{
?>
<option value = "<?php echo($row['brand'])?>" name = "brandOS">
<?php echo($row['brand']) ?>
</option>
<?php
}
?>
</select>
I have this loop copy/pasted for right eye and left eye. But it only works on which ever drop down appears first in the code.
A possible solution will be more efficient in term of performance could be :
<?php
$left_eye = '<option value="0">Please Select</option>';
$rigth_eye = '<option value="0">Please Select</option>';
while($row = mysqli_fetch_array($contact_list))
{
//logic for left eye
$left_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
//logic for rigth eye
$rigth_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
}
?>
<select class="form-control" name = "brandOS">
<?php echo $left_eye ; ?>
</select>
<select class="form-control" name = "brandOS">
<?php echo $rigth_eye ; ?>
</select>
With this solution you get your result in the same while loop. If the left and right select are the same you can use the same variable.
Store the brands in an array, then you can just loop through the array.
<?php
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
$brands = array();
while($row = mysqli_fetch_array($contact_list))
{
array_push($brands, $row['brand']);
}
?>
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
foreach($brands as $brand){
?>
<option value = "<?php echo($brand[0])?>" name = "brandOS">
<?php echo($brand[0]) ?>
</option>
<?php
}
?>
</select>
You can use a PHP array, like the SESSION one, to store values and use them across your site. Be sure you call "session_start()" method on each page you use that array, though.
//Initialize sessions
session_start();
...
//Right after getting result from query
$_SESSION['contact_list'] = $contact_list;
To use it, just be sure to call the method I told you above, and call the variable with the same syntax:
<?php
while($row = mysqli_fetch_array($_SESSION['contact_list'])) { ?>
Hope this helps.
I have two tables in my database. I have a form to add information to table news. In my form I have dropdown list with information from another table courses. I want to save the result of the selection and save it in table news. How I can do that?
code:
<p>Course</p>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("university");
$sql = "SELECT name FROM courses";
$result = mysql_query($sql);
print '<select>';
while ($row = mysql_fetch_assoc($result)) {
print '<option value='.$row['name'].'>'.$row['name'].'</option>';
}
print '</select>';
?>
First you would need to give a nameattribute to your select tag, then as soon as you post your form, you will get the value of the selected option the exact same way as you get the value of other input fields.
I would also advise to get both the id and the name of your courses from your selct query. You should then use the id as the value attribute of your option , qhile you can keep the name to display it to the user. That way you would store the id of the selected course instead of the name, meaning that if a name slightly changes, you will still have the proper course id stored.
So your final HTML for this field should look something like that :
<select name="selectedcourse">
<option value="1">Course 1</option>
<option value="2">Course 2</option>
<option value="3">Course 3</option>
</select>
And you would get the seleted value server-side in PHP using something like :
$_POST["selectedcourse"]
If, of course, your form method was POST and not GET...
I have searched quite a bit on here about this topic. But I could not find a solution for my problem. I'd appreciate it a lot if you could help me, this is for a school project I am working on.
I have a database with a table ("Main_table") and columns including "sector" and "sub_sector". I want to have two select boxes, first one will load all the records from database in "sector" column and the second one will load all the records from database in "sub_sector" column depending on the selection value of the first select box. (For example: If I select "plastics" on the first select box, then second select box should be updated with sub_sector values where sector value is equal to "plastics").
I have managed to load the options values from database for the first select box but when I click on any selection, it does not load any option to the second select box. You can find the codes below. I did not put "sector_options.php" below, as it seems to work just fine.
index.html shown below:
<script>
$(document).ready(function() {
$('#filter_sector')
.load('/php/sector_options.php'); //This part works fine - uploads options to the first select box
$('#filter_sector').change(function() {
$('#filter_subsector').load('/php/subsector_options.php?filter_sector=' + $("#filter_sector").val()
} //This part does not work - no options on the second select box
);
});
</script>
<body>
<div id="sectors"><p>Sector:</p>
<select id="filter_sector" name="select_sector" multiple="multiple" size="5"> </select>
</div>
<div id="subsectors"><p>Sub Sector:</p>
<select id="filter_subsector" name="select_subsector" multiple="multiple" size="5"> <option value="" data-filter-type="" selected="selected">
-- Make a choice --</option>
</select>
</div>
</body>
</html>
sector_options.php shown below:
<?php
$link = mysqli_connect("*******", "*******","******","********") or die (mysql_error());
$query = "SELECT sector FROM Main_table ";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {
$options .= "<option value=\"".$row['sector']."\">".$row['sector']."</option>\n ";
}
echo $options;
?>
subsector_options.php shown below:
<?php
$link = mysqli_connect("********", "*****,"*******", "********") or die (mysql_error());
$Sectors = $_REQUEST['filter_sector'];
$query = "SELECT sub_sector FROM Main_table WHERE sector='$Sectors'";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {$options .= "<option value=\"".$row['sub_sector']."\">".$row['sub_sector']."</option>\n ";
}
echo $options;
?>
For completeness, the solutions were:
Check how AJAX operations are doing using a browser network monitor
Load AJAX fetcher scripts in a browser tag - in many cases they will render quite happily there, allowing them to be more easily debugged
AJAX scripts that return HTML for injection should only return that HTML, and not a full HTML document.
hi i want to get the value the user selected from the dropdown menu without submitting a form and save it in a php variable any ideas ??
<select name="car" value="Select" size="1">
<?php
$sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$name=$row["fullname"];
$options .= '<option value="'.$name.'">'.$name.'</option>';
}
?>
<?php echo $options; ?>
</option>
</select>
I don't think you fully understand how PHP works server side, but to get the value of a dropdown menu without submitting it you'll need Javascript (jQuery makes everything easier). From there you just send an AJAX request using JSON as data format and retrieve it from PHP with json_decode.
I'm writing a jQuery mobile page (with PHP) that populates a select element and it's option tags with "items" from a table in a MySQL database (the table contains id, items, cost). Using the commonly cited mysql_query and while mysql_fetch_assoc method to echo out the options this works fine. Stripping to the bare code:
<?php
$itemQuery = mysql_query("SELECT shortDesc FROM `items` ORDER BY shortDesc");
?>
<label for="item" class="select">Item:</label>
<select name="item" id="item" data-native-menu="false">
<option>Select Item:</option>
<?php
while($temp = mysql_fetch_assoc($itemQuery))
{
echo "<option value='".$temp['shortDesc']."'>".$temp['shortDesc']."</option>";
}
?>
</select>
I'd like however to be able to update the input element below that called "cost" with the actual item's cost from the MySQL table, when the user selects an item from the list, and I'm uncertain how to do that using jQuery/PHP/MySQL. The cost input field:
<label for="cost">Cost:</label>
<input type="text" name="cost" id="cost" value="" placeholder="Cost (£)"/>
I'm also not sure if we can get the cost value somehow from the results already returned in $itemQuery (by changing the SELECT to shortDesc,cost) saving another database query, or whether we do have to query the database again to perform a select where the user's selection = shortDesc.
I suspect in different forms, this is a common requirement for developers; essentially grabbing some information from a database based on a user's selection / interaction. I have looked on Google and searched here but I am not sure if I'm using the right search terms to find what I suspect will already be answered elsewhere!
Help greatly appreciated as always.
You could do:
1) Loop through your query results and write your options and some javascript (i use an Associative Array)
<?php
$result = mysql_query("SELECT shortDesc,costs FROM items ORDER BY shortDesc");
$options = '';
$javascript = '';
while ($row = mysql_fetch_assoc($result))
{
$options .= '<option value="'.$row['shortDesc'].'">'.$row['shortDesc'].'</option>';
$javascript .= '\''.$row['shortDesc'].'\' : \''.$row['costs'].'\',';
}
?>
2) write your html and javascript:
<select id="yourselect">
<option>select</option>
<?=$options?>
</select>
<label for="cost">Cost:</label>
<input type="text" name="cost" id="cost" value="" placeholder="Cost (£)"/>
3) write some javascript to update your input field:
<script>
var costs = {<?=$javascript?>};
$(function() {
$('#yourselect').change(function() {
cost = costs[$('#yourselect').val()];
$('#cost').val(cost);
});
});
</script>