PHP echo in selection list with multiple selection options - php

I'm having a bit of struggle putting together a selection list in HTML where the options or values are being retrieved from a table in the database. I get the following result:
As you can see this is not my intention, I rather have all options in 1 Selection List, with multiple options clickable.
This is my HTML Code (Bootstrap Framework):
<div class="container-fluid well well-lg">
<?php while ($itemsrow = $query->fetch()) : ?>
<div class="form-group">
<label for="sel1">Select list:</label>
<select class="form-control" id="sel1">
<option><?php echo $itemsrow['Beschrijving']; ?></option>
The PHP Logic:
$conn = Db::getInstance();
$query = $conn->prepare("SELECT * FROM items WHERE user_id = $userID");
$query->execute();
Thanks in advance!

Currently, you're creating new elements inside the loop so yes, you will get a new select per iteration.
You should only keep the <option>-elements inside the loop:
<div class="container-fluid well well-lg">
<div class="form-group">
<label for="sel1">Select list:</label>
<select class="form-control" id="sel1">
<?php while ($itemsrow = $query->fetch()) : ?>
<option><?php echo $itemsrow['Beschrijving']; ?></option>
<?php endwhile; ?>

You should put your loop into <select> tag
<select class="form-control" id="sel1">
<?php while ($itemsrow = $query->fetch()) : ?>
<option><?php echo $itemsrow['Beschrijving']; ?></option>

Related

How to pass two id values from one drop down with PHP

I have one dropdown with a foreach loop which will pass to model using post method.
<div class="form-group" ">
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php
foreach ($relationship as $row) {
?>
<option value="<?php echo $row->relID; ?>"><?php echo $row->relCat; ?></option>
<?php
}
?>
</select>
</div>
and in the model it is getting the proper values using post method. As
$rel = $_POST['rel'];
Here the problem is when the user select one option ,I want to get two values like
"<?php echo $row->relID; ?>" and "<?php echo $row->relCatID; ?>"
like
$rel = $_POST['rel']; //this for the $row ->relID
$relcat = $_POST['relcat'];//this for the $row ->relCatID
I want to get both from one selection without adding any visble dropdown or element..
Try bellow code with ajax call
In Javascript code get value
var name = $('#rel option:selected').attr('data-cat_id');
var id = $('#rel').val();
<div class="form-group" >
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php foreach ($relationship as $row) { ?>
<option value="<?php echo $row->relID; ?>" data-cat_id="<?php echo $row->relCatID; ?>"><?php echo $row->relCat; ?></option>
<?php } ?>
</select>
</div>

Hide id while show data from database in select option [duplicate]

This question already has answers here:
Using $_POST to get select option value from HTML
(8 answers)
Closed 1 year ago.
Look at picture above. "2" is an id of "fsdfs". I need the id to process it in backend. But in frontend, i don't want to show the id in that select option. Is there any way to make it real?
Here's my currect code:
<div class="col-xl-6 col-md-6 col-12 mb-1">
<div class="form-group">
<label for="supplier_id">Supplier</label>
<select class="form-control" name="supplier_id">
<?php
$sql = mysqli_query($con, "SELECT * FROM supplier ORDER BY id ASC");
while($supplier = mysqli_fetch_array($sql)){
?>
<option value="<?php echo $supplier['id'].' - '.$supplier['name']; ?>"><?php echo $supplier['id'].' - '.$supplier['name']; ?></option>
<?php } ?>
</select>
</div>
</div>
Use $supplier['id'] as value (which will be sent when submitting the form) and $supplier['name'] as the text content of your <option>:
<div class="col-xl-6 col-md-6 col-12 mb-1">
<div class="form-group">
<label for="supplier_id">Supplier</label>
<select class="form-control" name="supplier_id" id="supplier_id">
<?php
$sql = mysqli_query($con, "SELECT * FROM supplier ORDER BY id ASC");
while($supplier = mysqli_fetch_array($sql)){
?>
<option value="<?php echo $supplier['id']; ?>"><?php echo $supplier['name']; ?></option>
<?php } ?>
</select>
</div>
</div>
More on <option>: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
Sidenote: the for attribute of a <label> element should point to the id attribute of the related <input>. I added id="supplier_id" to your <select> to make it valid.

Multi select dropdown select all

