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.
Related
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.
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).
How do I create a dynamic select input with options from PHP whenever I click on a button?
The working should go like this:
Whenever I click on a button, a select element with options from PHP should be available to choose from.
<div class="form-group">
<label class="control-label col-sm-2" for="product">Products:</label>
<div class="col-sm-4">
<select class="form-control" name="product[]" multiple required>
<option selected="selected">--SELECT PRODUCT--</option>
<?php
$sql = "SELECT * FROM `product`";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['name']; ?>"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
</div>
</div>
Whenever I click on a button, I should be able to create a new select input with the above-mentioned code.
here you need to use ajax like below
function getOptions(query){
$.post(endPoint, query, function(response){
$('select[name="product[]"]').empty();
response.forEach(function(value){
$('select[name="product[]"]').append("<option value='" +value+ "'></option>");
});
})
}
and trigger it on click
$('body').on('click', '#myButton', function(){
getOptions(null);
});
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>
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;