I've written this piece of code:
<select name="account_type" id="account" class="form-control" required>
<option value="Asset">Asset</option>
<option value="Bank">Bank</option>
<option value="Capital">Capital</option>
<option value="Cash" <?php if($_POST['Cash'] == $ledger_account){?> selected <?php }?>>Cash</option>
<option value="Expense">Expense</option>
<option value="Income">Income</option>
<option value="Liability">Liability</option>
<option value="Creditors">S. Creditors</option>
</select>
I got a value from different page, which is stored in $ledger_account. However, I want to see an option (for example, Cash) to be selected here when the page loaded if the option value matches with $ledger_account value. Please help me to identify what I've done wrong here.
Your code are correct. but it's give some error..
then try to somting like this...
<option value="Cash" <?php if($_POST['Cash'] == $ledger_account){ ?> selected="selected" <?php }?> >Case</option>
I am not sure but i think this useful for you
Related
I am using HTML + PHP to build a web App.
I wanted to create an editable form that includes a couple of <select> fields. As an example, we can assume that I want to create a user profile.
The thing is, when new user wants to edit some data about himself, all fields are empty. For the field sex it may look like this:
<select class="form-select" name="sex" required>
<option value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
or:
<select class="form-select" name="sex" required>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
Whereby default the empty value described by "-" will be displayed.
But what about when the user already has filled that field? Let's assume we have an object variable $user and may access his/hers sex by $user->sex. By default, I want the <select> to be selected with the proper attribute. Using PHP I may go for something like this:
<select class="form-select" name="sex" required>
<?php if ($user->sex == 'M') : ?>
<option value="">-</option>
<option selected value="M">Male</option>
<option value="F">Female</option>
<?php elseif($user->sex == 'F' : ?>
<option value="">-</option>
<option value="M">Male</option>
<option selected value="F">Female</option>
<?php else : ?>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
<?php endif; ?>
</select>
But that requires a lot of if, elseif statements. As an example, I've provided only three-optional field. What about when there are more options?
Is there any elegant solution to choose a selected option of <select>, based on a variable (object fetched from database / POST / GET / SESSION - does not really matter), without using so many ifs? At best, I'd like to list possible options only once and then tell the site which is to be chosen by default.
Thanks everyone in advance!
Why do you repeat the options? Just check every option if it needs to be selected...
<select class="form-select" name="sex" required>
<option <?php if($user->sex == "") echo "selected" ?> value="">-</option>
<option <?php if($user->sex == "M") echo "selected" ?> value="M">Male</option>
<option <?php if($user->sex == "F") echo "selected" ?> value="F">Female</option>
</select>
I'm using xampp PHP
How to do I get values from MySQL in select type like getting value in
<input type="text" value="<?php $variable['valueinsql']; ?>">
But I want the value to be inserted in
<select type>
And I tried
If(isset($_GET['id'])){
<p> Banks:</p><select multiple="multiple" name="otherbanks[]" style=" width:190;" value = "<?php $list["Banks"] ?>"">
<option value="Banksone">One</option>
<option value="Bankstwo">two</option>
<option value="Bankthree">three</option>
I want to see it automatically highlighted.
Use in_array to check the value available in array or not. If available echo 'selected';
Try this ==>
<select multiple="multiple" name="otherbanks[]" style=" width:190;">
<option <?php if (in_array('Banksone',$list["Banks"])) {echo 'selected';} ?> value="Banksone">One</option>
<option <?php if (in_array('Bankstwo',$list["Banks"])) {echo 'selected';} ?> value="Bankstwo">two</option>
<option <?php if (in_array('Bankthree',$list["Banks"])) {echo 'selected';} ?> value="Bankthree">three</option>
</select>
To highlight particular options the selected attribute should be set on each option that is applicable - as below example.
<select name='otherbanks[]' multiple=true>
<option value=1 selected>one
<option value=2>two
<option value=3 selected>three
<option value=4>four
<option value=5 selected>five
</select>
Presumably you generate the select menu using PHP and a query to the database so to accomplish that ( highlighting ) in PHP you would need a loop to iterate through each value in $list["Banks"] and check the value against the value from sql. Without knowing exactly how you generate the select menu or knowing the value of $list['Banks'] it is hard to know exactly and I may be "barking up the wrong tree" as it were.
Posting the selected html option attribute via php..Here is my html code and I'm trying to select one option from the event-drop down menu, but really finding it hard to configure with php variable $event. Any help will be much appreciated.
HTML Code:
<div>
<h4>Event</h4>
<p class="cd-select icon">
<select class="budget">
<option value="0">Select Occasion</option>
<option value="1">alpha</option>
<option value ="2">Bravo</option>
<option value="3">Charlie</option>
<option value="4">Delta</option>
</select>
</p>
</div>
PHP version
$event = $_POST['selected'];
In PHP form fields are posted with their name. In your case you'd have to add the name attribute to the select.
<select class="budget" name="selected">
<option value="0">Select Occasion</option>
<option value="1">alpha</option>
<option value ="2">Bravo</option>
<option value="3">Charlie</option>
<option value="4">Delta</option>
</select>
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>