I am trying to get a values from the drop down displayed from table .
i have tried in diff ways but i don't know where i have gone wrong.
Can any one help me on this..
Below is my code.
<tr class="form-field" id="appid">
<div>
<th valign="top" scope="row" >
<label for="country"><?php _e('country', 'custom_table_example')?></label>
</th>
<td>
<select id="country" name="country" class="code" >;
<option value="">select country</option>
<?php
global $wpdb;
$coun_name = $wpdb->get_col("select country_name FROM countries") ;
//print_r($coun_name);
foreach($coun_name as $a)
{
echo '<option value="'. strtolower($a) .'" />' . "$a </option>";
}
?>
</td>
</div>
</tr>
The above code is displaying values into drop down.
now the problem is i need to get the selected values.
echo '<option value="'. strtolower($a) .'"<?php echo $item['country']==".$a."?'selected="selected"':'' ?> />' . "$a </option>";
$item is the variable where i am storing all data.
country =name attribute.
Have you set $item from $_POST? Are you using POST as your form action? Do something like this:
<form name='countryTest' method='POST' action='<?/*where your action is going to*/?>'>
<select name='country'>
<?foreach($coun_name as $c){
?><option value='<?echo$c;?>'<?if($_POST['country']==$c)echo' selected="selected"';?><?
}?>
</select>
</form>
OR! If you want to be really cool (yea ok not so cool but nerdy)! Make a function or class function to do all this for you!
class formHelper{
public function select_form($name,$options=array([0]=>'Please select'),$selected=array(),$multiple=false){//name of select, options, selected options, multiple select
if(!is_array($selected))$selected=array($selected);
$sel='<select name="'.(($multiple===true)?$name.'[]':$name).'"';
if($multiple===true)$sel.=' multiple';
$sel.='>';
foreach($options as $value=>$shown){
$sel.='<option value="'.$value.'" '.((in_array($value,$selected))?'selected="selected"':'').'>'.$shown.'</option>';
}
return$sel.='</select>';
}
}
Now to use it just do this
$coun_name=array(merge(array('Please select a country'),$coun_name));
formHelper::select_form('country',$coun_name,$_POST['country']);
EDIT
your error is you've set your value to lower but when you're comparing it's not lowered. See strtolower. What you want to do is compare both as lower as $item will be lower. I'd recommend using an integer when comparing like this:
array(
[1]=>'England',
[2]=>'Wales',
[3]=>'Scotland'
);
So that your values will be
<option value='1'>England</option>
<option value='2'>Wales</option>
<option value='3'>Scotland</option>
But ye your issue is $item['country']==$a. Needs to be $item['country']==strtolower($a). And remove the string quotes with the full stops. "england" does not equal ".England.". The reason it's "england" already is because you've already set the string to lower. Unless $item is not $_POST['county']'
Related
I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?
I am printing a simple select list out by looping through an associative array which is populated elsewhere.
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This select list gives the options available from the array. However, I am trying to pre-populate the list with the selected value, for example if a user is updating their user information, then I would like to set that choice at the top of the list (ideally without the choice appearing further down the list).
I have access to this data in a variable which I have queried:
$this->selected_choice //value
$this->selected_id //key
Now I have been testing some solutions
<select id="choice" name="choice">
<option name="<?php echo $this->selected_choice; ?>" value="<?php echo $this->selected_id;?>">"<?php echo $this->selected_choice; ?></option>
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This solution does display the users choice on top of the list, but the foreach loop will also display this option as there is no indication that it shouldn't.
This is my question to you guys, how can I do this so the the selected_choice appears at the top of the list, but will then not appear in the foreach array.
I am hoping there is some simple solution to this problem, as it is not so important, but something I come across quite a lot.
Option have no "name". Name is attribute for SELECT tag.
If option is selected, you need to pass "selected" attribute in body of option
Ex: <option value="1" selected>Tramtada</option>
Do it this way:
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option <?php if($key == $this->selected_id) {?> selected <?php } ?> value=".$key.">" .$value. "</option>" ;
}
?>
</select>
I have a form which allows a user to edit a record. One of the items in the form is a drop down menu populated with values from a database. I was wondering if there were a way to set the default value of the drop down to the value held in the database for that record instead of automatically just going to the first item in the list (I am hoping this makes sense).
<td><label for ="genre">Image Genre: </label></td>
<td><select name="genre">
<?php echo'<option value="'. $row['genreID'] .'">'. $row['genreName'] .'</option>'; ?>
<?php while($genre_item = mysqli_fetch_array($genre_sql)){
echo'<option value="'. $genre_item['id'] .'">'. $genre_item['genreName'] .'</option>';
}
?>
</select></td>
As you can see I have put an option in to populate the first value with the value held in the database. The problem with this is that the genreName then appears twice.
Are there any solutions to this?
<td><label for ="genre">Image Genre: </label></td>
<td><select name="genre">
<?php $genre_item = mysqli_fetch_array($genre_sql); ?>
<?php foreach($genre_item as $item){
echo'<option value="'. $item['id'] .'">'. $item['genreName'] .'</option>';
}
?>
</select>
</td>
if i understood you right ....
Bearing in mind this question is more than two years old, it doesn't have an accepted answer...
Use the HTML tag <option> attribute selected:
selected
If present, this Boolean attribute indicates that the option is initially selected. If the element is the descendant of a element whose multiple attribute is not set, only one single of this element may have the selected attribute.1
Integrating this with the PHP code:
<?php while($genre_item = mysqli_fetch_array($genre_sql)){
$selected = '';
//modify this condition per your needs -
if ($genre_item['id'] == $row['genreID') {
$selected = 'selected';
}
echo'<option value="'. $genre_item['id'] .'" '.$selected.'>'. $genre_item['genreName'] .'</option>';
When the selected option is found, the output should appear like below:
<label for ="genre">Image Genre: </label><br />
<select name="genre">
<option value="ind">Indie</option>
<option value="rock" selected>Rock</option>
<option value="srock">Soft Rock</option>
<option value="tech">Techno</option>
</select>
1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
My update statement isn't working correctly, I am attempting to pull the data from the database, populate a dropdown with either "Y" or "N" inside it, on submit the values are entered into the database and the page refreshes.
So far I have my list of items, each with correctly populated dropdown, it is now my submit that is failing to work.
<?php
$updatedFeatProd = $_POST['featuredProduct'];
var_dump($updatedFeatProd);
if ($_POST) {
foreach ($_POST['featuredProduct'] as $key => $val) {
$query = 'UPDATE tblProducts SET featuredProduct = ' . $updatedFeatProd . '
WHERE fldID = ' . $val;
$sql = dbQuery($query);
}
}
$sql = dbQuery('SELECT fldId, fldName, featuredProduct FROM tblProducts');
?>
<form method="post" action="#" name="featuredProd">
<table>
<tr><td><p>Product Name</p></td><td><p>Is a featured product?</p></td></tr>
<?php
$products = dbFetchAll($sql);
foreach ($products as $product) {
//var_dump($product['fldName']);
?>
<tr>
<td>
<p><?php echo $product['fldName']; ?></p>
</td>
<td>
<select name="featuredDropdown">;
<?php
if ($product['featuredProduct'] == 'Y') {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">N</option>
<?php
} else {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">Y</option>
<?php
}
?>
</select>
</td>
</tr>
<?php
}
?>
The presentaton here does not make much sence. You have a dropdown with the ProductName in one slot and a 'N' in another.
Once the user has selected 'N' for a product they have no idea what they have said NO to, as they can no longer see the product name that they have selected NO for.
It would make more sence to provide a <label> containing the product name and a YES/NO dropdown beside it for them to select from.
However the reason your update code is not working is that you have called the dropdown featuredDropdown
<Select name="featuredDropdown">
and you are trying to process a field called featuredProduct in the update code
foreach ($_POST['featuredProduct'] as $key => $val) {
Your next problem will probably be that you are oututting more than one <Select name="featuredDropdown"> so you need to make that into an array as well like this:
<Select name="featuredDropdown[]">
Then you will have an array of featuredDropdown in the $_POST array. $_POST['featuredDropdown'][]
I have a form which POSTs to itselft so the user can do things that you would find in a Shopping Cart.
e.g. Increase quantity, select postage type.
My problem is for my form Select element called "postage" whenever the form reloads itself , it forgets what was selected.
All my other fields remember their values using this:
<input type="text" name="postcode" value="<?php echo $_POST['postcode']; ?> " />
How do I use the $_POST value to automatically select the option in the select field that was done by the user?
I tried this:
<select name="postage" selected="<?php echo $_POST['postage']; ?>" >
and this
<select name="postage" value="<?php echo $_POST['postage']; ?>" >
Thanks
You almost got it. You need to set the attribute selected="selected" (the exact form you need technically depends on your HTML doctype, but this is a safe default) on the <option> element if and only if the value of $postage equals the value of the element. So:
<select name="postage">
<option value="foo"
<?php if ($_POST['postage'] == "foo") echo 'selected="selected" '; ?>
>
</select>
Note that this violates the DRY principle because you now have two occurrences of the string "foo" in there, so it's a prime candidate for refactoring. A good approach would be to keep value/text pairs in an array and iterate over it with foreach to produce the <option> tags.
You need to foreach through all the options.
Make an array with all the dropdown options, loop through that, and compare with what is stored in post.
E.G.:
<?php
$aDropd = array("apple","orange","banana");
echo "<select>";
foreach($aDropd as $sOption){
$sSel = ($sOption == $_POST['postage'])? "Selected='selected'":"";
echo "<option $sSel>$sOption</option>";
}
echo "</select>";
no its not working at all..you need to put some kind of loop for that.
For Example : foreach($record => $values){
if($values == $_POST['postage']){
$selected = "selected='selected' ";
}else{
$selected = "";
}
}
<input name="postage" value="1" <?=$selected?> >
EDITED:
if($_POST['postage'] == 1){
$selected1 = "selected='selected' ";
}else if($_POST['postage'] == 2){
$selected2 = "selected='selected' ";
} and so on..........
<select name="postage">
<option value="1" <?=$selected1;?> />
<option value="2" <?=$selected2;?> />
</select>
I think this may be helpful to you..you can ask me if anything else needed...
Thanks.
The value of selected should be selected
<select name="postage" value="1" <?php echo (($_POST['postage'] == 1)?'selected="selected"':''); ?> >
Your html syntax is wrong. The correct way to write the html is like this:
<select>
<option value ="<?php echo $_POST['postage']; ?>" selected="selected"></option>
</select>
You can also make it shorter:
<select>
<option value ="<?=$_POST['postage']; ?>" selected="selected"></option>
</select>
<select name="foo">
<option value="1" <?php echo strcmp($_POST['foo'],"1")==0?"selected=\"selected\"":"" ?>>option1</option>
<option value="2" <?php echo strcmp($_POST['foo'],"2")==0?"selected=\"selected\"":"" ?>>option2</option>