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;
});
});
Related
This for my dropdownlist and format for auto numbering
and if someone choose NK/W will get No.Reg :
001/NK/W/2018, 002/NK/W/2018, 003/NK/W/2018
And if someone choose NK/S will get No. Reg 001/NK/S/2018.
If years change, the auto numbering will reset to 001 again
The solution here has more to do with javascript than Laravel or PHP for instance.
Check this out
https://jsfiddle.net/j6znavy9/
<p>
Reg No: Auto/
<select id="mySelect">
<option selected disabled>Select an Option</option>
<option value="NK/W">NK/W</option>
<option value="NK/S">NK/S</option>
<option value="MM/A">MM/A</option>
</select>
Year: *
<select id="yearSelect">
<option selected disabled>Select a Year</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</p>
<script>
$(document).ready(function() {
function pad(number, size) {
var s = String(number);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
var year, mySelect, auto = 1;
$('#yearSelect').on('change', function(){
year = $(this).val();
auto = 1;
});
$('#mySelect').on('change', function(){
if( year === null || year === undefined ) {
alert('year is not defined!');
} else {
alert('Reg No: ' + pad(auto, 3) + '/' + $(this).val() + '/' + year);
auto++;
}
})
});
</script>
I am submitting a form using jquery and ajax. I have my first select menu and depending on the value of this select menu a div that contains a different menu will become visible.
<table>
<tr>
<td>
<select required="" class="input-medium" id="foulTypes" name="foul" data-original-title="" title="">
<option value="">Select Foul</option>
<option value="85">AID - Aiding Runner</option>
<option value="1">BAT - Illegal Bat</option>
<option value="2">BBW - Block Below Waist</option>
<option value="3">BOB - Blocking Out of Bounds</option>
<option value="4">CHB - Chop Block</option>
</select>
</td>
</tr>
</table
<script>
var foulTypes = $('#foulTypes');
var select = this.value;
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
} else {
$('#catDPI').hide();
}
if ($(this).val() == '85') {
$divSelectedCategory = $("#catILF");
$('#catILF').show();
} else {
$('#catILF').hide();
}
if ($(this).val() == '2') {
$('#catOPI').show();
$divSelectedCategory = $("#catOPI");
} else {
$('#catOPI').hide();
}
//We now enable only for the selected category
$divSelectedCategory.show().find(".category").attr("disabled", false).val('');
});
</script>
<div id="catDPI" style="display:none;">
<select name="category" id="dpiCategory" class='category' style="width:210px;">
<option value="Playing Through Receiver">Playing Through Receiver</option>
<option value="arm bar">arm bar</option>
<option value="Grabbing">Grabbing</option>
</select>
</div>
<div id="catILF" style="display:none;">
<select name="category" id="ilfCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="moving">Moving</option>
<option value="illegal">Illegal</option>
<option value="standing">Standing</option>
</select>
</div>
<div id="catOPI" style="display:none;">
<select name="category" id="opiCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="restrict">Restrict</option>
<option value="holding">Holding</option>
</select>
</div>
The foulTypes .change function is working and the correct div is being displayed. The problem I am having is when I submit the form. If the #catDPI is visible the category menu selected value is submitted fine. If #catILF or #catOPI is visible the value for category is blank. I am not using the Ids of the category menus.
$("#submit-foul").submit(function(e){
e.preventDefault();
var category = $(".category").val();
var dataString = 'category=' + category;
if($("#submit-foul").valid()){
$.ajax({
url: 'myUrl',
type: 'POST',
success: function(data){
if(data == 1){
alert('Success');
};
}
});
}
return false;
});
I am not sure what I am doing wrong. Time to send it to the experts!! I appreciate all your help.
Try to change function "foulTypes" and add class in visible select.
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
$('#dpiCategory').addClass("visible");
} else {
$('#catDPI').hide();
$('#dpiCategory').removeClass("visible")
}
.... CONTINUE
In your ajax submit..
var category = $(".category.visible").val();
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>
`<select multiple="multiple" name="state[]" id="state">
<option value="">Select a State</option>
<?php
foreach($state_list as $key=>$value){
echo "<option value=\"$key\"";
if($html['state']==$key|| $row['state']==$key){
echo ' selected="selected"';
}
echo ">$value</option>\n";
}?>
</select> </p>`
<p>Select A Country
<select name="country" id="country">
<option value="0">Select a Country</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
</select> </p>
I have two select boxes 1)state[] 2) country . What is want is that "If the selected country is not USA, disable the multi-select box which consists all the USA states". Any help is appreciated
Add this Javascript to your code:
<script type="text/javascript">
<!--
window.onload = function() {
var selectCountry = document.getElementById('country');
selectCountry.onchange = function() {
document.getElementById('state').disabled = (selectCountry.value != 'USA')? true : false;
};
};
//-->
</script>
You could do the following if you have jQuery:
$('#country').change(function () {
if($('#country').val() != "USA") {
$('#state').attr('disabled', 'disabled');
}
else {
$('#state').removeAttr('disabled');
}
});
Or if you are using plain JavaScript:
var elem = document.getElementById("country");
elem.onchange = function () {
if(elem.options[e.selectedIndex].value != "USA") {
document.getElementById('state').disabled = "disabled";
}
else {
document.getElementById('state').removeAttribute("disabled");
}
}
This will disable the state combo box when the country combo box contains a value that is not "USA". When the selected country is "USA", it will remove the disabled attribute and make it usable again.
Hope this helps.
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);
}
);
});