How to get bootstrap-select drop down value in jquery? - php

I have a drop down with name="country1" where I am getting dynamic data through query inside it. Now, I want to get the value of in my alert box but it's not working right now. So, How can I do this? Please help me.
code:
<div class="btn-group bootstrap-select show-tick">
<div class="dropdown-menu open">
<div class="bs-actionsbox">
<div class="btn-group btn-group-sm btn-block">
<button type="button" class="actions-btn bs-select-all btn btn-default">Select All</button>
<button type="button" class="actions-btn bs-deselect-all btn btn-default">Deselect All</button>
</div>
</div>
<ul class="dropdown-menu inner state1" role="menu">
<?php
$this->db->select('*');
$this->db->from('states');
$this->db->where('country_id','101');
$sql = $this->db->get();
$result = $sql->result_array();
foreach($result as $row)
{
?>
<li data-original-index="0">
<a tabindex="0" data-tokens="null">
<span class="text"><?php echo $row['name']; ?></span><span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<?php
}
?>
</ul>
</div>
<select name="country1" class="bootstrap-select" title="India:State" multiple="" data-actions-box="true" tabindex="-98">
<?php
$this->db->select('*');
$this->db->from('states');
$this->db->where('country_id','101');
$sql1 = $this->db->get();
$result1 = $sql1->result_array();
foreach($result1 as $row1)
{
echo "<option value='".$row1['id']."'>".$row1['name']."</option>";
}
?>
</select>
</div>
jquery code:
<script>
$(document).ready(function(){
$('.state1 li').click(function(){
state = $(this).text();
alert(state);
});
});
</script>

Related

Getting Dropdown hidden id and get it using $_POST

I have this Dropdown made with bootstrap. I'm trying to take the id from the company the user has chosen to my $_POST method. In my table "empresas" I have two columns in it: 'id' and 'name';
This is my Dropdown code:
<ul class="nav nav-pills" role="tablist">
<li role="presentation" class="dropdown" style = "margin-left: auto; margin-right: auto">
</span>
<?php
require 'includes/dbh.inc.php';
$result = $conn->query("SELECT * from empresas") or die($conn->error);
?>
<ul class="dropdown-menu" id="menu1" aria-labelledby="drop4">
<?php
while ($row = $result->fetch_assoc()) : ?>
<li><a class="dropdown-item" href="#" name="idEmpresa" id = "idEmpresa" value = "<?php echo $row['id'] ?>" style="display:none;"><?php echo $row['id']; ?></a></li>
<li><a class="dropdown-item" href="#" value = "<?php echo $row['id'] ?>"><?php echo $row['nome']; ?> </a></li>
<?php endwhile; ?>
</ul>
</li>
</ul>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
<button type="submit" name="addDataDep" class="btn btn-primary" id="addbtn1">Adicionar</button>
</div>
</div>
</div>
</form>
</div>
</div>
And this is where i'll use the $_POST method:
if(isset($_POST['addDataDep'])) {
require 'dbh.inc.php';
$nomeDepartamento = $_POST['nomeDepartamento'];
$id = $_POST['idEmpresa'];
$_SESSION['message'] = "O departamento $idEmpresa foi adicionada com sucesso!";
}
require 'dbh.inc.php' already connects to my database.
When I try to $_POST it, it returns me this error: "Undefined index: idEmpresa".
Thank you for your help!

I want to get other buttons' data-filter value at the same time

