I try to make a list of data from a table in a database. I do this with an select option, because the user is only allowed to choose one.
I do it on this way:
<select name="test">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php" onClick="window.location.href='other.php'">
Other
</option>
</select>
This works in IE and Firefox, but not in Google Chrome.
It's not an option to put the onClick in the <select>-tag, because they don't have a href and I need their values to get the primary key and store something in the table.
I hope you can help me.
UPDATE: this is the solution for my case, solved by Harry
<select name="test" onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
Try the below code and check if it solves your problem. You needn't have the href values in the select tag for this to work. The href can be in your option's value tag.
UPDATE: The below code would do the redirect only if the value of the selected option tag has .php in it. For the first two options, no action will be taken.
<select name="test"
onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
this isn't valid HTML. it should at least be:
<option value="other.php" onClick="window.location.href='other.php'">
try this
<select name="test"
onchange="var val = this.value;
if (val.indexOf('other.php')!=-1) window.location=loc">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
Related
I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>
I have a question. Let me explain this with an example.
I have this piece of code:
<form action="vehicles.php" method="get">
<span>Marca:
<select name="brand">
<option value="null" selected="selected"></option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="null" selected="selected"></option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Is any way to prevent the assignment of those variables if the option selected is the "null" one?
If, for example, I select brand="Renault" and model="null" the url is
http://mywebpage.com/vehicles.php?brand=Renault&model=null
but it should be
http://mywebpage.com/vehicles.php?brand=Renault
I know how to unset the variables with "null" value after the form submission with PHP. Is any way to do it before the submission and after the variables are setted to "null".
I would like a cleaner url, free of "variable=null" results.
P/D: I don't have a native english speaking so feel free to edit my question. I wish you understand me.
I am afraid that you might need javascript to achieve what you want.
Take a look at this code:
<form action="vehicles.php" id="carForm" method="GET">
<span>Marca:
<select name="brand" id="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model" id="model">
<option value="none" selected="selected">-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="button" value="Submit" onclick="submitFormFilter();" />
</form>
Javascript : code will be like this:
<script>
function submitFormFilter(){
var myForm = document.getElementById("carForm");
var carBrand = document.getElementById("brand");
var carModel = document.getElementById("model");
if(carBrand.value === "none" || null){
carBrand.parentNode.removeChild(carBrand);
}
if(carModel.value === "none" || null){
carModel.parentNode.removeChild(carModel);
}
myForm.submit();
}
</script>
http://jsfiddle.net/7xzKP/1/
you can it try here.
Include the value attribute of your option tags.
<option value="" selected="selected">-</option>
You should really do this for all of your options.
if you don't want to use $_GET superglobal because of the url extension that it comes with try using $_POST instead. It will not have a url extension and it can still store values that you can later retrieve. Just be sure to change your method to equal POST instead of GET.
So the code for the form tag would change to:
<form action="vehicles.php" method="POST">
And you can later access it by (for example):
echo $_POST['brand'];
or
echo $_POST['model'];
as well, you probably want to add a value param to the values that you have in your option tag.
EDIT-
I've added this new section since you don't want to use POST even though I think you should.
You can stay with the GET method by doing this line of code:
<form action="vehicles.php" method="GET">
<span>Marca:
<select name="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="none" selected>-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Let me know if that helps
You should set the value for the option tag of <select> :)
You's missing set value for option tag. you should set the value for option tag in select tag. With the blank value, you can assign it to a special value like zero(0)
<select name="model">
<option value="0" selected>-</option>
<option value="1">206</option>
<option value="2">Suran</option>
<option value="3">Passat</option>
<option value="4">Punto</option>
</select>
EDIT:
Ok. I got your point. You can import jquery to your page and make a check by javascript before you submit, if the value of select box is blank. you can remove it.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$.ready(function(){
$("form").submit(function(){
if($("select[name='model']").val() == "0") $("select[name='model']").remove();
return true;
});
})
</script>
i'm having the following html markup:
<select id=gender>
<option value=''>Please select</option>
<option value='m'>male</option>
<option value='f'>female</option>
</select>
i want to set the value using the simple html dom parser - here's my code - which doesn't work:
$combo = $el->find("#gender",0);
$combo->value = "m";
i also tried $combo->setAttribute('value', 'm'); without success
any ideas?
thanks
<select> has no value attribute. What you need to do is find the option you want selected (e.g. something like #gender/option[value='m']) and set the selected attribute on that.
In order to set the selected option out of a <select>, you need to set selected on the corresponding <option>:
<select id=gender>
<option value=''>Please select</option>
<option value='m' selected>male</option>
<option value='f'>female</option>
</select>
So I have an interesting issue. I have a <select> menu, and it is all working as I would like with one exception. When I hover over any of the <options>, the highlighted option skip from the option I am hovering over, to the top value (and it seems to make no difference whether it is marked as 'selected,' or not). When I click it selects the correct option, not the highlighted one. It works correctly in Firefox, but not in Chrome.
Can anyone explain to me why it behaves like this, and what I might do.
Thanks!
<select name="grade" id="grade" value="" >
<option value="4" selected >A</option>
<option value="3.7" >A-</option>
<option value="3.4" >B+</option>
<option value="3" >B</option>
<option value="2.7" >B-</option>
<option value="2.4" >C+</option>
<option value="2" >C</option>
<option value="1.7" >C-</option>
<option value="1" >D</option>
<option value="0" >E</option>
</select>
Here is the only time I use this in my JavaScript. And as I said, it is only not functioning in google-chrome.
var in_gpa = Number($('#grade').val());
I had exactly same problem in Chrome Only!
Problem: Chrome extention:FastestChrome - Browse Faster 6.9.7
You are self closing the select and option tag, try this and see, moreover you can't set a value for <select> tag.
<select name="grade" id="grade">
<option value="4" selected>A</option>
<option value="3.7">A-</option>
<option value="3.4">B+</option>
<option value="3">B</option>
<option value="2.7">B-</option>
<option value="2.4">C+</option>
<option value="2">C</option>
<option value="1.7">C-</option>
<option value="1">D</option>
<option value="0">E</option>
</select>
Try disabling AdBlock when installed. This caused jumping options in dropdowns for me.
i want to know that how to get multiple selectbox values in php
means that i have more than 1 select box on a page and it can be increased when user click on addmeans that a page can contain numerous selectboxes now plz tell me how to get the values from that boxes and also how do i get know that how many select boxes were used by user
i think an array should be used but i dont know how??
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
</select>
like in above example
how do i get know that how many selectboxes were used and how to get value of each box
code for adding the new dropdown
function addRow()
{
var newRow = document.all("tblGrid").insertRow();
var oCell = newRow.insertCell();
oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
First of all you need to use unique id and name for each select box. Otherwise it is difficult to manage them after post.
So you can do it something like this:
<select name="myselect[]" id="select1">
<select name="myselect[]" id="select2">
<select name="myselect[]" id="select3">
After form post you can get all select box values:
print_r( $_POST['myselect'] );
For number of select boxes on page you can try:
echo count( $_POST['myselect'] );
Change the name attribute value in each tag to something unique like 'select1','select2','select3' . To refer these values in PHP use $_POST['select1'] and so on. OR use $_GET['select1'] incase of get method is used in the form.