I can't insert data of select from modal? - php

I tried to do the same for the input but With no chance however I did not understand if I has to be done with the change function should it be inserted seperately from the rest of the rest of the modal data
<select class="form-select form-select-lg mb-3" aria-label=".form-select-lg example" name="TypeCo" id="TypeCo">
<option selected name="TypeCo" id="TypeCo">Type de concentrateur</option>
<option value="Concentrateur Fixe">Concentrateur Fixe</option>
<option value="Concentrateur transportable">Concentrateur transportable</option>
</select>
<select class="form-select form-select-lg mb-3" aria-label=".form-select-lg example" name="CapCo">
<option selected name="CapCo" id="CapCo">Capacité de concentrateur</option>
<option value="5L">5L</option>
<option value="7L">7L</option>
<option value="10L">10L</option>
<option value="15L">15L</option>
</select>
and here is my script
$(document).ready(function() {
$(document).on('click', '#save', function() {
var fname = $('#fname').val();
var lname = $('#lname').val();
var tel = $('#tel').val();
var email = $('#email').val();
var TypeCo = $('#TypeCo').val();
var CapC = $('#CapCo').val();
alert(fname + lname + tel + email + TypeCo);
$.ajax({
url: "insert.php",
type: "post",
data: {
fname: fname,
lname: lname,
email: email,
tel: tel,
TypeCo: TypeCo,
CapCo = CapCo
},
success: function(data) {
$("#save").modal('hide');
alert('jj');
location.reload();
}
});
});
});
any help please ? it's my first project with ajax and I cannot really find a vid that can help me

