I have created the dropdown list in my index page i want to select the one value from that list and validate it if not selected any value the code for that is as follows:
<form action="" method="post">
<select value="state" name="state">
<option selected="">---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php code for the above file is as follows:
<?php
if(!empty($_POST['state'])) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
I dont want to be select the first option in selection list please enter to be selected but the code which I have used is taking that value also I want relevant code how to validate that list?
I prefer adding a disabled attribute to the placeholder option, that way, a user has to choose something if they click the drop-down:
<select value="state" name="state">
<option selected disabled>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
Quick demo: http://jsfiddle.net/hxxJZ/
something like this should to the trick:
<form action="" method="post">
<select name="state">
<option value="0" selected>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php
<?php
if( 0 != $_POST['state'] ) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
your HTML is not right,
try edit this line
<option selected=""...
to this one
<option selected value="">---
select elements do not have an (HTML) value attribute, so remove value="state".
The selected attribute should be selected="selected" (or just SELECTED).
Related
I am trying to get drop down select value posted upon send.
Markup
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
php
$selectOption = $_POST['taskOption'];
{
$message.=$value.'<br>';
}
$message.='
What am I doing wrong?
Is this what you're trying to do?
if(!empty($_POST['taskOption']))
{
$value = $_POST['taskOption'];
$message.=$value.'<br>';
}
you need to use post in your form, method="post"
if(isset($_POST['submit'])){
$dropdwon = $_POST['taskOption'];
//code here
}
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 this form:
<form name="summaryform">
<select name="category" id="category" style="width:500px;">
<option value="">Choose category...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
</select><br>
<select name="subcategory" id="subcategory" style="width:500px;">
<option value="">Choose sub category...</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
<option value="Scheme">Scheme</option>
</select><br>
<input type="text" name="comments" id="comments" value=" Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">
On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments")
How do I pass the values from this form to another form?
Let's say below is the form I want to pass the values to:
<div>
<p>
<form name="summaryform" method='POST' action='process.php'>
<div style="font-size:12pt;">
value from one dropdown
value from the other dropdown
value from textbox
</form>
</p>
</div>
jQuery Method
If the forms are on the same page, you can use jQuery to get the values and write to the div:
var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);
PHP Method
If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for.
NOTE: for this method to work the page being POSTed to MUST be PHP. You will also need to add an action and method in the first form to POST to the second form.
This would be the code in the form on the page being POSTed to:
<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
A value can be passed between forms using the POST and GET methods. The first form (Which is sending the values) does not have this defined, so modify the first form to this -
<form name="summaryform" method='POST' action='process.php'>
Then, in process.php, put the following php code -
$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];
After that, you can just use the variables as you see fit!
If you want just print the selected values from first form #zsaa14's
answer is good
If you want to print whole select list, autoselected your value, heres a code.
foreach($categories as $category) {
$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';
echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;
}
NOTE: the variable names and keys are for example, use Yours. This is only fragment of code.
You can use post data fields to send the values to the next form
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>
<form method="get" action="index.php">
<select name="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">C</option>
</select></form>
The code above displays a dropdown menu of options, such as a, b, or c, the user can click on. After the user clicks on a, b, or c from the drop down menu, that option is submitted in the form, and the page reloads with a new $_GET variable "choice" defined as a, b, or c. I want the option that the user last selected to now be the default selected option listed. For example, after the user clicks on "b" from the dropdown menu, "b" shows up as selected on the drop down menu list. What's the best way to accomplish this?
Here you go:
<?php
if (isset($_REQUEST['choice'])) {
$selected_choice = $_REQUEST['choice'];
}
else {
$selected_choice = "none";
}
?>
<html>
<head>
</head>
<body>
<form method="get" action="">
<select name="choice" size="1" onchange="this.form.submit()">
<option value="a" <?php if($selected_choice == "a"){ print "selected='selected'"; } ?> >a</option>
<option value="b" <?php if($selected_choice == "b"){ print "selected='selected'"; } ?> >b</option>
<option value="c" <?php if($selected_choice == "c"){ print "selected='selected'"; } ?> >C</option>
</select>
</form>
</body>
</html>
When outputting the form do a check like:
<option value="a"<?php if ($_GET['choice'] == "a") {?> selected <?php } ?>>a</option>
You'd then want to extract that into a function that either does the conditional code for you, or outputs the entire option element with the provided value, label, and sanitized data with the check done internally.
Another approach to avoid spaghetti code (or at least reduce it) is to set it through Javascript, like:
<html>
<body>
<form method="get" action="test.htm">
<select name="choice" id="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</form>
<script>
document.getElementById('choice').value = 'b';//replace with something like: <?php echo $_GET['choice'] ?>
</script>
</body>
</html>
IMHO this approach is better than put a test in each option tag.
Try saving the posted select option in session like below
<?php $_SESSION['current_select'] = $_REQUEST['posted_select_option']?>
And in page load check as below
<script>
var currentSelect = <?php echo $_SESSION['current_select']; ?>
$(document).ready(function(){
if(currentSelect!="")
$("#choice").val(currentSelect);
});
</script>
Considering your select tag has id like below
<select name="choice" id="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">C</option>
</select>