Here is my code;
<select onchange="location = this.options[this.selectedIndex].value;">
<option value="">Select Something</option>
<option value="<?php echo site_url(); ?>">Home</option>
<option value="<?php echo site_url(); ?>publications">Publications</option>
<option value="<?php echo site_url(); ?>contact_us">Contact Us</option>
</select>
When an option is selected, that page will immediately load. However, the drop-down list does not "remember" that that option was selected. The drop-down list will just default back to Select Something. How can I get the list to display what was selected instead?
Before I began using Frameworks I might have tried something like this;
<option value="<?php echo site_url(); ?>contact_us" <?php if($value == "contact_us") {echo "selected"; } ?> >Contact Us</option>
And I would have created a $value using $_REQUEST['value'] or something. But I am using the CodeIgniter framework so I don't think I can do that anymore.
This may help you:
<?php if($value == $_SERVER["REQUEST_URI"]) {echo "selected"; } ?>
I suggest from your code that you have some php $value-es equal to every option value. So, you can compare it with web page URI
More about $_SERVER global
Related
I need one help.i am fetching data from my DB ,i need when data has fetched one Javascript function will called through onChange event.Let me to explain my code.
<?php
$id=$_GET['ids'];
if($id!=""){
$getcustomerobj = $dbobj->getFeedbackData($db,$id);
}
?>
<div style="width:24%; float:left; padding:10px;">Answer Type :
<select class="form-control" id="answer_type" name="answer_type" onChange="selectScale(this.value);">
<option value="">Select Answer Type</option>
<?php
$ustatus=array("status"=>'1');
$feeddata=$db->kf_answertype->find($ustatus);
foreach($feeddata as $v){
?>
<option value="<?php echo $v['_id']; ?>" <?php if($getcustomerobj->answer_type == $v['_id'] or $_REQUEST['answer_type'] == $v['_id']){ print 'selected'; } ?>><?php echo $v['answertype']; ?></option>
<?php } ?>
</select>
</div>
<div style="width:24%; float:left; padding:10px; display:none;" id="scaleid">Scale :
<select class="form-control" id="nscale" name="noofscale">
<option value="">Select No Of Scale</option>
<?php
$status=array("status"=>'1');
$feeddata=$db->kf_scale->find($ustatus);
foreach($feeddata as $v){
?>
<option value="<?php echo $v['_id']; ?>" <?php if($getcustomerobj->no_of_scale == $v['_id'] or $_REQUEST['no_of_scale'] == $v['_id']){ print 'selected'; } ?>><?php echo $v['noofscale']; ?></option>
<?php } ?>
</select>
</div>
<script>
function selectScale(id){
console.log('select scale',id);
var data=$.param({'op':'getscale','sid':id});
$.ajax({
method:'POST',
url:"dbcon/DBConnection.php",
data:data
}).done(function(msg){
//console.log('msg',msg);
var dedata=JSON.parse(msg);
//console.log('decode',dedata);
if(dedata[0]['data']==1){
document.getElementById("scaleid").style.display="block";
}
if(dedata[0]['data']==0){
document.getElementById("scaleid").style.display="none";
}
});
}
</script>
Here I need when data are fetching from DB the Answer Type drop down field will set with some value.As the second drop down list(i.e-Scale) is depends on first drop down, i need to call selectScale function which has triggered using onChange event.Please help me.
I think you can call onchange event for your first drop-down on document ready so based on this your second drop-down value will be selected.
$(document).ready(function(){
$("#answer_type").trigger("change");
});
may be this is helpful for you.
As what i have understood ,what you need is simply add this line to your java script
selectScale($('#answer_type').val());
Also this solution could work, by triggering change function using jQuery
$("#answer_type").trigger("change");
Am loading my user records from database to the table, Am using:
foreach ($userArray as $key => $value) {
I have isAdmin row which contains 'Y', 'N' data.I need to make dropdownlist from this row.If data in Db is 'Y' other dropdownlist option should be 'N' and v/v.
Am using <select id="<?php echo $userArray[$key]["isAdmin"]?>">
I tried something like that, but if my data is Y it gives two more options , Y and N, it should be N, how to repair that?Here is my code:
<select id="<?php echo $userArray[$key]["isAdmin"]?>">
<option value="<?php echo $userArray[$key]["isAdmin"]?>">
<?php echo $userArray[$key]["isAdmin"];
if (strcmp($userArray[$key]["isAdmin"],"Y")==0){
?></option>
<option value="N">N</option>
<option value="Y" style="display:none;">Y</option>
<?php
}
else
?>
<option value="Y">Y</option>
<option value="N" style="display:none;">N</option>
</select>
I found a solution, here is my code:
<option value="<?php echo $userArray[$key]["isAdmin"]?>">
<?php echo $userArray[$key]["isAdmin"];
if (strcmp($userArray[$key]["isAdmin"],"Y")==0){
?></option>
<option value="N">N</option>
<?php
}
if (strcmp($userArray[$key]["isAdmin"],"N")==0){
?>
<option value="Y">Y</option>
<?php
}
?>
</select>
Create a if condition in your nav bar checking isAdmin and then echo the other dropdownlist.
if($isadmin == "Y"){
echo '?>
//Put Html code here for admin
<?php ';
}else{
echo'?>
//Put Html code here for non admin
<?php';
}
I wont to keep the selected item of my menu after reload or in a different page with the samedrop menu.
This code works perfectly but i need to populate mi dropdownmenu with mysql.
document.getElementById("plist").onchange = function() {
localStorage.setItem('plist', document.getElementById("plist").value);
}
if (localStorage.getItem('plist')) {
document.getElementById("plist").options[localStorage.getItem('plist')].selected = true;
}
<select name="produttore" id="plist">
<option value="0">Audi</option>
<option value="1">BMW</option>
<option value="2">Alfa Romeo</option>
<option value="3">Aborth</option>
</select>
If i populate the menu with that code the local storage doesn't seem to work.
document.getElementById("plist").onchange = function() {
localStorage.setItem('plist', document.getElementById("plist").value);
}
if (localStorage.getItem('plist')) {
document.getElementById("plist").options[localStorage.getItem('plist')].selected = true;
}
<select name="produttore" id="plist">
<option value=" ">Seleziona produttore</option>
<?php
foreach($results as $produttore) {
?>
<option value="<?php echo $produttore["category_id"]; ?>"><?php echo $produttore["name"]; ?></option>
<?php
}
?>
</select>
KEEP CALM AND don't kill me I'm a noob
As miraco suggested the problem was the value.
I changed my code and using "selectedIndex" and not "value" it work.
I made,options in select box clickable. However for the very first option I din't put any link. But just when I click on the select box it redirects to somewhere and says page not found.
But if you click on the arrow and hold for a while then the dropdown appears.Now when u click any of the option it works. But I wonder why at first it doesn't shows the dropdown and just redirect.
here's the js fiddle: http://jsfiddle.net/58cqc812/
HTML
<select id="myselect">
<option>Go To ...</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/">Home</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/page/about-us">About Us ▾</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/page/the-centre-point-of-any-web-projects">Centre-Point of Web Projects</option>
<option>Branches ▾
<?php #Core::getHook('block-branches'); ?></option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/news">News</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/event">Events</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/contact">Contact Us</option>
</select>
JS
<script>
//$('#myselect').on('change', function() {
//location.href=$(this).data('url');
//});
document.getElementById("myselect").onclick = function(d){
window.location = this.value;
};
</script>
You can refer to the live site. You need to resize the browser to the smallest, then only this menu appears.It's meant for mobile version.
http://ymm.valse.com.my/
change on click event to change event .... it will fire when click the select box .
document.getElementById("myselect").onchange = function(d){
window.location = this.value;
};
i don't exactly know how on change work with JavaScript bcz i prefer jQuery
Using jQuery
$("#myselect").change(function(){
if($(this).val()!==""){
window.location = $(this).val();
}
});
I am using session variables to pass values from one page to another in php. I have passed a value from one page to another, now I want the passed value to be selected in dropdown menu.
Suppose dropdown select has 1,2,3,4,5 and when I pass 2 to the page containing dropdown, 2 should automatically get selected. How to do this?
Something like this will loop $x to 5 and check on each one if Session value is equal to it's value and then select it for you.
<select name="numbers" id="numbers" >
<?php for ($x=1; $x < 5; $x++): ?>
<option <?php if($_SESSION['value'] ==$x){echo "selected";} ?> value="<?php echo $x; ?>"><?php echo $x;></option>
<?php end for ?>
</select>
Roughly something like this should work, but for future reference please show more effort in your question i.e posting your code or some research attempts.
<label for ="Numbers"><span>Numbers</span>
<select name="numbers" id="numbers" >
<option value="" <?php if($_SESSION['value'] ==""){echo "selected";} ?>>Please Select</option>
<option value="1" <?php if($_SESSION['value'] =="1"){echo "selected";} ?>>1</option>
<option value="2" <?php if($_SESSION['value'] =="2"){echo "selected";} ?>>2</option>
</select>
</label>
<select>
<option <?php echo ($_SESSION['your-session'] == 1) ? "selected" : "" ?> value='1'>1</option>
<option <?php echo ($_SESSION['your-session'] == 2) ? "selected" : "" ?> value='2'>2</option>
<option <?php echo ($_SESSION['your-session'] == 3) ? "selected" : "" ?> value='3'>3</option>
</select>
printing "selected" on the option tag will solve your problem, you must check first if the value of the option is the value you stored in your session.