I'm trying to retrieve data from a SQL DB when the user types a category ID in to search with. It should then autofill (populate) the html form data with the retrieved data from the row's columns (upon pressing the submit button).
Everything is working properly, but I don't know how to change a dropdown menu
For example:
<input name="prodname" type="text" value="<?php echo $pName; ?>"/>
Fills the form value successfully after I press the search(submit) button.
This following code does not change the drop down values at all (<select> vs <input>)
<select name="supplierID" value="<?php $suppID; ?>">
How can I change the drop down values of a while only using HTML/PHP?
Thank you.
To select a option in <select> tag you need to set selected attribute to that option. The following code may help you to understand.
<select name="supplierID">
<option value="1" <?php if($suppID == 1) echo 'selected'; ?> >1</option>
<option value="2" <?php if($suppID == 2) echo 'selected'; ?> >2</option>
<option value="3" <?php if($suppID == 3) echo 'selected'; ?> >3</option>
<option value="4" <?php if($suppID == 4) echo 'selected'; ?> >4</option>
</select>
Hope this helps.
To select the values in drop down, the selected attribute is required. You may check with the code below:
<select name="supplierID">
<option value="1" <?php if($id == 1) echo "selected"; ?>> A </option>
<option value="2" <?php if($id == 2) echo "selected"; ?>> B </option>
<option value="3" <?php if($id == 3) echo "selected"; ?>> C </option>
</select>
Related
I am using sumoselect (http://hemantnegi.github.io/jquery.sumoselect/sumoselect_demo.html) and CodeIgniter. I am displaying data from the database. I am inserting product type multiple data in the database like
producttype
1
1+2
1+2+3
I am displaying like this on HTML page
<select name="producttype[]" class="form-control multipleselect event_stop" id="producttype" multiple="multiple">
<option value="" selected disabled>Select</option>
<option value="1" <?php if($post->producttype =="1") echo 'selected'; ?>>One</option>
<option value="2" <?php if($post->producttype =="2") echo 'selected'; ?>>Two</option>
<option value="3" <?php if($post->producttype =="3") echo 'selected'; ?>>Three</option>
<option value="4" <?php if($post->producttype =="4") echo 'selected'; ?>>Four</option>
</select>
If I am getting 1 from the database then select dropdown displaying One but sometimes I am getting 1+2 or 1+2+3 then how can I show in the select dropdown?
Would you help me out in this?
I don't have any knowledge of sumoselect but it may help you to show selected multiple options as
$producttype_array = explode("+","1+2+3");
<select name="producttype[]" class="form-control multipleselect event_stop" id="producttype" multiple="multiple">
<option value="1" <?php if(in_array(1,$producttype_array)) echo 'selected'; ?>>One</option>
<option value="2" <?php if(in_array(2,$producttype_array)) echo 'selected'; ?>>Two</option>
<option value="3" <?php if(in_array(3,$producttype_array)) echo 'selected'; ?>>Three</option>
<option value="4" <?php if(in_array(4,$producttype_array)) echo 'selected'; ?>>Four</option>
</select>
For the 'wachtwoord' input field, the value is the current database record. But how can I show the current record for the 'Jaar' select field? It's currently showing one, but it has to be two.. Anyone know how to solve this?
<label for="password">Wachtwoord</label>
<input type="password" id="password" name="password" value="<?php echo $t['gids_password'] ?>"/>
<br/>
<label for="jaar">Jaar</label>
<select name="jaar" id="jaar">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
You would need to compare the result from the database with the value of the options. If it matches, you need to echo 'selected'; Like so:
<option value="1" <?php if($t['gids_jaar']==1){ echo 'selected'; } ?> >1</option>
<option value="2" <?php if($t['gids_jaar']==2){ echo 'selected'; } ?> >2</option>
<option value="3" <?php if($t['gids_jaar']==3){ echo 'selected'; } ?> >3</option>
NOTE:
$t['gids_jaar'] would be the value of the database. Dont forget to put space between the closing php tag and the end of the option open tag. So this part has to have a space:
?> >3</option>
there has to be a space between the ?> and >
The answer of Daan works as well but I just wanted to add some additional information.
You need to add a selected to one of your option tags, most easy is with a ternary operator:
<option value="1" <?php echo ($t['myvalfromdb'] == 1) ? 'selected' : ''; ?> >1</option>
<option value="2" <?php echo ($t['myvalfromdb'] == 2) ? 'selected' : ''; ?> >1</option>
<option value="3" <?php echo ($t['myvalfromdb'] == 3) ? 'selected' : ''; ?> >1</option>
I am currently working on a edit form with drop down list. I have a edit form that display current item from database. But I have no idea how my drop down menu to display current value from database that match drop down list. I know my explanation a bit confuse therefore I picture below will show better explanation.
This is one of my edit form that showing drop down menu. What I want is display database value with this view of drop down menu.
Example :
My database value is Pending. Then it will show Pending inside this view of drop down menu. And when I click on the drop down menu, it will show me like below picture :
this is what I am looking for. And below is the code that I tried.
<div class="floatleft">
<select name="select2" class="select-form-standard" >
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>
</div>
But I just know how to design a drop down menu and do not have idea how to work like this. I am working with smarty framework. Can someone give me some solution to work on it? Thanks in advanced.
Add selected attribute for the option which must be selected. Assume $value = 0
<?php $value = 0; ?>
<select name="select2" class="select-form-standard" >
<?php foreach($options as $id => $option) { ?>
<option value="<?php echo $id ?>"<?php
if($id == $value) {
echo " selected";
} ?>><?php echo $option ?></option>
<?php } ?>
</select>
Try this:
$statusval is variable coming from your database
<option value="0" id="0" <?php if($statusval==0){echo "selected;"}?> name="status">Deleted</option>
<option value="1" id="1" <?php if($statusval==1){echo "selected;"}?> name="status">Active</option>
<option value="2" id="2" <?php if($statusval==2){echo "selected;"}?> name="status">Pending</option>
<option value="2" id="2" <?php if($statusval==3){echo "selected;"}?> name="status">Suspended</option>
<?php
$databaseValue = $your_database_value;
?>
<select name="select2" class="select-form-standard" >
<option value="0" <?php echo (($databaseValue==0)?"selected":"") ?> id="0">Deleted</option>
<option value="1" <?php echo (($databaseValue==1)?"selected":"") ?> id="1">Active</option>
<option value="2" <?php echo (($databaseValue==2)?"selected":"") ?> id="2">Pending</option>
<option value="3" <?php echo (($databaseValue==3)?"selected":"") ?> id="3">Suspended</option>
</select>
Try this
/**
* Takes To values (First for option value Second for value to be compare)
* #param Srting $option stores Option Value String
* #param String $value Stores to be compare
* #return String
* #access public
*/
function selectBoxSelection($option, $value) {
if ($option == $value) {
return "selected";
}
}
<select name="status">
<option value style='display:none;'>-SELECT Status--</option>
$select_data=mysql_query("Select status,options from yourtable");
while($result=mysql_fetch_array($select_data)){
echo "<option value ='YourValue' ".selectBoxSelection('YourOptionValue', $result['status']).">".$resule['option']."</option>";
}
</select>
<select name="select2" class="select-form-standard" >
<option><?php echo $select2e; ?></option>
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>
You need to add the attribute "selected" to the option you wish to have preselected:
<option value="2" id="2" name="status" selected>Pending</option>
or if you are using xhtml you will need to use
selected="selected"
To have this work off a value in database you will need to write an if statement to determine if the database value is the same as the option value, and if it is, then echo out the "selected" attribute within the option tag.
you can easily do it by a simple if statement. inside your loop try this
<option value="<?= $row['id']) ?> " <?= $row['id']==$categoryIDfromDatabase?'selected':''; ?> >
<?= $row['categoryName'] ?>
</option>
I have a profile page in php for the user to set up his profile. The profile page prompts the user to set the gender.This is the code:
<form action='database.php' method=post>
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male">male</option>
<option value="female">female</option>
</select>
<input type=submit>
</form>
after the user selects the gender, the following code gets the selection
$myvalue=$_POST['myvalue'];
and then by using a function stores value in database.
Next time that the user will visit his profile page, I want the dropdown list to show the value that selected before. For example lets say that the user selects as gender "male" the first time created his profile. If he visits again the profile page, the dropdown list must have automated as selected value, the value "male".
Try this :
<?php
$gender = /// fetch value from database
?>
<form action='database.php' method=post>
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" <?php if($gender == "male"){?> selected="selected" <?php }?>>male</option>
<option value="female" <?php if($gender == "female"){?> selected="selected" <?php }?>>female</option>
</select>
<input type=submit>
</form>
To set a option field as default selected you can use the selected keyword
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" selected>male</option>
<option value="female">female</option>
</select>
if you use xml document type you have to use
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" selected="selected">male</option>
<option value="female">female</option>
</select>
it's up to you to load values from cookie or database for that user and then perform checks to echo the "selected" value into that option field
Just check agains value and show selected="selected" in html
<?php
$value = $_POST['myvalue'];
?>
<form action='database.php' method=post>
<select name="myvalue">
<option value=" " <?php echo $value==' ' ? 'selected="selected"' : ''; ?> >--select--</option>
<option value="male" <?php echo $value=='male' ? 'selected="selected"' : ''; ?> >male</option>
<option value="female" <?php echo $value=='female' ? 'selected="selected"' : ''; ?> >female</option>
</select>
<input type=submit>
</form>
Something like this:
<form action='database.php' method=post>
<select name="myvalue">
<option value=" ">--select--</option>
<option value="male" <?php echo $gender=="male"?"selected":""; ?>>male</option>
<option value="female" <?php echo $gender=="female"?"selected":""; ?>>female</option>
</select>
<input type=submit>
</form>
But better would be to use a decent template system.
add selected attribute to the option
<select name="myvalue">
<option value=" ">--select--</option>
<?php
$selected = 'selected="selected" ';
foreach(array('male', 'female') as $v){
echo '<option '.(($v==$dbgender)? $selected : '').'value="'.$v.'">'.$v.'</option>';
}
?>
</select>
$dbgender is the value that stored for that user.
Do validation like this.
$val = value_from db;
if($val == "male") {
echo '<option value="male" selected="selected">male</option>';
} elseif($val == "female") {
echo '<option value="female" selected="selected">female</option>';
}
I have a form where I post some options to a mysql query. When it returns, it a draw a table with some data.
How to set the select box's option to be selected if my form returned?
I tried this, but not working:
<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1" if($ceg==1){ print 'selected'; }>Magánszemélyek</option>
<option value="2" if($ceg==2){ print 'selected'; }>Cégek</option>
</select>
</form>
You forgot to open and close php tags:
<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1"<?php if($ceg==1){ print ' selected'; }?>>Magánszemélyek</option>
<option value="2"<?php if($ceg==2){ print ' selected'; }?>>Cégek</option>
</select>
</form>
Try it like this:
<?php if($ceg==1){ echo 'selected="selected"'; } ?>
Also make sure what you get in $ceg var.
<option value="<?php echo $ceg;?>"
<?php if($ceg==1){ echo 'selected="selected"'; }?>>Magánszemélyek</option>
<option value="1" <?php if($ceg==1){ print 'selected'; }?> >Magánszemélyek</option>