I got only 0 values in this code. When I write this inside the function I get 3 same values. Please help me with this problem. Thanks in advance
$('#main_cat button, #price button, #color button').click(function() {
// Get data of category
var main_id = $("#main_cat button").data('filter');
var price = $("#price button").data('filter');
var color = $("#color button").data('filter');
alert(main_id + " " + price + " " + color);
$.ajax({
url: "<?php echo base_url(); ?>user/fetch",
method: "POST",
data: {
main_id: main_id,
price: price,
color: color
},
success: function(data) {
$('#products_list').css("height", "455px");
$('#products_list').html(data);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main_cat" class="flex-w flex-l-m filter-tope-group m-tb-10">
<button
class="stext-106 cl6 hov1 bor3 trans-04 m-r-32 m-tb-5 how-active1"
data-filter="0"
>
All Products
</button>
<?php foreach ($main_cat as $key =>
$value) { ?>
<button
class="stext-106 cl6 hov1 bor3 trans-04 m-r-32 m-tb-5"
data-filter="<?php echo $value['id']; ?>"
>
<?php echo $value['name']; ?>
</button>
<?php } ?>
</div>
<div class="filter-col3 p-r-15 p-b-27">
<div class="mtext-102 cl2 p-b-15">
Color
</div>
<ul id="color">
<li class="p-b-6">
<button data-filter="0" class="filter-link stext-106 trans-04">
All
</button>
</li>
<?php foreach ($color as $key =>
$value) { ?>
<li class="p-b-6">
<button
data-filter="<?php echo $value['name'];?>"
class="filter-link stext-106 trans-04"
>
<?php echo $value['name']; ?>
</button>
</li>
<?php } ?>
</ul>
</div>
<div class="filter-col2 p-r-15 p-b-27">
<div class="mtext-102 cl2 p-b-15">
Price
</div>
<ul id="price">
<li class="p-b-6">
<button
data-filter="0"
class="filter-link stext-106 trans-04 filter-link-active"
>
All
</button>
</li>
<li class="p-b-6">
<button data-filter="1" class="filter-link stext-106 trans-04">
$0.00 - $50.00
</button>
</li>
<li class="p-b-6">
<button data-filter="2" class="filter-link stext-106 trans-04">
$50.00 - $100.00
</button>
</li>
</ul>
</div>

Fetch data from mysql by checking radio button

i have few models of products and when i click on each model to show me his variants, like on image from below
Here is what i made in html :
<div class="owl-carousel owl-theme no-pb slide-arrow-2" data-
dots="false" data-nav="true" data-items="4" data-lg-items="3"
data-md-items="2" data-sm-items="2" data-margin="30" data-
autoplay="false">
<?php
$qua = "SELECT * FROM produse WHERE categorie = '$id' ORDER BY pozitie";
$qla = mysqli_query($conn, $qua);
while($rova = mysqli_fetch_assoc($qla)){?>
<?php
$id_paletar = $rova['id'];
$sql2=mysqli_query($conn,"SELECT * FROM pret_produse WHERE produs_id='$id_paletar'");
$pret2=mysqli_fetch_array($sql2);
?>
<div class="item">
<div class="product-item">
<div class="product-img">
<img class="img-fluid" src="cms/produse/<?=$rova['poza'];?>" alt="">
<div class="product-overlay">
<ul class="list-unstyled">
</ul>
</div>
</div>
<div class="product-btn ifix">
<div class="btn btn-theme btn-block btn-new" ><span style="font-size: 15px;"><?=$rova['titlu'];?></span>
<div>
<label class="containere">
<input type="radio" name="produse" id="paletar_pret" class="palt" value="<?=$rova['id'];?>">
<p class="checkmark"></span>
</label>
</div>
</div>
</div>
</div>
</div>
<?php }?>
</div>
Here is my jquery:
$(document).ready(function(){
$('.palt').change(function() {
if(this.checked) {
var check= $(this).val();
alert(check);
$.ajax({
url: 'cere_cotatie.php',
type: 'POST',
data: {check:check},
success: function(data){
$('.fetch').append(data);
}
});
}
});
And here is my php file which i use on ajax to call:
<?php
if($_POST)
{
include('config.php');
echo $check = $_POST['check'];
$s = "SELECT * FROM variante WHERE id_var='$check'";
$q = mysqli_query($conn, $s);
while ($r = mysqli_fetch_assoc($q)) {?>
<div class="item" style="width:220px;">
<div class="product-item">
<div class="product-img">
<img class="img-fluid" src="cms/variante/<?=$r['poza'];?>" alt="">
<div class="product-overlay">
<ul class="list-unstyled">
</ul>
</div>
</div>
<div class="product-btn ifix">
<div class="btn btn-theme btn-block btn-new" ><span style="font-size: 15px;"> <?=$r['denumire'];?></span>
<div>
<label class="containere">
<input type="checkbox">
<p class="checkmark"></span>
</label>
</div>
</div>
</div>
</div>
</div>
<?php }?>
<?php }
?>
My problem is like this if i check first box to display me variants of what i checked but my code is doing this way, if i check first box is loading data and after that if i check another box results from first box need to dissapear and to be only what i check.
EDIT: in that print screen you can see the problem.
I think you should add below code
$('.palt').change(function(e) {
e.preventDefault();
});
and remove alert(check);
Hope it work for you.

bootstrap input field and dropdown button submit mail form

I have the following problem. I have a form where I can choose from three options. The problem is that it does not send the selected option while the other sections Name, surname, etc. send. Below I am sending the code:
thank you in advance
<div class="col-sm-4">
<div class="dropdown">
<button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="drp-name" data-bind="label">Choose</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="service-type">
<li>x1</li>
<li>x2</li>
<li>x3</li>
</ul>
</div>
</div>
php code:
<?php
$to = 'xxx#xx.com';
$subject = "Appointment Form";
$servicetype = stripslashes($_REQUEST['service-type']);
$msg = "";
$msg .= "Service Type: ".$servicetype."\r\n\n"; // add sender's sources to the message
$mail = #mail($to, $subject, $msg, "From:".$email);
if($mail) {
header("Location:index.html");
} else {
echo 'Message could not be sent!';
}
?>
I also added JS but it still does't work
Dropdown Select
$( document.body ).on( 'click', '.dropdown-menu li', function( event ) {
var $target = $( event.currentTarget );
$target.closest( '.dropdown' )
.find( '[data-bind="label"]' ).text( $target.text() )
.end()
.children( '.dropdown-toggle' ).dropdown( 'toggle' );
return false;
});
HTML:
<div class="col-sm-4">
<div class="dropdown">
<button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="drp-name" data-bind="label">Rodzaj usługi</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="service-type">
<li>x1</li>
<li>x2</li>
<li>x3</li>
</ul>
</div>
<input type='hidden' name='service-type'> <!-- ADD THIS -->
</div>
JavaScript:
$(document.body).on('click', '.dropdown-menu li', function(event) {
event.preventDefault();
var $target = $(event.currentTarget);
$target.parents('.dropdown').sibling('[name=service-type]').val($target.text());
});
you cant pass values to php without name attribute and form tag . you can directly set it with select tag
<form action="file.php" method=POST>
<select name=option>
<option value=0 selected>0</option>
<option value=1> 1</option>
</select><input type=button value=submit>
</form>
Suppose the name of form is mail_form, and there is a hidden form element to hold the choice information with id service-type-holder and name service-type. Then the following code could be used with inline Javascript:
<form name="mail_form" id="mail_form" action="" method="post">
<input type="hidden" name="service-type" id="service-type-holder">
<div class="col-sm-4">
<div class="dropdown">
<button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="drp-name" data-bind="label">Choose</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="service-type">
<li onclick="$('#service-type-holder').val('x1'); $('#mail_form').submit()">x1</li>
<li onclick="$('#service-type-holder').val('x2'); $('#mail_form').submit()">x2</li>
<li onclick="$('#service-type-holder').val('x3'); $('#mail_form').submit()">x3</li>
</ul>
</div>
</div>

Delete divs using jQuery

I'm working with divs, jquery & PHP. Here is what I have up to now:
http://rabelatos.com/admin/prueba.php
If you click on some of the 2 gray buttons a new div will be added. But I would like to add a new icon to delete divs.
How can I implement this functionality?
This is the code that I have at this moment:
HTML & PHP:
<div class="page-content">
<div class="container-fluid">
<div class="row">
<div align="col-sm-12" style="padding-bottom: 15px;">
<center>
<button type="button" class="btn btn-secondary" data-dismiss="modal" id="addProduct">
<i class="fa fa-plus-circle" aria-hidden="true"></i> <i class="fa fa-cutlery" aria-hidden="true"></i>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" id="addDrink">
<i class="fa fa-plus-circle" aria-hidden="true"></i> <i class="fa fa-coffee" aria-hidden="true"></i>
</button>
</center>
</div>
</div>
<div id="FoodContainer">
<div class="form-group row" id="Food" style="display:block">
<label class="col-sm-2 form-control-label">
Food
</label>
<div class="col-sm-10">
<select name="food[]" id="food[]" class="form-control">
<option selected>Select</option>
<?
$s1=mysql_query("select * from productos where idCategoria='2'");
while($f1=mysql_fetch_array($s1)){
$idProduct=$f1["id"];
$title=$f1["titulo"];
echo "<p class=\"form-control-static\"><option value=\"$idProduct\">$title</option></p>";
}
?>
</select>
</div>
</div>
</div>
<div id="DrinkContainer">
<div class="form-group row" id="Drink" style="display:block">
<label class="col-sm-2 form-control-label">Drink</label>
<div class="col-sm-10">
<select name="drink[]" id="drink[]" class="form-control">
<option selected>Select</option>
<?
$s2=mysql_query("select * from productos where idCategoria='1'");
while($f2=mysql_fetch_array($s2)){
$idProduct1=$f2["id"];
$title1=$f2["titulo"];
echo "<option value=\"$idProduct1\">$title1</option>";
}
?>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
jQuery:
<script type="text/javascript">
jQuery(function($){
var $food = $('#Food'), count = 1;
$('#addProduct').click(function(e){
e.preventDefault();
var idname = 'food' + (++count);
$food.parent().append($food.clone().attr({id: idname, name: idname}));
});
var $drink = $('#Drink'), count = 1;
$('#addDrink').click(function(e){
e.preventDefault();
var idname = 'drink' + (++count);
$drink.parent().append($drink.clone().attr({id: idname, name: idname}));
});
});
</script>
Thanks !
Why don't you just add a delete (an X image) button next to each of the select boxes. As they are distinguished with their IDs (idname) , it can be easily deleted using jQuery. Hope that works for you.

Categories