Here is my Controller function and view of multi select dropdown.How can i include 'all' condition to select all data that are in multselect dropdown .Please Help me
Controller Function
$where2 = array('departments.isDeleted =' => 0);
$data['dept'] = $this->general_model->get('departments',$where2);
View:
<div class="form-group">
<label class="col-sm-3 control-label">Departments</label
<div class="col-sm-6">
<select name="departmentId[]" class="form-control" multiple="multiple" id="departmentId" >
<?php if(is_array($dept)){ foreach($dept as $depts){ ?>
<option value="<?php echo $depts['departmentId'];?>">
<?php echo $depts['departmentName'];?>
</option>
<?php }} ?>
</select>
</div>
</div><!--/form-group-->
Before <?php if(is_array($dept)){ foreach($dept as $depts){ ?>
add <option value="all">All</option>
Sometimes the easiest solutions are the least apparent ;)
Note: you could make it so that (with js) when the "all" option is selected the rest are deselected for better ui but it really doesn't matter. On the backend you can just have a condition checking for all as selected and disregard the rest that are selected (because you have multiple enabled).

Empty query result

in a web form there are two drop-down lists. The second list items should change dynamically depending on the value selected on the first drop-down list.
This is how am I trying to do it:
index.php:
...
<script>
function getClient(val) {
$.ajax({
type: "POST",
url: "get_contacts.php",
data:'client_id='+val,
success: function(data){
$("#contacts-list").html(data);
}
});
}
</script>
...
<div class="form-group">
<label for="mto_client" class="col-sm-2 control-label">MTO Client</label>
<div class="col-sm-10">
<select name="mto_client" id="clients_list" onChange="getClient(this.value)">
<option value="">Select a Client</option>
<?php
do {
?>
<option value="<?php echo $row_RSClients['id_client']?>" ><?php echo $row_RSClients['client_name']?></option>
<?php
} while ($row_RSClients = mysql_fetch_assoc($RSClients));
?>
</select>
</div>
</div>
<div class="form-group">
<label for="mto_client_contact" class="col-sm-2 control-label">MTO Client Contact</label>
<div class="col-sm-10">
<select name="state" id="contacts-list">
<option value="">Select Client Contact</option>
</select>
</div>
</div>
get_contacts.php
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["client_id"])) {
$query ="SELECT * FROM tb_client_contacts WHERE contact_client_id = '" . $_POST["client_id"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select Client Contact</option>
<?php
foreach($results as $state) {
?>
<option value="<?php echo $state["id_client_contact"]; ?>"><?php echo $state["contact_name"]; ?></option>
<?php
}
}
?>
There are objects on the table tb_clients_contact that meet the condition, but the second drop-down list doesn't show any objects.
Any help is welcome.
Instead of
$("#contacts-list").html(data);
It should be
$('#contacts-list').empty().append(data);
empty() will clear first the options inside the contacts-list select field, then append() will insert the options from the result of your AJAX.
You can also look at the console log for errors. If you are using Google Chrome, hit F12 to display the console log.

Populating a dropdown field using php

below code doesnot populate the list. i am trying populate a dropdown list from database but the below query doesnot populate the dropdownlist.
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label for="inputLocation" class="col-sm-2 control-label">Location</label>
<div class="col-sm-4">
<select class="form-control" id="inputLocation" name="inputLocation">
<?php
$queryData = mysql_query("SELECT DISTINCT cat_name as cat_name FROM 'categories' ORDER BY `cat_name`");
$result = mysql_fetch_array(mysql_query($queryData)); //$result now has database tables
?>
<select name='cat_name'>
<?php
while($row = mysql_fetch_array($result))
{
?>
<option values=<?php echo($row['cat_name']); ?><?php echo($row['cat_name']); ?></option>
<?php
}
?> </select>
</div>
</div>
</form>
So you seem to be calling mysql_query and fetching the array twice...
$result = mysql_fetch_array(mysql_query($queryData);
Should be changed to
$result = mysql_fetch_array($queryData);
Then further down, change
while($row = mysql_fetch_array($result))
To...
foreach($result as $row)
You can always do
print_r($result);
To ensure you are getting data back from the query.
You also have two select tags, one of which isn't closed. Remove this top one
<select class="form-control" id="inputLocation" name="inputLocation">
Couple of other points to note:
The MySQL extension is depreciated, you should look into converting this query to MySQLi or PDO
You don't need brackets around an echo statement, just use
echo $variable;
Full edited code here
http://ideone.com/GASwcB

Categories