This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
Is it possible to trigger an event on selected option without submitting form?
<h3 id="center" align="center">OUR SITE CAN ONLY PROVIDE YOU INFO ABOUT MANTIONED COUNTRIES</h3>
<SELECT style="padding:10px;margin-left: 550px;margin-top: 20px;">
<option name="india">India</OPTION>
<option name="pakistan">Pakistan</OPTION>
<option name="US">UNITED STATE</OPTION>
<option name="UK">UNITED KINGDOM</option>
</SELECT>
You can't do that with PHP (since it's server side language). Use js/jQuery for that.
$(document).ready(function() {
$("#dropdown").change(function() {
alert("Selection: " + $("option:selected", this).val() + ":" + $("option:selected", this).text());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 id="center" align="center">OUR SITE CAN ONLY PROVIDE YOU INFO ABOUT MANTIONED COUNTRIES</h3>
<select id="dropdown" name="countries" style="padding:10px;margin-left: 550px;margin-top: 20px;">
<option value="india">India</option>
<option value="pakistan">Pakistan</option>
<option value="US">UNITED STATE</option>
<option value="UK">UNITED KINGDOM</option>
</select>
Also, name attribute is for select not for option. Check snippet markup changed.
Because you requested for a javascript example. Here is an example of a javascript variant.
<select onchange="jsFunction(this)">
<option value="" disabled selected style="display:none;">Label</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<script>
function jsFunction(select)
{
alert(select.options[select.selectedIndex].value);
}
</script>
you cant do that with php but can be done using Javascript/Jquery as #S.Pols said
$(document).ready(function() {
$('body').on("change","#select",function(){ // select is the id of your select tag
alert("selected");
});
});
Related
Hey guys I'm trying to create a dynamic button from a drop down list. for example, in the HTML code below...
function dropdownbutton(){
var make = document.getElementById("makelist");
var answer = make.options[make.selectedIndex].value;
alert("answer")
}
<div class="make">
<label>Make</label>
<select name="make" id="makelist" onchange="getId(this.value);">
<option value="">Select Make</option>
<option value="">1</option>
</select>
</div>
<div id="model">
<label>Model</label>
<select name="model" id="modellist" onchange="getId2(this.value);">
<option value="">Select Model</option>
<option value="">1</option>
<option value="">2</option>
</select>
</div>
<div id="year">
<label>Year</label>
<select name="year" id="yearlist" onchange="getId3(this.value);">
<option value="">Select Year</option>
<option value="">1</option>
<option value="">2</option>
</select>
</div>
<button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>
I'm open to trying it in different languages but I would prefer to do it in php, I simply don't know how to get the values from each dropdown.
I have taken help of Jquery here, inside the dropdownbutton() function i call all the select box element.
And using .each function extracted the value selected value of dropdowns.
i check those value if not empty then created a button inside element having Id #append_new_button
Please check the below code it might solve your issue
Thanks
function dropdownbutton() {
jQuery('#append_new_button').html('');
jQuery('select').each(function(){
var select_value = jQuery(this).val();
var select_label = jQuery("#"+jQuery(this).attr('id')+" option[value='"+select_value+"']"). text();
if(select_value != ''){
jQuery('#append_new_button').append('<input type="button" id="'+select_value+'" value="'+select_label+'"></button>')
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="make">
<label>Make</label>
<select name="make" id="makelist">
<option value="">Select Make</option>
<option value="make_1">Make 1</option>
<option value="make_2">Make 2</option>
</select>
</div>
<div id="model">
<label>Model</label>
<select name="model" id="modellist" >
<option value="">Select Model</option>
<option value="model_1">Model 1</option>
<option value="model_2">Model 2</option>
</select>
</div>
<div id="year">
<label>Year</label>
<select name="year" id="yearlist" >
<option value="">Select Year</option>
<option value="year_1">year 1</option>
<option value="year_2">year 2</option>
</select>
</div>
<button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>
<div id="append_new_button">
</div>
I updated that code to find the values for each drop down
function dropdownbutton(val){
var make = document.getElementById("makelist");
var answer = make.options[make.selectedIndex].value;
var model = document.getElementById("modellist");
var answer2 = model.options[model.selectedIndex].value;
var year = document.getElementById("yearlist");
var answer3 = year.options[year.selectedIndex].value;
alert(answer2);
}
Now i need to figure out how to pass these variables to a link for example, mydomain.com/answer/answer2/answer3
i think that this code is more dynamic.
$("button").click(function() { // if button is clicked
var arr = $('select').map(function() { // run through all selects and get value
return this.value
}).get();
var url = "http://exp.com"; // base for the url
arr.forEach(function(item) {
url = url + '/' + item; // append all options to the base url
});
window.location.href(url); // redirect to base url + all options
});
Don't forget to add value to your options. <option value="1">1</option>
Please look at jsfiddle for a working example
This question already has answers here:
Using Ajax, jQuery and PHP in two dropdown lists
(2 answers)
Closed 8 years ago.
I was making a dropdown switch. I mean when I choose an option of the first drop down the second will show specified message preset's in stead of the first.
**Dropdown one**
<select name="bantype">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select></br></br>
**Dropdown two**
<select name="reason">
<option value="Sexual harassment">Sexual harassment</option>
</select>
How can I make that when I select an option from the first dropdown that the option values of the second will switch?
Thank you for help
It's called hierselect, chained or related selects.
I like this jquery plugin: http://www.appelsiini.net/projects/chained
So you have predefined values for each dropdown value from first list
<select id="first">
<option value='ban' data-show="#optionsOne">Ban Ban</option>
<option value='banAll' data-show="#optionsTwo">Ban All</option>
</select>
And you have other (hidden) dropdown for each of your options:
<select id="optionsOne" class='drop'>...</select>
<select id="optionsTwo" class='drop'>...</select>
When #first changes values, show relative dropdown:
$('#first').change(function(){
$('.drop').hide(); // hide all dropdowns
id = $('option:selected', this).data('show'); // take relevant id
$(id).show(); // show that dropdown with above id
});
Call javascript function event onchange,
<select name="bantype" onchange="getoptions(this.value);">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select><br/><br/>
<select name="reason" id="reason"></select>
In your function, use following
getoptions(val) {
if(val == 'user') {
var your_options = '<option value="abc">Abc</option>';
jQuery('#reason').html(your_options);
jQuery('#reason').show(); // if second dropdown is hidden
}
}
If you have a fixed option on page load, then here is one way to achieve what you want.
Give a relation between first select and the second one, like this:
<select name="bantype">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select></br></br>
**Dropdown two**
<select name="reason">
<option class="user" value="a">a</option>
<option class="user" value="d">d</option>
<option class="ip" value="b">b</option>
<option class="system" value="c">c</option>
</select>
Use jQuery to show and hide the option, like this
$("select[name='reason'] option").each(function(){
$(this).hide();
});
$("select[name='bantype']").on("change", function(){
var selected = $(this).val();
$("select[name='reason'] option").each(function(){
if($(this).prop("class") != selected){
$(this).hide();
}else{
$(this).show();
}
});
});
Check out this DEMO..
I want to create a very simple (no plugins and basic layout) for a dropdown menu that has checkboxes for options. For example, if C and F are selected and the form is submitted, I want the url to show /index.php?category1=3&&category2=6
<form action="" method="get">
<select multiple="multiple" name="category">
<optgroup label="Category1">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</optgroup>
<optgroup label="Category2">
<option value="4">D</option>
<option value="5">E</option>
<option value="6">F</option>
</optgroup>
</select>
<br><input type="submit" value="go">
</form>
I am not even sure how to start the jquery to pass the values to the url. Any help is much appreciated!
EDIT:
I figured I can do this using instead and simulate a dropdown with jquery.
Here is the jsfiddle: http://jsfiddle.net/k6R7g/
how can I show "2 Selected" instead of "Select..." when selections are made?
See code below, construct the url yourself and it will work. In case the form contains more values you can better add the category to the value of the option.
<form method="get">
<select multiple="multiple" name="category">
<optgroup label="Category1">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</optgroup>
<optgroup label="Category2">
<option value="4">D</option>
<option value="5">E</option>
<option value="6">F</option>
</optgroup>
</select>
<br><input type="button" value="go" onclick="clicked();">
</form>
<script type="text/javascript">
function clicked(){
var v = "";
$('select').find(":selected").each(
function(){
if (v != ""){
v += "&";
}
v += $(this).parent()[0].label + '=' + $(this).context.value;
}
);
window.location.href = "index.php?" + v;
}
</script>
Wow...all similar questions have serious downvotes. Guess I will be taking some as well, but been on this for a couple days and still stuck. So here it goes...
All of the following code is in index.php:
<form action="index.php" method="get">
<select name="month" id="month">
<option value="1">jan</option>
<option value="2">feb</option>
<option value="3">mar</option>
<option value="4">apr</option>
<option value="5">may</option>
<option value="6">jun</option>
<option value="7">jul</option>
<option value="8">aug</option>
<option value="9">sep</option>
<option value="10">oct</option>
<option value="11">nov</option>
<option value="12">dec</option>
</select>
</form>
<?php
$month = $_GET["month"];
echo "MONTH: $month";
$month in the php is NULL (Nothing prints out for $month). I want to be able to use it in the <?php ?> to make adjustments to it, then use it again in my HTML. How can I get the value from the HTML code for use in the PHP code?
I want to take 'value' and assign it to '$month'
Changes on the html page cannot change things within a php script as it has already executed on the server side. You need to setup some method to add the proper variable to the url (as you are using $_GET which is an array of the url variables in the query string, name value pairs after the ? in a url).
HTML
<select name="month" id="mySelect">
<option value="1">jan</option>
<option value="2">feb</option>
<option value="3">mar</option>
<option value="4">apr</option>
<option value="5">may</option>
<option value="6">jun</option>
<option value="7">jul</option>
<option value="8">aug</option>
<option value="9">sep</option>
<option value="10">oct</option>
<option value="11">nov</option>
<option value="12">dec</option>
</select>
JS
window.onload = function(){
var select = document.getElementById("mySelect");
select.onclick = function(){
var selectval = this.value;
window.location.href = "/somepage.php?month="+selectval;
};
};
Or using form submit, forms with the method GET will put all the input/select/textarea elements values into the url when submitted
<form action="somepage" method="GET">
<select name="month" id="mySelect">
<option value="1">jan</option>
<option value="2">feb</option>
<option value="3">mar</option>
<option value="4">apr</option>
<option value="5">may</option>
<option value="6">jun</option>
<option value="7">jul</option>
<option value="8">aug</option>
<option value="9">sep</option>
<option value="10">oct</option>
<option value="11">nov</option>
<option value="12">dec</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
PHP is a server side language. You cannot use php on client side. If you want to do something on client side, use JavaScript.
If you want to use php with your html either you have to refresh the page(see answer of Patrik Evans), or use Ajax.
If you are planning to use ajax as it is fast and simple, move your php to a different file like index-form.php and call it by ajax get method.
Basically when you load the page via browser, browser first lookup for your site IP from DNS. Then your server will compile the whole page (ignoring html, js and other client side script) and compile the server side scripts like PHP, Perl, Python etc depending upon the type of server. Then your server will send the compiled page to your browser and browser will read it, compile the client side scripts and render it in your viewport.
This question already has an answer here:
exclude value from one select box based on value of other selectbox
(1 answer)
Closed 9 years ago.
I have 2 selects (a,b). Same data in both select.
I want that when I choose an option in one of them, the same option disapear in the second one. If a reselect an other option, the one tha disapeared should reappears
tx
check here http://jsfiddle.net/RTzwL/
html
<select id="first">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="japan">Japan</option>
<option value="australia">Australia</option>
</select>
<select id="second">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="japan">Japan</option>
<option value="australia">Australia</option>
</select>
Javascript
$('select').change(function() {
$(this).siblings('select').find('option').attr({disabled:false});
var thisIndex = parseInt($(this)[0].selectedIndex)+1;
$(this).siblings('select').find('option:nth-child('+thisIndex+')').attr({disabled:true});
});