Is there a better way to do this jquery code?
Currently I have to use PHP to insert the starting position of the jquery code.
The code is for a country/state list.
If a user picks USA then a state dropdown list is below it, if any other country is selected, then it will show a different text input box and hide the dropdown list.
Now if a user has a country saved into the database already and they are on a page to edit this value, then I have to use PHP to show which should be shown first, either the USA states or the state input.
When a user signs up, by default the USA state list is shown, only if they choose a non usa country if the state list changed to a state input instead.
Hope I made sense. the ultimate goal is to somehow make it completely javascript/jquery and not rely on PHP to set anything
country dropdown list
<select name="country" id="country" class="textarealong signup_good"/>
<option value=1001>Choose a Country</option>
<option value=238>Zimbabwe</option>
...
</select>
USA state dropdown list
<select name="usstate" id="usstate" class="textarealong signup_good"/>
<option value=1001>Choose a State</option>
<option value=238>Florida</option>
...
</select>
NON-USA state INPUT box
<input type="text" id="othstate" name="othstate" id="othstate" value="" class="textarealong signup_good" maxlength="100">
<?PHP
//fix jquery country/state list based on there current saved country/state
if($_SESSION['member_info']['country'] == 224){
//$jquerycountry = "$('#othstate').hide().attr(\"disabled\", \"disabled\");";
$jquerycountry = "$('#othstate').hide().val('');";
}else{
$jquerycountry = "$('#usstate').hide().attr(\"disabled\", \"disabled\");";
}
?>
<script>
$(document).ready(function() {
locationlist();
});
function locationlist() {
<?PHP echo $jquerycountry; // includes country jquery code from above ?>
$('#country').change(function () {
var val = $(this).val();
if (val == 224) {
$('#usstate').val('').show().removeAttr("disabled");
$('#othstate').hide().val('');
} else {
$('#usstate').val('').hide().attr("disabled", "disabled");
$('#othstate').show().removeAttr("disabled");
}
});
}
</script>
Maybe this could be a fine solution for you:
HTML selection and input fields:
<select name="country" id="country" class="textarealong signup_good"/>
<option value=1001>Choose a Country</option>
<option value=238>Zimbabwe</option>
<option value=239>Rwanda</option>
</select>
USA state dropdown list
<select name="usstate" id="usstate" class="textarealong signup_good"/>
<option value=1001>Choose a State</option>
<option value=238>Florida</option>
</select>
<input id="otherstate"/>
Then the jQuery part with some 'ready'-magic: hide the otherstate field and trigger the change event on loading like Greg mentioned already. This makes sure, that if a country was already selected on page load the right form field will be selected:
<script type="text/javascript">
$(document).ready(function () {
$("#otherstate").hide();
$("#country").trigger("change");
});
$("#country").change(function () {
if ($("#country").val() != '1001') {
$("#usstate").hide();
$("#otherstate").show();
} else {
$("#usstate").show();
$("#otherstate").hide();
}
});
</script>
Hopes this will help you! ;)
Could you just do this in ready()?
loctionlist();
$('#country').trigger('change');
(Coming at it from a different angle entirely:)
With all the options/possibilities you're describing here, it might be wise to scrap all the drop-downs and just use an autocomplete text field.
Related
I have php search filter page in which i used different fields..One of them is Age of youngest driver field which have 2 values like 21-24 and 25+ ..Basically i only want to keep the selected values in the select box even after search so user know what he searched for ..What happen now when i select 25+ from the field and click on search it goes back to 21-24 . But As page is connected top database so it picks values from database which i enter like the database screenshot below Please check and let me know if it have any solution
<select class="half" id="form_frame" name="" onsubmit="getData(this);"/>
<option id="value1" value="21-24" selected="selected">21-24</option>
<option id="value2" value="25+">25+</option>
</select>
When i slect all values from the field and selct 25+ from driver age field then url is
http://localhost/Vehicle2/?car_type=0&pickup=0&return=0&residence_two=International&driver_age=25%2B&passengers=No.+of+Passengers&search=
http://localhost/Vehicle2/?car_type=0&pickup=0&return=0&residence_two=International&driver_age=21-24&passengers=No.+of+Passengers&search=
I want driverage=21-24 instead of driver_age
Apply onchange event in javascript like as follows if you are using jquery
jQuery(function () {
jQuery("#form_frame").change(function () {
var id = jQuery(this).val();;
location.href = "http://localhost/php-page/option"+id+"="+id
})
})
Simple use change event handler for selectbox
$(document).on('change','#form_frame',function(){
//window.location.href="http://localhost/php-//page/option"+$(this).//val()+"="$(this).val();
console.log("http://localhost/phppage/option"+$(this).val()+"="+$(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="half" id="form_frame" name="">
<option value="10" selected="selected">10</option>
<option value="11">11</option>
</select>
Im working on a small web application that follows a basic MVC pattern. currently i have data being pulled from a database into an array and im echoing out that value into one textbox (for now)
<input type="text" name="txtGasConRes" id="gasConRes" value="<?php echo $typical; ?>" disabled/>
The $typical value is being retrieved from my Model class into my Controller class and then echoed into the textbox for gas. I have a very similar textbox for Electricity.
My dropdown is as follows:
<select name="heatingType" id="heatingType" required>
<option value="" disabled selected>Select Your Option</option>
<option value = "Gas">Gas</option>
<option value = "Electricity">Electricity</option>
<option value = "Other">Other</option>
</select>
I was wondering if theres a way to determine where to echo the $typical value. So for example, If the user selected Gas as their heating type, then once the form was submitted it, the value would appear in the Gas textbox. If the user selected Electricity from the dropdown then the value would be echoed out into the Electricity textbox.
Any information will be appreciated.
Thanks!
This is definitely a job for JavaScript or jQuery if you prefer. You need to setup a handler to detect a change event on the select input. Once the user selects a value, then you can update the text field with text based on what the user has selected.
Not tested but something like this should work.
var select = document.getElementById('heatingType');
function updateTextField(selectedValue) {
switch(selectedValue){
case 'Gas':
return 'some text that you want in the text field';
break;
// etc.
}
}
select.addEventListener('change', function(){
var selectedValue = this.value,
textField = document.getElementById('gasConRes');
textField.value = updateTextField(selectedValue);
});
Create javascript function and put function in select box onchange event.(following code is working)
<script>
function heatingType(obj){
alert(obj.options[obj.selectedIndex].value); //if you want to show in alart
//or put in a variable
$x=obj.options[obj.selectedIndex].value;
}
</script>
<select name="heatingType" id="heatingType" onChange="heatingType(this)" required>
<option value="" disabled selected>Select Your Option</option>
<option value = "Gas">Gas</option>
<option value = "Electricity">Electricity</option>
<option value = "Other">Other</option>
</select>
so I want to direct users to different pages based off their choice of 2 different down down menus
so if we have:
Menu 1
girl
boy
and
Menu 2
blue
green
yellow
red
I'd like to redirect to different pages if they choose girl + blue, boy+ blue, and boy+green,etc. for all combinations. Thanks so much for any help! I tried searching of course but couldn't find how to deal with this rather simple situation.
here's what I have so far:
<td><span class="style1">You're a:</span><br>
<select name="select1" id="select1" onchange="getACupOfCoffee(this)">
<option selected="selected">-- Please Select --</option>
<option value="1">Male seeking Female</option>
<option value="2">Female seeking Male</option>
<option value="3">Male seeking Male</option>
<option value="4">Female seeking Female</option>
</select>
</td>
</tr>
<tr>
<td><img src="http://cdn.qtxi.com/dm/datespace/_spacer.gif" height="35" width="300"></td>
</tr>
<tr>
<td><span class="style1">Relationship I'm looking for:</span><br>
<select name="select2" id="select2">
<option>-- Please Select --</option>
<option value="1b">Casual</option>
<option value="2b">Serious / Longterm</option>
<option selected="selected" value="3b">Friend</option>
<option value="4b">Nothing</option>
</select></td>
</tr>
<tr>
</tr>
<tr>
<td><input type="image" src="continue2.jpg" width="149" height="37" border="0" /><a onclick="internalLink=true" href="redirect-30074.php"><img src="free.gif" alt="continue" /></a></td>
Your html requires a form tag and a submit button that directs to a php page that decides what to do.
Lets say you did that and called the php page decision.php .
In decision.php you then compare the values you received from the form.
You receive the info in either $_POST or $_GET variables depending on which html form tag you used.
Sanitize your variables you received and then use a php case statement.
switch ($_POST['select1'].$_POST['select2']) {
case "11b":
$redirectLocation = "MaleseekingFemaleCasual.html";
case "12b":
$redirectLocation = "MaleseekingFemaleSerious.html";
ect.
header ("Location: ".$redirectLocation);
Hope it helps.
Since i see a onchange function and there is no form i'm going to assume you use javascript
I personaly use jQuery when dealing with javascript so ill use it in my awnser aswell, if you don't use jquery. the method should still point in the right direction.
give ur continue button a id
$('#continue').click(function(){
if( $('#select1').val() == "1" ){
//male seeking female
if( $('#select2').val() == "1b"){
//casual in male seeking female
window.location = "1/1b/page.html";
}
}
if( $('#select1').val() == "2" ){
//female seeking male
if( $('#select2').val() == "1b"){
//casual in female seeking male
window.location = "2/1b/page.html";
}
}
});
and repeat.... hope it helps
You can use jQuery for this , for ex.
$(document).ready(function(){
$("#select2").change(function(){
var selA=$("#select1 option:selected").val();
var selB=$("#select2 option:selected").val();
document.location.href="http://your_url_here/"+selA+selB;
});
});
This code will make a redirect to your url using the values in the select option if the select2 changes
You need to go through the client side scripting to redirect user somewhere.
Add onchange event to your select2 id. This would be something like <select name="select2" id="select2" onchange="gotourl();">
Now, right your custom function in Clientside language that you can easily use. Code would take the value from your first drop down, generate url and redirect. Code would be something like this:
function gotourl() {
var choice_one = $('#select1').val();
var choice_two = $('#select2').val();
// MVC based side pattern
window.location = "mywebsitename.com/".choice_one.'/'.choice_two;
// CUSTOM PAGES BASED SITE.
switch(choice_two) {
case 'Casual':
switch(choice_one) {
case 'Male seeking Female':
window.location = "mywebsitename.com/".choice_one.'/'.choice_two;
break;
//ANOTHER CASE FROM BELOW....
}
break;
// ANOTHER CASE from ..
}
}
I tried a lot but code is not going into the { } block.
Try this code from your editor.
You could create a javascript that gets called when a user click on an item from the second dropdown list?
Something like:
<select onchange="MyFunc(this.options[this.selectedIndex].value)">
<option value="">Choose a destination...</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
</select>
And in the function you get the value from the first DropDown list.
EDIT:
In the MyFunc-function just check the value of the first dropdown and compare it to the value that you take in as a parameter in the function.
var e = document.getElementById("select1");
var result = e.options[e.selectedIndex].text;
//Or var result = e.options[e.selectedIndex].value;
You then have both the values and can determine where to redirect the user.
Refer to this: Get selected value in dropdown list using JavaScript?
Please try this code as an example
<html>
<head>
<script>
function openPage(){
var a=document.getElementById("drop1").value;
var b=document.getElementById("drop2").value;
if (a==1 && b==1){
window.location.href = "a.html";
}
if (a==1 && b==2){
window.location.href = "b.html";
}
}
</script>
</head>
<body>
<select name="dropdown" id="drop1">
<option value="1">Diesel</option>
<option value="2">Gas petrol</option>
</select>
<select name="dropdown" id="drop2">
<option value="1">Auto</option>
<option value="2">Heavy Duty trucks</option>
<option value="3">light Duty trucks</option>
</select>
<button onclick="openPage()">Search</button>
</body>
</html>
Hmm...this question may sounds silly , hope you all don't mind...
If I have a drop list:
<select name="myoption" onchange="document.textbox.value=this.value">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<input type="text" name="textbox" id="textbox">
So with this now the textbox will display what is selected , but is it possible to display the A,B,C instead of 1,2,3?
Actually I need a drop-list which will display 2 different values to 2 textbox,such as if A is selected,textbox1 will display "A" and textbox2 will display "1".
I don't know if it is possible and I tried for some times already...can someone give me some hints?
Thanks in advance.
Yes, it is possible. I would just externalize all javascript into a separate file to avoid mixing markup and scripts.
So the script:
// subscribe for the DOM ready event to ensure that you
// are manipulating the DOM only when it is loaded
window.onload = function() {
// subscribe for the onchange event of the dropdown
document.getElementById('myoption').onchange = function() {
// fetch the text of the currently selected element
var text = this.options[this.selectedIndex].innerHTML;
// and assign it to the corresponding input
document.getElementById('textbox').value = text;
};
};
and the markup:
<select name="myoption" id="myoption">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<input type="text" name="textbox" id="textbox" />
and a live demo.
I am developing an application where I am using country (dropdown list) and state (dropdown list).
The question is when 1 select country, it's states will be populated in state dropdownlist but the state list is only provided for USA and other countries are not provided so they have to type in.
So how to change state input type (TEXTBOX OR DROPDOWNLIST) based on USA (country).
That is
If USA -DROPDOWNLIST Else Other Countries -TEXTBOX
I would make is a select box by default, then use this jQuery to alter it.
if($(#country_select).val() != 'USA'){
$('#selectbox').attr('type', 'textbox');
$('#selectbox option').remove();
}
NOTE
This is a very simple solution, and requires that the default value is USA. I think there is enough info there to build a more robust solution.
Hi ON the country drop down list attach an event onchange and switch over the selectedValue if it is one of the countries isnt have a city hide the drop down list and show the textbox and vise versa .
<select id="country" onchange="check(this)">
<option value="Select">Select</option>
<option value="USA">USA</option>
<option value="EG">EG</option>
</select>
<select id="s_city">
</select>
<input type="text" id="t_city" style="display:none;">
Script
function check(s)
{
document.getElementById('t_city').value ='';
document.getElementById('s_city').innerHTML='';
var c= s.options[s.selectedIndex].value;
//Here You Can Start Loading your states with each country
if(c !="USA")
{
document.getElementById('t_city').style.display='block';
document.getElementById('s_city').style.display='none';
}
else
{
document.getElementById('t_city').style.display='none';
document.getElementById('s_city').style.display='block';
}
}
I Wish This example helped out
I would have a dropdownlist of states and a textbox for other countries.
HTML
<select id="cboCountry">
<!-- <option value="USA">USA</option> -->
<!-- etc -->
</select>
<select id="cboState">
<!-- <option value="Alabama">Alabama</option> -->
<!-- etc -->
</select>
<input type="text" id="txtState" style="display:none;" />
Javascript
$('#cboCountry').change(
if($('#cboCountry').value() != 'USA'){
$('#txtState').show();
$('#cboState').hide();
}
else
{
$('#txtState').hide();
$('#cboState').show();
}
);
The idea here is that you will need a drop-down for the state and also a text box. If you prefer to display the drop down first then don't display the text box, or inter change.
Though my solution is pretty similar to Marvan, just take a look a live example here
http://jsfiddle
for other reference. If your going to send this along with form you, I guess the name must be same.
Using the DOM/Inner HTML Method you can achieve that.
innerHTML method without JQuery
<select id='country_select'>
<div id='container'>
<input ...>
</div>
if (document.getElementById ("country_select").value != "USA")
{
document.getElementById("container").innerHTML = "<input ...>";
}
else
document.getElementById("container").innerHTML = "<select ...>";