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

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.

Related

How to disable input in datalist?

I want to block the user to input I just want him to choose only from the list
I tried disable and readonly but this will disable my list and I don't want that.
This is my code
<div class="form-group row justify-content-center" id=" input-champs " style="margin-left:;" ><!-- grossistee -->
<label for="grossiste" class="col-2 col-form-label"></label>
<div class="col-3">
<input type="text" list="list-gro" placeholder="Grossiste1" id="g_name1" name="g_name1" class="form-control">
<datalist id="list-gro">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</datalist>
</div>
From the living standard spec of the datalist element:
Each option element that is a descendant of the datalist element, that is not disabled, and whose value is a string that isn't the empty string, represents a suggestion. Each suggestion has a value and a label.
Conversely, this means that a user can enter anything he wants. The datalist element only contains suggestions that the user can use, but does not have to.
For your puprose I 'd suggest a select element. A select element dictates strict values for the user to use.
<label for="my-select">My Select</label>
<select name="my-select" id="my-select" required>
<option value="" disabled selected>Select your option</option>
<?php foreach ($databaseresult as $row): ?>
<option value="<?= htmlspecialchars($row['value']) ?>">
<?= htmlspecialchars($row['label']) ?>
</option>
<?php endforeach ?>
</select>
Its better to use select in place of input as you dont want the user to type in anything from himself/herself.
You can do like this to implement select with database query
<select name="g_name1" id="g_name1" class="form-control">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</select>

Setting a select option default to a previously set option

In my modal window, I have a select where a user can select a breakfast meal from my database. By selecting a meal and submitting it, a record is saved in the database. However, I would like to achieve that when the user enters into the same modal window, whatever meal they have selected before hand will appear as the default option. I have created a query that checks to see if a meal has already been selected (optionquery1). Any ideas to achieve this? Thanks
On a side note, I'm not sure that I'm setting the date php variable currently. The day, month and year is stored in the html hidden inputs but not sure how to extract the values.
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<div class="modal-body">
<form action="my_planner.php" method="post">
<!--<span class="test5"></span>-->
<!--<span class="test"><input type="text" name="id_disabled" value="" disabled/>-->
<input class="test" type="text" name="id1" value="" style="text-align:center; border:0px; font-weight:bold; font-size: 25px;"/>
<hr class="my-4">
<input type="hidden" name="id" value=""/>
<input type="hidden" name="month" value=""/>
<input type="hidden" name="year" value=""/>
<br>
<p id="breakfastTag"><br>Breakfast:</p>
<select id="breakfast" name="breakfast">
<option value="" disabled selected>Select your breakast</option>
<?
$meal='breakfast';
$date='<input type="hidden" name="id" value=""/>'.'<input type="hidden" name="month" value=""/>'.'<input type="hidden" name="year" value=""/>';
$query = $db->prepare("select * from recipe_category where categoryID=1");
$query->execute();
while ($results = $query->fetch()) {
$recipeID=$results["recipeID"];
$query3 = $db->prepare("select * from recipe where id=:recipeID");
$dbParams3=array('recipeID'=>$recipeID);
$query3->execute($dbParams3);
$breakfast = $query3->fetchAll();
$optionquery = $db->prepare("select * from user_planner where userID=:userID and date=:date and meal=:meal");
$optionParams= array ('userID'=>$thisUser, 'date'=>$date,'meal'=>$meal);
$optionquery->execute($optionParams);
while ($results12 = $optionquery->fetch()) {
$selectedmeal = $results12['recipeID'];
}
$optionquery1 = $db->prepare("select * from recipe where id=:recipeID");
$optionParams1= array ('recipeID'=>$selectedmeal);
$optionquery1->execute($optionParams1);
while ($results123 = $optionquery1->fetch()) {
$selectedrecipe = $results123['name'];
}
foreach($breakfast as $breakfast): ?>
<option value="<?= $breakfast['id']; ?>"><?= $breakfast['name']; ?></option>
<?php endforeach;} ?>
</select>
Of course you can, you need to use the selected attribute on the <option> you want to show.
First remove the selected attribute from the first <option>:
<option value="" disabled>Select your breakast</option>
Then in the foreach loop you need an if condition:
$results123 = $optionquery1->fetch();
$selectedrecipe = $results123['id'];
foreach($breakfast as $breakfast): ?>
<option value="<?= $breakfast['id']; ?>" <?php if ($breakfast['id'] == $selectedrecipe) echo 'selected' ?>><?= $breakfast['name']; ?></option>
<?php endforeach;} ?>
Notice I have removed the while loop since we are sure Mysql will return only one record (WHERE ID = <ID>).

isset() does not work properly by using include in PHP