This is indeed how you get the value from a form element in jQuery:
var TypeCo = $('#TypeCo').val();
However, you are targeting the wrong element. #TypeCo is currently targeting an <option> element:
<option selected name="TypeCo" id="TypeCo">Type de concentrateur</option>
You want to get the value of the <select> element itself (which contains <option> elements for the user to select that value). Move the name and id there:
<select class="form-select form-select-lg mb-3" aria-label=".form-select-lg example" name="TypeCo" id="TypeCo">
<option selected>Type de concentrateur</option>
<option value="Concentrateur Fixe">Concentrateur Fixe</option>
<option value="Concentrateur transportable">Concentrateur transportable</option>
</select>
Then you can use .val() exactly like you already do:
$('#TypeCo').on('change', function () {
let value = $('#TypeCo').val();
console.log(value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-select form-select-lg mb-3" aria-label=".form-select-lg example" name="TypeCo" id="TypeCo">
<option selected>Type de concentrateur</option>
<option value="Concentrateur Fixe">Concentrateur Fixe</option>
<option value="Concentrateur transportable">Concentrateur transportable</option>
</select>

Related

How to fix bootstrap multiselect search data using ajax

I am Using bootstrap multi select. I have two drop downs and both have datas with search feature.
what i am doing .. on change function on first drop down i want to change second drop down value using ajax.I have used rebuild too. But search feature value is not changing
<select class="selectpicker form-control multiselect" multiple="multiple" role="multiselect" data-live-search="true" id="sel1">
<option selected disabled> Class</option>
<option value="one">one</option>
<option value="two">two</option>
<option value=" three">three</option>
</select>
<select class="selectpicker form-control multiselect" multiple="multiple" role="multiselect" data-live-search="true" id="sel2">
<option selected disabled>Molecule</option>
<option value="100 student">100 student</option>
<option value="200 student">200 student</option>
<option value="300 student">300 student</option>
</select>
My ajax function
$("#sel1").change(function () {
$.ajax({
type: "POST",
url: "http://test.com/selectdropdown",
cache: false,
data: {
classval: val1
},
}).done(function (msg) {
var data = $.parseJSON(msg);
var options = '';
var tmp = [];
for (var i = 0; i < data.classdata.length; i++) {
var id = data['classdata'][i]['class_id'];
var name = data['classdata'][i]['noofstudent'];
options += '<option value=' + id + '>' + name + '</option>';
element = { "label": name, "value": id }
tmp.push(element);
}
$("#sel2").multiselect('dataprovider', tmp);
$('#sel2').multiselect('rebuild');
})
});
but drop down two data is not chaning.. if i use inspect element i can see the data
After lot of rnd . I have got solution. i have to refresh select picker too. hope this help to others
$('.selectpicker').selectpicker('refresh');

How to pass dropdown value with url?

code:
<script>
$(document).ready(function(){
$("#client").change(function(){
client = $(this).val();
$("#customer").html(client);
});
});
</script>
PDF
<select name="client" id="client" class="rights">
<option value="">Select Client</option>
<option value="test">test</option><option value="ABC Corp">ABC Corp</option>
<option value="others">Others</option>
</select>
In this code I have a dropdown through which I want when I change or select any value from dropdown then that value will be add with url as I mention in link tag i.e.. So, How can I do this ? please help me.
Thank You
you need to store the selected value and register click event on link, where you can pass the stored value
<script>
var value = null;
$(document).ready(function(){
$("#client").change(function(){
value = $(this).val();
});
$("#link").click(function(event){
event.preventDefault();
location.href=$(this).attr("href")+value;
return false;
});
});
</script>
PDF
<select name="client" id="client" class="rights">
<option value="">Select Client</option>
<option value="test">test</option><option value="ABC Corp">ABC Corp</option>
<option value="others">Others</option>
</select>
<script>
$(document).ready(function(){
$("#client").change(function(){
value = $(this).val();
href = $("#link").attr("href");
$("#link").attr("href",href + value);
});
});
</script>
PDF
<select name="client" id="client" class="rights">
<option value="">Select Client</option>
<option value="test">test</option><option value="ABC Corp">ABC Corp</option>
<option value="others">Others</option>
</select>

Can't get the select option value into jQuery post

HTML:
<select id="s2_basic" class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Javascript:
function submitForm() {
var userid = $('#userid').attr('selected', 'selected');
$.ajax({
type: "POST",
url: "submit.php",
data: "userid=" + userid,
success: function(text) {
if (text == "success") {
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
Submit PHP returns error message:
Value: ([object Object])
I have also tried
var userid = $('#userid').val();
var userid = $('#userid option').val();
Still, the option value never gets passed correctly.
What am I doing wrong?
I want just the value="x" number to be posted as userid=x.
remove id="s2_basic" from your select.
change :
<select id="s2_basic" class="form-control" id="userid" name="userid">
to
<select class="form-control" id="userid" name="userid">
$('#userid').on('change',function(){
console.log('value '+$('#userid option:selected').val())
console.log('text '+$('#userid option:selected').text())
console.log('index '+$('#userid option:selected').index())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
try this

Send text instead of value

I have a HTML attribute in my script:
<select name="cars" onChange="getPrice(this.value)">
<option value="1">Volvo XC90</option>
<option value="2">Saab 95</option>
<option value="3">Mercedes SLK</option>
<option value="4">Audi TT</option>
</select>
When I select the first item and post this to the database my script sends the wrong data to the database. In my database I get the value instead of the text.
By selecting the first <option> I want to post Volvo XC90 instead of s. How can I solve this?
The Ajax script I am using is:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getPrice() {
// getting the selected id in combo
var selectedItem = jQuery('#cars option:selected').val();
// Do an Ajax request to retrieve the product price
jQuery.ajax({
url: 'get.php',
method: 'POST',
data: {'cars': selectedItem },
success: function(response){
// and put the price in text field
jQuery('#cars').val(response);
},
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>
send.php
<?php
$correct = true;
$product1 = $_POST['product1'] ;
if($correct){
$db = new PDO('mysql:host=localhost;dbname=database', 'root', '');
$query = "INSERT INTO forms(product1) VALUES (?)";
$stmt = $db->prepare($query);
$stmt->execute(array($product1));
header("Location: ./print.php");
}
else
{
echo "<br /><br />Error.<br />\n";
}
?>
Update 1:
Something like this is ideal for my situation:
html:
<select name="cars" onChange="getPrice(this.value)">
<option id="1" value="Volvo XC90">Volvo XC90</option>
<option id="2" value="Saab 95">Saab 95</option>
<option id="3" value="Mercedes SLK">Mercedes SLK</option>
<option id="4" value="Audi TT">Audi TT</option>
</select>
function:
$('[id=cars]').change(function()
{
alert($('[id=cars] option:selected').text());
});
Use html as
<select name="cars" onChange="getPrice(this.value)">
<option value="">Select</option>
<option value="Volvo XC90">Volvo XC90</option>
<option value="Saab 95">Saab 95</option>
<option value="Mercedes SLK">Mercedes SLK</option>
<option value="Audi TT">Audi TT</option>
</select>
and little change in your js function as
function getPrice(selectedItem) {
Instead of
function getPrice() {
You are already sending onChange="getPrice(this.value)" selected value here, just needed to catch it.
so there is no need to code to get again, remove this line
var selectedItem = jQuery('#cars option:selected').val();
Also added new option with blank value as you are sending value in onchange, there will be no effect on first load and it will be selected first column.
I suggest first check not empty for selectedItem then go for ajax.
function getPrice(selectedItem) {
alert(selectedItem);
}
<select name="cars" onChange="getPrice(this.value)">
<option value="">Select</option>
<option value="Volvo XC90">Volvo XC90</option>
<option value="Saab 95">Saab 95</option>
<option value="Mercedes SLK">Mercedes SLK</option>
<option value="Audi TT">Audi TT</option>
</select>
Bunch of things :
1.Replace .val() with .text().
2.Use name selector ( since there is no such element with id = cars )
var selectedItem = jQuery('[name=cars] option:selected').text();
EDIT:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getPrice()
{
var selectedItem = $( "#cars" ).val();
alert(selectedItem);
}
</script>
<select name="cars" onChange="getPrice(this.value)" id="cars">
<option id="1" value="Volvo XC90">Volvo XC90</option>
<option id="2" value="Saab 95">Saab 95</option>
<option id="3" value="Mercedes SLK">Mercedes SLK</option>
<option id="4" value="Audi TT">Audi TT</option>
</select>

Getting value of a dynamically created select

Apologies if my question is a little badly worded but I am not quite sure how to ask it correctly.
I am using PHP to dynamically generate some selects and I was just wondering if it is possible to do this?
Or would I need to add something like {onChange="some_function();"} to each of the selects, passing some variable(s) to id it ?
<script type="text/javascript">
$('Selects ID or class').change(function(event) {
$.post('formupdate.php', { selected: $('Selects ID or class').val() },
function(data) {
$('update the div linked to select').html(data);
}
);
});
</script>
<p>Q1</p>
<select name="Q1select" id="Q1select" class="Q1">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update1" class="Q1"></div>
<p>Q2</p>
<select name="Q2select" id="Q2select" class="Q2">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update2" class="Q2"></div>
<p>Q3</p>
<select name="Q3select" id="Q3select" class="Q3">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update3" class="Q3"></div>
Updated working script :
<script type="text/javascript">
$(document).ready(function () {
$('.question').change(function (event) {
var $this = $(this);
$.post('formupdate.php', { selected:$this.val() },
function (data) {
$('#' + $this.data('div')).html(data);
}
);
});
});
</script>
Updated html :
<select name="select_Q1" id="select_Q1" class="question" data-div="update_Q1">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update_Q1"></div>
<p>Q2</p>
<select name="select_Q2" id="select_Q2" class="question" data-div="update_Q2">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update_Q2"></div>
<p>Q3</p>
<select name="select_Q3" id="select_Q3" class="question" data-div="update_Q3">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update_Q3"></div>
You can assign an event handler to all the select elements using the following:
$('select').change(function(event) {
....
});
​
In the event handler, you can get the value of the select using:
$(this).val()
To get the ID of the select in the event handler, you can use;
$(this).attr('id')
Yes that would work ... but you could simplify a touch :
$('Selects ID or class').change(function(event) {
$.post('formupdate.php', { selected: $(this).val() },
function(data) {
$('update the div linked to select').html(data);
}
);
});
changed the selector within the function to $(this) instead of a class or id that way it will work when you use on multiple elements
To link the select to a div you could either name the div similar to the select ie select id = select_1 and div = div_1 or use the data property
<select name="Q2select" id="Q2select" data-div="divid" class="Q2">
then within your AJAX callback you can do :
$('#' + $(this).data('div')).html(data);
this will set the HTML content of the div with the id = divid ... just an idea
Update
there are a couple of things to try and debug this, first :
function (data) {
$('#update_Q3').html(data);
}
this puts the content in a div - this checks the content returned from the post is working and that you can insert HTML at this point
and try this :
function (data) {
alert($(this).data('div'));
}
this will confirm your selecting the correct div by getting the data
Update 2
My screw up !!!!! $(this) is not working in the callback, try this :
$('.question').change(function (event) {
var $this = $(this);
$.post('formupdate.php', { selected:$(this).val() },
function (data) {
$('#' + $this.data('div')).html(data);
}
);
});
we save $(this) into a variable $this for later use
Here's how I would do it, using a class of question on each <select> and using the id as the unique identifier. The benefit of using a class to grab the selects is if you ever add any other select to the page, they won't be affected.
<script type="text/javascript">
$('.question').change(function(event) {
$.post('formupdate.php', { selected: $(this).val() },
function(data) {
// lets find the div that matches this question and update it
var questionNum = $(this).attr('id').replace('select_Q', '');
var divId = 'update_Q' + questionNum;
$('#' + divId).html(data);
}
);
});
</script>
<p>Q1</p>
<select name="select_Q1" id="select_Q1" class="question">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update_Q1"></div>
<p>Q2</p>
<select name="select_Q2" id="select_Q2" class="question">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update_Q2"></div>
<p>Q3</p>
<select name="select_Q3" id="select_Q3" class="question">
<option value="-">-</option>
<option value="type1">Type One</option>
<option value="type2">Type Two</option>
<option value="type3">Type Three</option>
</select>
<div id="update_Q3"></div>
Yes you could do
//For every select whose id ends with select
$('select[id$=select]').change(function(event) {
//getthe id so that you can post it
var id = this.id;
//save the variable this to another one to use in the callback
var that = this;
//post the selected value and thew id of the select
$.post('formupdate.php', { selected: $(this).val(), id : id },
function(data) {
update the next element after the select
$(that).next().html(data);
}
);
});

Categories