PHP getting value from a dynamic select list - php

I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks

You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>

shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>

Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"

Related

PHP drop down list from db, view selected post and UPDATE post

Hi I would like to have it to work with two fields, both id and text but I cant even get this to work.
I get a post from the database to update.
If I have a ordinary optionlist. It views the selected value and I can change option and save it to the database.
This works:
<select name="ddlLinkType" title="<?php echo $row_visa_link['LinkType']; ?>">
<option value="" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == '' ) echo 'selected = "selected"'; ?>></option>
<option value="Styrelsen" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Styrelsen') echo 'selected = "selected"'; ?>>Styrelsen</option>
<option value="Alla" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Alla') echo 'selected = "selected"'; ?>>Alla</option>
<option value="Option3" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Option3') echo 'selected = "selected"'; ?>>Option3</option>
</select>
But I would like to get the options dynamic from database table. I have tried many things now but nothing works. I am so confused and everything is messed up right now. I am trying to generate the optionlist without loosing the function that it shows the selected value from the post to be updated.
This doesnt work:
<select name="ddlLinkCategory" title="<?php echo $row['LinkCatID']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategoy)) { ?>
<!-- Dont know how to solve this-->
<option value="<?php($row['LinkCatID'])?>" <?php if (!empty($row_visa_link['LinkCategory']) && $row_visa_link['LinkCategory'] == $row['LinkCatID']) echo 'selected = "selected"'; ?>>$row['LinkCatID']</option>
<?php }} ?>
</select>
Thank You.
This code is wrong:
<select name="ddlLinkCategory" title="<?php echo $row['LinkCatID']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategoy)) { ?>
You dont acces to <?php echo $row['LinkCatID']; ?> in line 1, $row is defined in line 2.
Thank You! Now it Works :)
<select name="ddlLinkCategory" title="<?php echo $row_visa_link['LinkCategory']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategory)) { ?>
<option value= "<?php echo($row['LinkCatID'])?>"<?php if (!empty($row_visa_link['LinkCategory']) && $row_visa_link['LinkCategory'] == $row['LinkCatID']) echo 'selected = "selected"'; ?>> <?php echo $row['LinkCatID']?></option>
<?php }} ?>
</select>

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

HTML Option tag with selected value from PHP

Can you please help me out as to what I am doing wrong with this code:
<option value="">------------ Select ------------</option>
<?php while (($row = mysql_fetch_array($tech))) {
?>
<option value= <?php echo $row['id'] ?>
<?php echo $selectedtechname ?>
<?php echo "selected" ?>
>
<?php echo $row['technician_name'] ?></option>
<?php } ?>
All variables are working fine since I used Var_dump and they echo correctly so no errors is SQL etc. What I need to do is pass a php statement in the option tag of HTML to simply select the value of $selectedtechname from the list. The value to post is $row['id'] but the selected value to show should be $selectedtechname
Thanks
I see a similar post
populate a select box with php mysql
while($row = mysql_fetch_assoc($result)) {
if ($row['technician_name'] == $selectedtechname) {
echo '<option value=\"'.$row['id'].'" selected>'.$row['id'].'</option>';
} else {
echo '<option value=\"'.$row['id'].'">'.$row['id'].'</option>';
}
}
You can try the following:
$selected='';
<option value="">Select a value</option>
<?php
while ($row = mysql_fetch_array($tech)) {
?>
<?php
if($selectedtechname == $row['selectedtechname'])
{
$selected='selected';
}
else
{
$selected='';
}
?>
<option value="<?php echo $row['id']?>" <?php echo $selected;?> ><?php echo $row['value_to_display'];?></option>
<?php
}
?>

Highlighting multiple selections on a form after submitting

This select box below does remember and highlight -one- selection after submitting the form. But when i make it multiple, it doesn't highlight any of the selectionS after submitting.
Any idea about how to achieve this?
Thanks in advance.
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if ($no_way == 'all') { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if ($no_way == $option) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
$_GET['no_way'] only handles single parameters you have to use $_GET['no_way[]'] and in_array($option, $no_way)
This works for me:
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if (in_array("all",$no_way)) { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if (in_array($option,$no_way)) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
I'm not sure if this will help or not, but is there any chance you've tried just using selected instead of selected="selected"?
<option <?php if ($no_way == $option) { ?> selected<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option>

How to set selected item when looping through php array

I have an html dropdown box. Then I use an array to fill the items in it.
The keys in this for each loop is just a number from 0 - 9. My problem now is how can I control what shows up as the default choice:
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>" selected="<?php if($k==1){ echo "selected"; } ?>"><?php echo $k + 1; ?></option>
<?php } ?>
In this code, you can see that I'm attempting to make the 2nd item as the default choice.
But it seems like I'm always brought to the last array item whatever number I type as the condition.
Try this:
<?php foreach($cat_r as $k=>$c){ ?>
<option value="<?php echo $k + 1; ?>" <?php if($k==1){ echo 'selected="selected"'; } ?>><?php echo $k + 1; ?></option>
<?php } ?>
Or, this format works too
<option value="foo" selected />
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>"
<?php if($k==1){ echo "selected=\"selected\""; } ?>>
<?php echo $k + 1; ?>
</option>
<?php } ?>
I came here for finding solution to set options selected by user before as selected , above question was in different context and I ended up in scratching my head to this thing...
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}
This code was functional for me, thanks..
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}

Categories