set select <option> as selected in form - php

I have a form wich inserts some data in a mysql database.
This form contain a select and some options with their respective values like
<select name="car_type">
<option value="sport">Sports car</option>
<option value="van">Van</option>
<option value="large">Large family sedan</option>
<option value="small">Small city car</option>
</select>
The form can also be used to update a car's details in the database, it does so by loading the values from the database and fills the form automatically but I am stuck at making the <option> in the select, selected by default based on the value already set in the DB.
So if the user chooses to edit a car, lets say a car that already has Sports type filled in the DB, I want the form to automatically set the <option value="sport">Sports car</option> as selected, <option selected="selected" value="sport">Sports car</option>. By not doing this, the user has to choose again the type every time he submits the form, otherwise the first <option> and its value (sport) is sent by POST.
I am able to retrieve the value from the database by using $data['type'] but I did not find the exact php code to set the selected <option> to that in the database, can you guys help ?

Although the code looks messy, you can do something like this:
<select name="car_type">
<option value="sport" <?php if($data['type']=='sport') echo "selected='selected'"; ?> >Sports car</option>
<option value="van" <?php if($data['type']=='van') echo "selected='selected'"; ?>>Van</option>
<option value="large" <?php if($data['type']=='large') echo "selected='selected'"; ?>>Large family sedan</option>
<option value="small" <?php if($data['type']=='small') echo "selected='selected'"; ?>>Small city car</option>
</select>

<option value="large" <?php echo ($dbvalue=="large") ? "selected=\"selected\"" : "" ;?>>Large family sedan</option>
Work out the sql query bit yourself and replace my $dbvalue with the column data from your db

If you're trying to mark the active car type I would do it like this:
foreach ($car_types as $car_type){
echo '<option value="'.$car_type.'" '.($data['type']==$car_type?'selected':'').'>'.$car_type.'</option>';
}

Related

Update Table if value not exists

I have two select box which values are
<select>
<option value="">I</option>
<option value="">II</option>
<option value="">III</option>
</select>
<select>
<option value="">I</option>
<option value="">II</option>
<option value="">III</option>
<option value="">IV</option>
<option value="">V</option>
</select>
My Scenario is when i select a class from first select box then after i select class from second select box.It will update the student Class "If value not exist in table." otherwise i will show error like this "Class already exist".
For Example
If i want to update student from "I" to "II" then value of 2nd select box should not in table.. please somebody help me.
There is one problem your not setting value and its empty. By which the values you select wont be added or updated to your database.
The second is you have not given the name to your select dropdown by which even if you select the values it wont be posted to your action.
<form action="update_student.php" method="post">
<select name="from_class">
<option value="I">I</option>
<option value="II">II</option>
<option value="III">III</option>
</select>
<select name="to_class">
<option value="I">I</option>
<option value="II">II</option>
<option value="III">III</option>
<option value="IV">IV</option>
<option value="V">V</option>
</select>
<input type="submit" value="Upgrade">
</form>
Suggestion:
And even you need to validate that the student must be promoted to the next class and not the class which he is already completed.
Eg: If your upgrading the student from II to III which is totally fine but you must not allow II to I.
In case if the user has asked for this kind of workflow then atleast you must prompt with confirmation for degrading the class from II to I

how to insert value from select list into mysql database

<select id='selectCat' name='selectCat'>
<option disabled selected value='0'>Select Type</option>
<option value='book'>Book</option>
<option value='txtbook'>Text Books</option>
<option value='notes'>Notes</option>
</select>
Above is the code in html.
I want the user to select one option from the select list and want to store that value into my database. But before storing it into database i am just retrieving the selected value as shown below.
$userCaT = mysqli_real_escape_string($conn,$_POST['selectCat']);
echo $userCat;
But it is showing an empty page.
So plz get me out from this problem.
Thanks.

How to keep showing selected option from drop down list?

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>

dropdown list with 2 values

I need to create a dropdownlist which has two values "OK" and "NOK" one should be selected by default based of an value of $Status. The user should than be able to change the selection or let the default one. How can i manage this? What i have tried:
<select name="Status">
<option value="OK">OK selected="selected"</option>
<option value="NOK">NOK</option>
</select>
<select>
<option value="OK" <?php if($Status == "OK") echo 'selected="selected"';?>>OK</option>
<option value="NOK" <?php if($Status == "NOK") echo 'selected="selected"';?>>NOK</option>
</select>
You can add an id and/or name attribute to the select as you like.

optgroup get in php label associated

I have following code in a html form
<select name="category" class="input" onchange="ShowTB(this,'suggest');">
<option value="0" selected="selected">
[choose yours]
</option>
<optgroup label="Item">
<option value="SubItem1"SubItem1</option>
<option value="SubItem2">SubItem2</option>
</optgroup>
<option value="Item2">Item2</option>
<optgroup label="Item3">
<option value="SubItem4"SubItem4</option>
<option value="SubItem5">SubItem5</option>
</optgroup>
<option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>
</select>
in php i get the value of field selected with:
$category = $_POST['category'];
in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3
How to ?
Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:
<option value="Item3.SubItem5">SubItem5</option>
Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.
you could make the values arrays e.g.
<option value="Item3[SubItem5]">SubItem5</option>
so then your $_POST['category'] should return an array

Categories