Forum Moderators: open

Message Too Old, No Replies

Javascript and select box

         

uNrEaLiTy

4:38 am on Jun 29, 2010 (gmt 0)

10+ Year Member



I have a homepage with a select box, whereby the user selects a country.

When the user selects the country, the page will refresh/load with the country page.

What i want to do is to have a direct link to access the country page. (The link will be used on external websites so as to directly access the country page)

I am trying to use a link such as www.domain.com?selected=2

in this case the brazil page should load.

but for some reason it is not loading. any idea why? any solutions to it?

Your help is greatly appreciated.


 <select id="country" onchange="setOperatorLogo(); setcountry(document.getElementById('country').value);">
<?php $selected = isset($_GET['selected']) ? $_GET['selected'] : ""; ?>
<option class="dropdown" value="000">Select Recipient Country&nbsp;&nbsp;</option>
<option <?php echo $selected == '1' ? 'selected="selected"':''?> value="880">(+880) &nbsp; Bangladesh</option>
<option <?php echo $selected == '2' ? 'selected="selected"':''?> value="55" onchange="setOperatorLogo(); setcountry(document.getElementById('country').value);"> (+55) &nbsp;&nbsp;&nbsp; Brazil</option>
<option value="86" > (+86) &nbsp;&nbsp;&nbsp; China</option>
<option class="dropdown" value="20"> (+20) &nbsp;&nbsp;&nbsp; Egypt</option>
<option class="dropdown" value="91"> (+91) &nbsp;&nbsp;&nbsp; India</option>
<option class="dropdown" value="62"> (+62) &nbsp;&nbsp;&nbsp; Indonesia</option>
<option class="dropdown" value="962">(+962) &nbsp; Jordan</option>
<option class="dropdown" value="7"> (+7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Kazakhstan</option>
<option class="dropdown" value="856">(+856) &nbsp; Laos</option>
<option class="dropdown" value="60"> (+60) &nbsp;&nbsp;&nbsp; Malaysia</option>
<option class="dropdown" value="52"> (+52) &nbsp;&nbsp;&nbsp; Mexico</option>
<option class="dropdown" value="977">(+977) &nbsp; Nepal</option>
<option class="dropdown" value="92"> (+92) &nbsp;&nbsp;&nbsp; Pakistan</option>
<option class="dropdown" value="63"> (+63) &nbsp;&nbsp;&nbsp; Philippines</option>
<option class="dropdown" value="7"> (+7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Russia</option>
<option class="dropdown" value="94"> (+94) &nbsp;&nbsp;&nbsp; Sri Lanka</option>
<option class="dropdown" value="84"> (+84) &nbsp;&nbsp;&nbsp; Vietnam</option>
</select>

subexpression

5:10 am on Jun 29, 2010 (gmt 0)

10+ Year Member



Hi uNrEaLiTy,

Remove the onchange="" attributes from any <option> elements.
Modify your <select> element like this:
<select id="country" onchange="setOperatorLogo(); setcountry(this.options[this.selectedIndex].value);">


- subexpression

uNrEaLiTy

6:00 am on Jun 29, 2010 (gmt 0)

10+ Year Member



Hi subexpression...

I modified it as you said... but it is not working

<select id="country" onchange="setOperatorLogo(); setcountry(this.options[this.selectedIndex].value);"> 

<?php $selected = isset($_GET['selected']) ? $_GET['selected'] : ""; ?>
<option class="dropdown" value="000">Select Recipient Country&nbsp;&nbsp;</option>
<option value="880">(+880) &nbsp; Bangladesh</option>
<option value="55"> (+55) &nbsp;&nbsp;&nbsp; Brazil</option>
<option value="86" > (+86) &nbsp;&nbsp;&nbsp; China</option>
<option class="dropdown" value="20"> (+20) &nbsp;&nbsp;&nbsp; Egypt</option>
<option class="dropdown" value="91"> (+91) &nbsp;&nbsp;&nbsp; India</option>
<option class="dropdown" value="62"> (+62) &nbsp;&nbsp;&nbsp; Indonesia</option>
<option class="dropdown" value="962">(+962) &nbsp; Jordan</option>
<option class="dropdown" value="7"> (+7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Kazakhstan</option>
<option class="dropdown" value="856">(+856) &nbsp; Laos</option>
<option class="dropdown" value="60"> (+60) &nbsp;&nbsp;&nbsp; Malaysia</option>
<option class="dropdown" value="52"> (+52) &nbsp;&nbsp;&nbsp; Mexico</option>
<option class="dropdown" value="977">(+977) &nbsp; Nepal</option>
<option class="dropdown" value="92"> (+92) &nbsp;&nbsp;&nbsp; Pakistan</option>
<option class="dropdown" value="63"> (+63) &nbsp;&nbsp;&nbsp; Philippines</option>
<option class="dropdown" value="7"> (+7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Russia</option>
<option class="dropdown" value="94"> (+94) &nbsp;&nbsp;&nbsp; Sri Lanka</option>
<option class="dropdown" value="84"> (+84) &nbsp;&nbsp;&nbsp; Vietnam</option>
</select>

uNrEaLiTy

6:02 am on Jun 29, 2010 (gmt 0)

10+ Year Member



i am using this:

<select id="country" onchange="setOperatorLogo(); setcountry(this.options[this.selectedIndex].value);">

<?php $selected = isset($_GET['selected']) ? $_GET['selected'] : ""; ?>
<option class="dropdown" value="000">Select Recipient Country&nbsp;&nbsp;</option>
<option <?php echo $selected == '1' ? 'selected="selected"':''?> value="880">(+880) &nbsp; Bangladesh</option>
<option <?php echo $selected == '2' ? 'selected="selected"':''?> value="55"> (+55) &nbsp;&nbsp;&nbsp; Brazil</option>
<option value="86" > (+86) &nbsp;&nbsp;&nbsp; China</option>
<option class="dropdown" value="20"> (+20) &nbsp;&nbsp;&nbsp; Egypt</option>
<option class="dropdown" value="91"> (+91) &nbsp;&nbsp;&nbsp; India</option>
<option class="dropdown" value="62"> (+62) &nbsp;&nbsp;&nbsp; Indonesia</option>
<option class="dropdown" value="962">(+962) &nbsp; Jordan</option>
<option class="dropdown" value="7"> (+7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Kazakhstan</option>
<option class="dropdown" value="856">(+856) &nbsp; Laos</option>
<option class="dropdown" value="60"> (+60) &nbsp;&nbsp;&nbsp; Malaysia</option>
<option class="dropdown" value="52"> (+52) &nbsp;&nbsp;&nbsp; Mexico</option>
<option class="dropdown" value="977">(+977) &nbsp; Nepal</option>
<option class="dropdown" value="92"> (+92) &nbsp;&nbsp;&nbsp; Pakistan</option>
<option class="dropdown" value="63"> (+63) &nbsp;&nbsp;&nbsp; Philippines</option>
<option class="dropdown" value="7"> (+7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Russia</option>
<option class="dropdown" value="94"> (+94) &nbsp;&nbsp;&nbsp; Sri Lanka</option>
<option class="dropdown" value="84"> (+84) &nbsp;&nbsp;&nbsp; Vietnam</option>
</select>

uNrEaLiTy

6:12 am on Jun 29, 2010 (gmt 0)

10+ Year Member



the link i am using to access the country page is:

www.domain.com/?selected=2

is it correct?

thanks for your help

subexpression

7:32 am on Jun 29, 2010 (gmt 0)

10+ Year Member



uNrEaLiTy,


Can you show us the setcountry() function?
Does it use some sort of window.location.href?
I'm thinking that once you send the index to setcountry(), you're probably putting that in the query string, am I correct?

Is the country page different than the home page where the user selects the country?
<script type="text/javascript">
function setcountry(val){
window.location.href = 'countrypage.php?selected=' + val;
}
</script>


But, if the home page and country page are one and the same, then you're just reloading the same page:
window.location.href = '?selected=' + val;


Once the page reloads, the query string must be parsed for that particular option to be selected:

<script type="text/javascript">
function optionsbyvalue(){
var loc = window.location.href,
keyval = loc.slice(winloc.indexOf('?') + 1).split('='),
country = document.getElementById('country'),
opt = country.options,
i = options.length;
while(i){
if(opt[--i].value == keyval[1]){
country.selectedIndex = i;
i = 0;
}
}
}
</script>


If you wanted to display the selected country, you'd have to add more after the while() loop:
var countryname = document.getElementById('countryname');
countryname.innerHTML = country.options[country.selectedIndex].text;


Then, the element with the id="countryname" would contain the text node of the selected option after a page load:
<h3 id="countryname"></h3>

uNrEaLiTy

7:50 am on Jun 29, 2010 (gmt 0)

10+ Year Member



the setcountry() function is below

function setcountry(code){
document.getElementById("phone").value = '+' + code;
document.getElementById("selectCountry").style.display="none";

document.getElementById("phonenumberdiv").style.display="block";
}







function setOperatorLogo(){
var countCodeArr = new Array();
countCodeArr['880']='Bangladesh';
countCodeArr['55']='Brazil';
countCodeArr['86']='China';
countCodeArr['20']='Egypt';
countCodeArr['91']='India';
countCodeArr['62']='Indonesia';
countCodeArr['962']='Jordan';
countCodeArr['7']='Kazakhstan';
countCodeArr['856']='Laos';
countCodeArr['60']='Malaysia';
countCodeArr['52']='Mexico';
countCodeArr['977']='Nepal';
countCodeArr['92']='Pakistan';
countCodeArr['7']='Russia';
countCodeArr['63']='Philippines';
countCodeArr['94']='Sri Lanka';
countCodeArr['84']='Vietnam';
countCodeArr['44']='UK';
var currCountry = document.getElementById("country").value;

uNrEaLiTy

7:56 am on Jun 29, 2010 (gmt 0)

10+ Year Member



subexpression, is there any way i can PM you?

uNrEaLiTy

10:46 am on Jun 29, 2010 (gmt 0)

10+ Year Member



any idea what's wrong?

uNrEaLiTy

1:58 am on Jun 30, 2010 (gmt 0)

10+ Year Member



please help me