html script - if statement based on checkbox - php

How do i make the depth section depends on which check box the user pick.
For example: If the user checked 1.0m for the depth interval, the depth should be a dropdown between 1.0,1.1,1.2,1.3,1.4 .
below are my current code:
<label>
Depth Interval:
</label>
<br>
<input type="checkbox" id="depth_interval1.0" name="depth_interval" onclick="onlyOne(this)" value="1.0m" />
<label for="depth_interval1.0"> 1.0m</label>
<input type="checkbox" id="depth_interval1.5" name="depth_interval" onclick="onlyOne(this)" value="1.5m" />
<label for="depth_interval1.5"> 1.5m</label>
<input type="checkbox" id="depth_interval3.0" name="depth_interval" onclick="onlyOne(this)" value="3.0m" />
<label for="depth_interval3.0"> 3.0m</label>
<input type="checkbox" id="depth_interval_cont" name="depth_interval" onclick="onlyOne(this)" value="continuos" />
<label for="depth_interval_cont"> Continuos</label><br><br>
<script>
function onlyOne(checkbox) {
var checkboxes = document.getElementsByName('depth_interval')
checkboxes.forEach((item) => {
if (item !== checkbox) item.checked = false
})
}
</script>
<label>
Depth:
</label>
<br>
<select name="depth_from" id="depth_from">
<option value="...">...</option>
<option value="1.0">1.0</option>
<option value="1.1">1.1</option>
<option value="1.2">1.2</option>
<option value="1.3">1.3</option>
<option value="1.4">1.4</option>
<option value="...">...</option>
</select>
to
<select name="depth_to" id="depth_to">
<option value="...">...</option>
<option value="1.0">1.0</option>
<option value="1.1">1.1</option>
<option value="1.2">1.2</option>
<option value="1.3">1.3</option>
<option value="1.4">1.4</option>
<option value="...">...</option>
</select>
<script>
var depth[] = 1.0,1.1,1.2,1.3,1.4
if(depth_interval=1.0)
depth_from.value&&depth_to.value = var
// something like this, i dont know much about scripts so forgive me
</script>