I am a very beginner in programming PHP.
I have the following HTML code and two PHP code "include". I am not sure why ISSET() not firing at all.
If I remove the isset(){} it brings up the data perfectly fine...
but when I try with isset() it shows no data. Can anyone help?
<form action="functions/pos-getPrice.php"class="form-horizontal"
method="post">
<div class="form-group">
<label class="col-md-4 control-label">Item</label>
<div class="col-md-6 selectContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i>
</span>
<select name="item" id="item" class="form-control selectpicker" >
<option>Please select your item</option>
<?php include "functions/pos-getItems.php" ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Price</label>
<div class="col-md-6 selectContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-list"></i>
</span>
<select name="price" class="form-control selectpicker" >
<option value="">Please select your price</option>
<?php include "functions/pos-getPrice.php" ?>
</select>
</div>
</div>
</form>
<?php
if(isset($_POST['item']) && $_POST['item'] == 'jacket') {
include "db-Info.php";
$res = mysqli_query($con, "select id, price, pointRequired from
tblPrice");
while($row = mysqli_fetch_array($res))
{
$price = $row['price'];
$pointRequired = $row['pointRequired'];
echo "<option value=\"".$row['id']."\">";
echo "Price: $price" ?> <?php echo "Point: $pointRequired";
echo "</option>";
}
}
?>
<?php
include "db-Info.php";
$res = mysqli_query($con, "select distinct item from tblPrice");
while($row = mysqli_fetch_array($res)){
$array = $row['item'];
echo "<option value=\"".$row['item']."\">";
echo $array;
echo "</option>";
}
?>
From what I can tell, I'm assuming your item list is loading correctly, and your price list is not? Are you expecting the price list to change when a user selects in item?
In this instance, there is no $_POST data when you load your page containing the form. Selecting an item in your dropdown does not send any kind of POST request causing the prices to change. Thus, at the time of page load, your price script is checking to see if $_POST["item"] is set and if that item is "jacket," which it is not at the time of page load. So no prices load, and that's that; the script doesn't keep checking what the item is after that.
If you're expecting prices to dynamically load based on the selected item, you'll either need to have an AJAX call that fetches the price options every time an item is selected, or you can preload all the price data upfront when the page loads, then use javascript to populate the price options on change of the item selector.

PHP echo in selection list with multiple selection options

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>

change of drop down in php my sql Dynamically

I have two drop downs .one have static value and second get values from db. I want to that if value is selected from 1st drop down then relevant values loaded in 2nd drop down. I have tried. but its load all the data from database according to user.for example when user select from request type dropdown having value inquiry.then 2nd drop down load only the values which have catType Inquiry.and if he select the complaint then complaint data must be shown.I have been tried but all the data is loaded ,or only one data is loading.any body help me in this regard.Thanks in Advance. Here is My Code
<div class="col-md-4">
<div class="form-group">
<label for="requesttype"><?php echo $requestField; ?></label>
<select class="form-control" required="" id="requesttype" name="requesttype" onchange="fcrActionChange(this);">
<option value="">Select Request Type</option>
<option value="Inquiry">Inquiry</option>
<option value="Complaint">Complaint</option>
<option value="Service Request/FCR">Service Request/FCR</option>
<option value="Verification Call">Verification Call</option>
</select>
<span class="help-block"><?php echo $requestHelp; ?></span>
</div>
</div>
$("#requesttype").change(function() {
$("#catId).load("navigation.php?requesttype=" + $("#requesttype").val());
});
</script>
<div class="col-md-4">
<div class="form-group">
<label for="catId"><?php echo $categoryField; ?></label>
<select class="form-control" name="catId" id="catId">
$tcat = "SELECT catId, catName FROM categories WHERE userId = ".$userId." AND isActive = 1 AND catType = ".$_GET['requesttype'];
$rest = mysqli_query($mysqli, $tcat) or die('-2'.mysqli_error());
while ($tcatrow = mysqli_fetch_assoc($rest)) {
echo "<option value="$tcatrow['catId'] >";
echo clean($tcatrow['catName'])."</option>";
}
</select>
<span class="help-block"><?php echo $categoryHelp; ?></span>
</div>
</div>
</div>
Your code is pretty confusing but anyway, the important part is within your ajax request and your PHP file
Ex. you have a div id secondOpt to be filled from the query made by the requesttype.
**Note I added a userId input field to pass the value for the SQL query in the navigation.php
<select class="form-control" required="" id="requesttype" name="requesttype" onchange="fcrActionChange(this);">
<option value="">Select Request Type</option>
<option value="Inquiry">Inquiry</option>
<option value="Complaint">Complaint</option>
<option value="Service Request/FCR">Service Request/FCR</option>
<option value="Verification Call">Verification Call</option>
</select>
<input type="hidden" id="userId" value="<?php echo $userId;?>">
<div id="secondOpt"></div>
After this, you will have an ajax request below, you can use $.post from jQuery and render the returned data on the secondOpt element
$('#requesttype').change(function(){
$.post("navigation.php",{requesttype: $(this).val(),userId: $('#userId').val()},function(options)
{
$('#secondOpt').html(options);
});
});
And for the navigation.php UPDATED
//don't forget your config file here to connect with the database
$userId = mysql_real_escape_string($_POST['userId']);
$requesttype = mysql_real_escape_string($_POST['requesttype']);
$output = "<select id='catId'>";
$tcat = "SELECT catId, catName FROM categories WHERE userId = ".$userId." AND isActive = 1 AND catType = ".$requesttype;
$rest = mysqli_query($mysqli, $tcat) or die('-2'.mysqli_error());
while ($tcatrow = mysqli_fetch_assoc($rest)) {
$output.="<option value=".$tcatrow['catId']." >";
$output.=clean($tcatrow['catName'])."</option>";
}
$output.="</select>";
echo $output;

Categories