How to make a select with the option correctly? - php

What is the best way to return exactly the select option that the end user chose?
In my query for example when selecting the imoveis_tipo
Returns me all the data being HOUSES, APARTMENTS, TERRENOS;
How do I return only 1 purpose type?
If my question was not very clear, please comment here so I can adjust my question.
Here's an example showing what I'm trying to do, by selecting PURPOSE as SALE
PROPERTY TYPE as 2 BEDROOM APARTMENT
And the CITY as CAPÃO DA CANOA
Returns me all the records in the database and not just the ones I selected in the search.
This is my code showing the purpose, type and city:
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Finalidade</label>
<select id="finalidade" name="finalidade" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql5 = mysqli_query($link, "SELECT * FROM imoveis_finalidades WHERE status='S' ORDER BY nome");
while ($dados5 = mysqli_fetch_array($sql5)) {
?>
<option value='<?php echo $dados5['url'] ?>'>
<?php echo ($dados5['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Tipo de Imóvel</label>
<select id="tipo" name="tipo" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql5 = mysqli_query($link, "SELECT * FROM imoveis_tipos WHERE status='S' ORDER BY nome ");
while ($dados5 = mysqli_fetch_array($sql5)) {
?>
<option value='<?php echo $dados5['url'] ?>'>
<?php echo ($dados5['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Cidade</label>
<select id="cidade" name="cidade" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql6 = mysqli_query($link, "SELECT * FROM localidades_cidades WHERE status='S' ORDER BY nome");// or die (mysqli_error($link));
while ($dados6 = mysqli_fetch_array($sql6)) {
?>
<option value='<?php echo $dados6['url'] ?>'>
<?php echo ($dados6['nome']); ?>
</option>
<?php
}
?>
</select>
</div>

Related

Repetitive form control

Vs code
Output
<div class="form-group">
<label class="control-label col-lg-4" for="username">Menu</label>
<div class="col-lg-8">
<select class="form-control select2 " id="exampleSelect1" name="menu[]" multiple='multiple' style="width: 100%;height:200px" placeholder="Select multiple members">
<?php
$m = mysqli_query($con,"SELECT * FROM combo_details natural join menu where combo_id='$id'");
while ($rowm = mysqli_fetch_assoc($m)){
?>
<option selected value="<?php echo $rowm['menu_id'];?>"><?php echo $rowm['menu_name'];?></option>
<?php
}
?>
<?php
$result = mysqli_query($con,"SELECT * FROM menu");
while ($row = mysqli_fetch_assoc($result)){
?>
<option value="<?php echo $row['menu_id'];?>"><?php echo $row['menu_name'];?></option>
<?php
}
?>
</select>
</div>
</div>
tried changing selected value and i even tried to delete it and merge it but it doesnt work. need help

How the proper way to explode array from database make it as option in dropdown in Codeigniter

i have array, and want to explode the array as option in dropdown, here is the table
id value option
K1 1,2,3 AAA,BBB,CCC
K2 1,2,3 DDD,EEE,FFF
i want the output like this
<div class="form-group">
<label for="val[]">Parameter K1</label>';
<select class="form-control" name="val[]">
<option value="" selected="" disabled="" hidden="">Choose one</option>
<option value="1">AAA</option>
<option value="2">BBB</option>
<option value="3">CCC</option>
</select>
</div>
<div class="form-group">
<label for="val[]">Parameter K2</label>';
<select class="form-control" name="val[]">
<option value="" selected="" disabled="" hidden="">Choose one</option>
<option value="1">DDD</option>
<option value="2">EEE</option>
<option value="3">FFF</option>
</select>
</div>
If I understood correctly, you want to suppress the data in a table with php in this way.
I wrote a mysqli query for you, it's $query variable.
$query = mysqli_query($db_connect,"SELECT * FROM tbl_variables");
Then I wrote this query with the while loop with the help of mysqli_fetch_object and read it line by line.
<?php
while ($row = mysqli_fetch_object($query)){
echo '
<option value="'?><?php echo $row->optionValue; ?><?php echo'">'?><?php echo $row->optionText; ?><?php echo'</option>
';
}
?>
All code parts;
<?php
$query = mysqli_query($db_connect,"SELECT * FROM tbl_variables");
?>
<div class="form-group">
<label for="val[]">Parameter K1</label>';
<select class="form-control" name="val[]">
<option value="" selected="" disabled="" hidden="">Choose one</option>
<?php
while ($row = mysqli_fetch_object($query)){
echo '
<option value="'?><?php echo $row->optionValue; ?><?php echo'">'?><?php echo $row->optionText; ?><?php echo'</option>
';
}
?>
</select>
</div>

How to show selected value from database and show in dropdownlist using codeigniter and bootstrap

When I select the all data of a product by it's product_id. and load product data in a form for an update. but here I cannot set selected drop-down list value in this case.
here, is my view
<form action="<?php //echo base_url().'admin_product/pro_update/'?>" method="post" class="form-horizontal">
<div class="control-group">
<label class="control-label">Title :</label>
<div class="controls">
<input type="text" class="span5 m-wrap" placeholder="Product title" name="title" value="<?php echo $item[0]['p_title']?>" />
<h6 style="color:red"><?php echo form_error('title')?></h6>
</div>
</div>
<div class="control-group">
<label class="control-label">Category :</label>
<div class="controls">
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>"><?php echo $c->cat_name;?>//selected set value here</option>
<?php endforeach;?>
</select>
<h6 style="color:red"><?php echo form_error('cat')?></h6>
</div>
</div>
here is my controller code,
public function pro_edit($id)
{
$p['item']=$this->ap->product_update($id);
$p['cat']=$this->ap->up_cat();
$p['color']=$this->ap->up_color();
$this->load->view('layouts/header',$p);
$this->load->view('admin/product_update',$p);
$this->load->view('layouts/footer',$p);
}
and model code,
public function product_update($id)
{
$this->db->select('p_title,p_description,p_category,p_stock_quantity,p_pprice,p_sprice,p_size_variant,cat_name,v_color,v_size,p_id');
$this->db->from('tbl_product');
$this->db->join('tbl_category','tbl_product.p_category=tbl_category.cat_id');
$this->db->join('tbl_variant','tbl_product.p_color_variant=tbl_variant.v_id');
$this->db->where('p_id',$id);
$query=$this->db->get();
return $query->result_array();
}
Assumptions
p_category is your category id
Code
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php
foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>" <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>>
<?php echo $c->cat_name;?>
</option>
<?php
endforeach;
?>
</select>
Explain
Q - What this if do in above <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>
A - Assume $item[0]['p_category'] has value 2 so when ever $c->cat_id equesl to that value its print selected keyword
Ex:
<select>
<option value="1">aa</option>
<option value="2" selected>bb</option>
<option value="3">cc</option>
<option value="4">dd</option>
</select>

Dependent dropdown getting ids not name

I am trying to get user selection from php form but instead of name I am getting id of elements. instead of ids I want to get names. Please help
here is the url
http://efinancec.com/d/drop4/index.php
and here is the code
<form action="../form/form1.php" method="post" enctype="multipart/form-data">
<div class="frmDronpDown">
<div class="row">
<label>Training:</label><br/>
<select name="training" id="training-list" class="demoInputBox" onChange="getCourse(this.value);">
<option value="">Select Training</option>
<?php
foreach($results as $training) {
?>
<option value="<?php echo $training["id"]; ?>"><?php echo $training["training"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>Course:</label><br/>
<select name="course" id="course-list" class="demoInputBox" onChange="getCountry(this.value);">
<option value="">Select Course</option>
</select>
</div>
<div class="row">
<label>Country:</label><br/>
<select name="country" id="country-list" class="demoInputBox" onChange="getCity(this.value);">
<option value="">Select Country</option>
</select>
</div>
<div class="row">
<label>City:</label><br/>
<select name="city" id="city-list" class="demoInputBox" onChange="getDates(this.value);">
<option value="">Select City</option>
</select>
</div>
<div class="row">
<label>Dates:</label><br/>
<select name="dates" id="dates-list" class="demoInputBox" onChange="getPrice(this.value);">
<option value="">Select Dates</option>
</select>
</div>
<div class="row">
<label>Online Onsite Price:</label><br/>
<select name="price" id="price-list" class="demoInputBox">
<option value="">Select Online or Onsite</option>
</select>
</div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
and this is how I am getting the values from sql database
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["training_id"])) {
$query ="SELECT * FROM course WHERE training_id = '" . $_POST["training_id"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select Course</option>
<?php
foreach($results as $course) {
?>
<option value="<?php echo $course["id"]; ?>"><?php echo $course["course"]; ?></option>
<?php
}
}
?>
Change this line:
onChange="getCourse(this.value);">
To this :
onChange="getCourse(this.text);">
You are passing id of the course as the value of dropdown and calling that value which is fine but you need to call the text of select option to get the course name.
I changed this.value to this.text as suggested and created the new page and it stoped working at all now it is not pulling the records from database except the first one. Here is the link
http://efinancec.com/d/drop4/index1.php
where as my earlier page is still working fine.
http://efinancec.com/d/drop4/index.php
I think I could not convey my problem earlier. My depended dropdown is working fine but when a user submits the form in the email I receive the ids instead of name. Thats the problem.
For example I myself made some selections on index.php and submitted form Here is the email I got
training: 1
course : 101
country: 1001
city: 5005
Basically I am getting the the respective ids instead of name.

Dependent dropdown

i have a problem in dependent dropdown and i am not professional in this issue
so the problem is when i choose the first one the another one doesnt get any value from database and i didnt know the solution
<?php
include 'header.php';
require 'connection.php';
?>
<div class="container">
<form id="contact" action="add_line.php" method="post">
<center> <h3>Add Line</h3></center>
<fieldset>
<?php
require 'connection.php';
$query = "select * from agents";
$result = mysqli_query($conn,$query);
?>
<div class="select">
<select class="agents-name " name="agents-name" autofocus tabindex="1">
<option selected="selected">--Select agent--</option>
<?php while ($row = mysqli_fetch_assoc($result)): ?>
<option value="<?php $row['id'];?>"><?php echo $row['name'];?></option>
<?php endwhile;?>
</select>
</div>
<div class="select" >
<select tabindex="1" name="sp_choosen" class="sp_choosen"
onChange="getState( this.value );" tabindex="2">
<option selected="selected">--Select service provider--</option>
<option value="CELLCOM">CELLCOM</option>
<option value="HoTMobile">HoTMobile</option>
<option value="Orange">Orange</option>
<option value="Pelphone">Pelphone</option>
<option value="Golan">Golan</option>
<option value="019">019</option>
</select>
</div>
<div class="select">
<select id="packet_select" name="packet_chossen" tabindex="3">
<option selected="selected">--Select packet--</option>
</select>
</div>
</fieldset>
<fieldset>
<input placeholder="customer name" type="text" tabindex="4"
name="customer_name" required >
</fieldset>
<fieldset>
<input placeholder="SIM_SERIAL" type="tel" tabindex="5" name="sim_serial"
required >
</fieldset>
<fieldset>
<input placeholder="phone_number" type="tel" tabindex="6" name="number"
required >
</fieldset>
<fieldset>
<label></label>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" tabindex="7" >Add
Available line</button>
</fieldset>
</form>
</div>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "dropdown_add_line.php",
data:'sp='+val,
success: function(data){
$("#packet_select").html(data);
}
});
}
</script>
<?php
include 'footer.php';
?>
and this is the dropdown_add_line.php :
<?php
require_once("connectione.php");
$db_handle = new DBController();
if(!empty($_POST["sp"])) {
$sp=$_POST['sp'];
$query ="SELECT * FROM packets p WHERE sp LIKE '%$sp%'";
$results = $db_handle->runQuery($query);
?>
<option value="">--Select service provider--</option>
<?php
foreach($results as $packets) {
?>
<option value= "<?php echo $packets["id"]; ?>" ><?php echo $packets["packet"]; ?></option>
<?php
}
}
?>
and the table name is "packets" and the columns is "id","sp","packet"

Categories