Should got a better way to do this , you can refer this to modify based on your requirement.
Can use jquery to hide and show the dropdownlist you want.
function onlyOne(checkbox) {
var checkboxes = document.getElementsByName('depth_interval');
checkboxes.forEach((item) => {
if (item !== checkbox) item.checked = false
})
// Get checkbox value
var depth = $("input[name='depth_interval']:checked").val();
// Check if depth is 1.0m
if(depth == '1.0m'){
$("#depth_from > option").each(function() {
var optionValue = this.value;
$('#depth_from [value="'+ optionValue +'"]').show();
$('#depth_to [value="'+ optionValue +'"]').show();
if(optionValue >= 1.5){
$('#depth_from [value="'+ optionValue +'"]').hide();
$('#depth_to [value="'+ optionValue +'"]').hide();
}
});
}
// Check if depth is 1.5m
if(depth == '1.5m'){
$("#depth_from > option").each(function() {
var optionValue = this.value;
$('#depth_from [value="'+ optionValue +'"]').show();
$('#depth_to [value="'+ optionValue +'"]').show();
if(optionValue < 1.5 || optionValue >= 3){
$('#depth_from [value="'+ optionValue +'"]').hide();
$('#depth_to [value="'+ optionValue +'"]').hide();
}
});
}
// Check if depth is 3.0m
if(depth == '3.0m'){
$("#depth_from > option").each(function() {
var optionValue = this.value;
$('#depth_from [value="'+ optionValue +'"]').show();
$('#depth_to [value="'+ optionValue +'"]').show();
if(optionValue < 3.0){
$('#depth_from [value="'+ optionValue +'"]').hide();
$('#depth_to [value="'+ optionValue +'"]').hide();
}
});
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="depth_interval1.0" name="depth_interval" onclick="onlyOne(this)" value="1.0m" />
<label for="depth_interval1.0"> 1.0m</label>
<input type="checkbox" id="depth_interval1.5" name="depth_interval" onclick="onlyOne(this)" value="1.5m" />
<label for="depth_interval1.5"> 1.5m</label>
<input type="checkbox" id="depth_interval3.0" name="depth_interval" onclick="onlyOne(this)" value="3.0m" />
<label for="depth_interval3.0"> 3.0m</label>
<br>
<select name="depth_from" id="depth_from">
<option value="1.0">1.0</option>
<option value="1.1">1.1</option>
<option value="1.2">1.2</option>
<option value="1.3">1.3</option>
<option value="1.4">1.4</option>
<option value="1.5">1.5</option>
<option value="3.0">3.0</option>
</select>
to
<select name="depth_to" id="depth_to">
<option value="1.0">1.0</option>
<option value="1.1">1.1</option>
<option value="1.2">1.2</option>
<option value="1.3">1.3</option>
<option value="1.4">1.4</option>
<option value="1.5">1.5</option>
<option value="3.0">3.0</option>
</select>

Related

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>

get value from multiple dropdown and add value to url

I have tried this code, that can display selected value and refresh the page .
This is the HTML
<select name="selectperiode" style="width:200px;" class="chzn-select" data-placeholder="Report Periode">
<option value=""></option>
<option value="1">Yearly</option>
<option value="4">Quarterly</option>
</select>
And this is the JQuery:
<script>
$(function(){
$("[name=selectperiode]").change(function(){
var val = $(this).val();
var stat;
if (typeof val !== 'undefined') {
stat = val;
}
else {
stat = '';
}
window.location.href = './corpreport.php?per='+stat;
return true;
});
});
</script>
It works fine. So I added one more dropdown and added some code with the code below:
<select name="selecttype" style="width:200px;" class="chzn-select" data-placeholder="Report Type">
<option value=""></option>
<option value="Pie">Pie</option>
<option value="Line">Line</option>
<option value="Bar">Bar</option>
<option value="Column">Column</option>
</select>
And this is the jquery
<script>
$(function(){
$("[name=selecttype]").change(function(){
var val = $(this).val();
var stat;
if (typeof val !== 'undefined') {
stat = val;
}
else {
stat = '';
}
window.location.href = './corpreport.php?per=<?php echo $_GET['per']; ?>&type='+stat;
return true;
});
});
</script>
It will produce localhost/folder1/file.php?per=1&type=Pie if you choose Yearly and Pie.
But when I change the periode to Quarterly, then the link change to localhost/folder1/file.php?per=4.
What should I do to make the link localhost/folder1/file.php?per=4&type=Pie ??
You can use the following jQuery:
$(function () {
$("[name=selectperiode]").change(function () {
window.location.href = period(this) + type($("[name=selecttype]"));
return true;
});
$("[name=selecttype]").change(function () {
window.location.href = period($("[name=selectperiode]")) + type(this);
return true;
});
function period(element) {
var val = $(element).val();
var stat;
if (typeof val !== 'undefined') {
stat = val;
} else {
stat = '';
}
return './corpreport.php?per=' + stat;
}
function type(element) {
var val = $(element).val();
var stat;
if (typeof val !== 'undefined') {
stat = val;
} else {
stat = '';
}
return '&type=' + stat;
}
});
With this PHP/HTML:
<?php
$per = (!empty($_GET["per"])) ? $_GET["per"] : '';
$type = (!empty($_GET["type"])) ? $_GET["type"] : '';
?>
<select name="selectperiode" style="width:200px;" class="chzn-select" data-placeholder="Report Periode">
<option value=""></option>
<option value="1" <?php if($per == "1"){echo "selected";} ?>>Yearly</option>
<option value="4" <?php if($per == "4"){echo "selected";} ?>>Quarterly</option>
</select>
<select name="selecttype" style="width:200px;" class="chzn-select" data-placeholder="Report Type">
<option value=""></option>
<option value="Pie" <?php if($type == "Pie"){echo "selected";} ?>>Pie</option>
<option value="Line" <?php if($type == "Line"){echo "selected";} ?>>Line</option>
<option value="Bar" <?php if($type == "Bar"){echo "selected";} ?>>Bar</option>
<option value="Column" <?php if($type == "Column"){echo "selected";} ?>>Column</option>
</select>
Assign ids to both select for instance period, type. Get values of both selects on change event to make the url.
Html
<select id="period" name="selectperiode" style="width:200px;" class="chzn-select" data-placeholder="Report Periode">
<option value=""></option>
<option value="1">Yearly</option>
<option value="4">Quarterly</option>
</select>
<select id="type" name="selecttype" style="width:200px;" class="chzn-select" data-placeholder="Report Type">
<option value=""></option>
<option value="Pie">Pie</option>
<option value="Line">Line</option>
<option value="Bar">Bar</option>
<option value="Column">Column</option>
</select>
Javascript
$("[name=selecttype]").change(function(){
var period = document.getElementById('period').value;
var type = document.getElementById('type').value;
window.location.href = './corpreport.php?per=' + period + '&type='+type;
return true;
});
<select id="selectperiode" style="width:200px;" class="chzn-select" data-placeholder="Report Periode">
<option value=""></option>
<option value="1">Yearly</option>
<option value="4">Quarterly</option>
</select>
<select id="selecttype" name="selecttype" style="width:200px;" class="chzn-select" data-placeholder="Report Type">
<option value=""></option>
<option value="Pie">Pie</option>
<option value="Line">Line</option>
<option value="Bar">Bar</option>
<option value="Column">Column</option>
</select>
Jquery
$(document).ready(function () {
$("#selecttype").change(function () {
var period = $("#selectperiode").val();
var type = $("#selecttype").val();
window.location.href = './corpreport.php?per=' + period + '&type=' + type;
return true;
});
});

Show/hide dynamic textbox based on pulldown

I'm trying to figure out how to have jQuery show/hide a dynamically generated textbox based on a pulldown menu selection. My JS skills are extremely limited. I have the following created in a while loop, depending on the number of people:
<select name="location[<?=$pid?>]" id="location[<?=$pid?>]">
<option value="<?=$loc_id?>" selected><?=$loc_name?> </option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<? $query_loc = mysqli_query($link, "SELECT * FROM locations WHERE loc_app = 'Y' ORDER BY loc_name ASC");
while($fetch_loc = mysqli_fetch_array($query_loc)){
$loc_id = $fetch_loc['loc_id'];
$loc_name = $fetch_loc['loc_name'];
?>
<option value="<?=$loc_id?>"><?=$loc_name?></option>
<?
} ?>
</select>
<input name="location_other[<?=$pid?>]" type="text" id="location_other[<?=$pid?>]" style="display:none" />
jQuery function:
<script type='text/javascript'>
$(window).load(function(){
$('#location').change(function () {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>
I need to keep the id of the and the serialized with the $pid variable for later processing. Is there another way to call the jquery function on these?
The HTML output looks like this:
<select name="location[287]" id="location[287]">
<option value="1" selected>A</option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<option value="12">B</option>
<option value="12">C</option>
</select>
<input name="location_other[287]" type="text" id="location_other[287]" style="display:none" />
Try
$(window).load(function(){
$('#location').change(function () {
$('input[id^=location_other]').hide();
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other' + $(this).val()).hide();
}
});
});
Try this
<select name="location[287]" id="location[287]">
<option value="Other">Other</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
<input name="location_other[287]" type="text" id="location_other" style="display:none" />
<script type='text/javascript'>
$(window).load(function() {
if ($('select[id^="location"]').val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
$('select[id^="location"]').change(function() {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>

Categories