Selection list populated from database - php

I have been having a problem trying to figure out how to create a selection list which is populated with data from a table in a database, namely with the options being customer's last names.
Here is what i have tried:
I am trying to make the selection list access the table "customer" and fields "customerID"
Any help would be greatly appreciated, if any more information is needed just ask.

You are messing in quotes. Try below.
$options .= "<option value='" . $id ."'>" . $name ."</option>";
And than use it like,
<select>
<option value=0>Choose</option>
<?php echo $options; ?>
</select>

"<OPTION VALUE=\"$id\">$name </option>";
This will prevent names, such as O'mally, from accidental truncation.

<?php
while ($row=mysqli_fetch_array($rs)) {
$id=$row["customerID"]
$name=$row["lastName"]
$options.="<OPTION VALUE=\"$id"\">".$name."</OPTION>;
}
?>
<p>Select a customer's ID to view information on</p>
<select>
<option value=0>Choose</option>
<?=$options?>
</select>

Change this:
$options.="<OPTION VALUE=\"$id"\">".$name;
to this:
$options.="<OPTION VALUE='" . $id. "'>" . $name . "</option>";
And if you have an error, tell us what error it is?

Related

How to make the Html dropdown list selected value to be printed as a CSV?

I have this html code and I want when I select name from table category with export.php to export all the data only for this category name.Can you help me please?
<select>
<option disabled selected></option>
<?php
include "config.php"; //
$products_cat = mysqli_query($con, "SELECT name From category");
while ($product_categoty = mysqli_fetch_array($products_cat)) {
echo "<option value='" . $product_categoty['name'] . "'>" . $product_categoty['name'] . "</option>";
}
?>
</select>
<form method="post" action="export.php">
<input type="submit" name="expo" value="Export Category">
</form>
</form>```
I think Select should be inside the form and please follow these steps to export data as csv.
https://www.codexworld.com/export-data-to-csv-file-using-php-mysql/

for in combination with html forms

I want to provide different options in a html form, with a php for loop, but the loop gets all the time stuck. Thanks in advance for helping me out.
<select name="position">
<?php
for($i=1;$i<10;$i++) {
echo "<option value= . $i >$i</option>";
}
?>
if you are use double quotes can without . Operator
<select name="position">
<?php
for($i=1;$i<10;$i++) {
echo "<option value= ' $i '>$i</option>";
}
?>
</select>
You look like you are missing the quotes in the value tag.
echo "<option value= \"$i\" >$i</option>";

Based on a condition, how to make a dropdown list single select or multiselect?

I had a requirement where in an employee list dropdown given,
if selectMultiple is set, the dropdown should allow multiple selects, and if selectMultiple is not set, it shouldn't.
<select name="employeeList[]" id="employeeList" class="form-
control" multiple="<?=$selectMultiple?>">
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
</select>
It's a phtml file, and $selectMultiple is passed from a controller(as in Phalcon). I have tried something like passing, $selectMultiple ="multiple", so the code will look like
<select name="employeeList[]" id="employeeList" class="form-control" multiple="multiple">
and $selectMultiple="" for the single select case.
<select name="employeeList[]" id="employeeList" class="form-control" multiple="">
But the very presence of multiple attribute itself makes the dropdown list elligible for multiselect.
In short, in either cases, it triggers multiselect regardless of the condition. Please help.
You can simply write your code like
You need to pass $selectMultiple = "multiple" or $selectMultiple = ''
<select name="employeeList[]" id="employeeList" class="form-control" <? echo !empty($selectMultiple) ? $selectMultiple : '' ?>>
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
I tried introducing a new variable "chooseId" to get rid of the multiple attribute and hence worked!
Modified the select tag to,
<select name="employeeList[]" id="employeeList" class="form-control"
<?php if ($chooseId== 1) { ?> multiple="multiple" <?php } ?>>
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
</select>
Now the code recognises the multiple attribute and enables multiselect only when chooseId is 1, otherwise single select works normally.

HTML Options from SQL Database

I have a HTML DataList input box, and I would like to have the options in the DataList to be things from my SQL database. I have been trying to make this work, but for some reason I cant. Does anyone know how this could be achieved?
Here is my code:
<?php
include_once 'connect.php';
$sql="select * FROM table";
?>
<form>
<input list="list" name="name">
<datalist id="datalist">
<?php
foreach ($dbo->query($sql) as $row) {
echo "<option value="$row[name]"/>";
}
?>
</datalist>
<input type="submit">
</form>
The option I get with this is literally $row[name]
Could someone tell me what I'm doing wrong?
Datalist id has to match the input list attribute and change the loop to "<option value=" . $row['name'] . "/>"

Hide options in a select list using jQuery

How can I hide room numbers that is not at Hotel1 and send chosen room number to next page when you click on submit button. As it is now I will be forced to send only the id for chosen hotel because i need to have the value for selHotel options and selroom the same.
I'm using php, html and Sqlite database. hotelrows and roomrows contains all rows of hotel table and rooms table. Can anyone give me an idea of ​​how I could get this to work? Ie when choosing Hotel1 I can just choose room 101 or 102 in selroom for example. (Sorry if my English is bad, hopefully someone understands what I'm asking for.)
Hotels:
id.....Hotel
1......Hotel1
2......Hotel2
Rooms:
hid....Room#
1.......101
1.......102
2.......301
My php/html:
<select id="selHotel" name="selH">
<option value="default" selected> Select hotel</option>
<?php
foreach($hotelrows as $row)
{
echo "<option value='". $row['id'] . "'>". $row['hotelname'] . "</option>";
}
?>
</select>
<select id="selroom" name="selr">
<option value="default" selected>Select room </option>
<?php
foreach($roomrows as $row)
{
echo "<option value='". $row['hid'] . "'>". $row['roomnum'] . "</option>";
}
?>
</select>
jQuery to hide Hotel2 Rooms then i choose Hotel1:
$("#selHotel").change(function(){
var hotelVal = $(this).find("option:selected").val();
$('#selroom option').hide();
$('#selroom option[value='+hotelVal+']').show();
});
Use the roomnum as value, add your hid as class, then use it as filter when selected:
php:
echo "<option class='cls_". $row['hid'] . "' vlaue='".$row['roomnum']."'>". $row['roomnum'] . "</option>";
page
$("#selHotel").change(function(){
var hotelVal = $(this).find("option:selected").val();
$('#selroom option').hide();
$('#selroom .cls_'+hotelVal).show();
$('#selroom .cls_'+hotelVal).first().attr('selected',true);
});

Categories