echo the selected value from the dropdown menu? - php

I have a form and here is code for the dropdown menu. Can you help me make a code to show the selected value after submitting the form? im using php
<select name="professional" />
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
echo "<option value ='$prc'>$prof</option>";
}
?>
</select>

<?php
echo $_REQUEST['professional'];
?>
you might wish to use $_GET or $_POST instead of $_REQUEST, please check: http://php.net/manual/en/reserved.variables.request.php

Related

Select form - use last posted value if blank

Sorry in advance for the novice question here...
I currently have my first value as a disabled and defaulted "Select" option which then changes to the selected option when a selection is made.
However if the user submits again without reselecting, the value defaults back because the post is blank. Therefore is there a way to use the previous value if so?
<select name="test_select" style="width: 110px">
<option disabled="disabled" selected="selected">
<?php
if(!empty($_POST['test_select'])){
echo $_POST[test_select'];}
else
echo "Select Option"; ?>
</option>
<?php $sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
?><option><?php echo $row['test']; ?></option><?php }?>
</select>
Thanks in advance,
Dan
I suppose that problem is that forms are not sending disabled values.
I would edit code as following:
<select name="test_select" style="width: 110px">
<?php
if (empty($_POST['test_select']))
echo '<option selected="selected">Select Option</option>';
$sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
$selected = isset($_POST['test_select']) && $row['test'] == $_POST['test_select']
? ' selected="selected"'
: '';
echo '<option'.$selected.'>'.$row['test'].'</option>';
?>
</select>

show selected value from dropdown

Hi i have been working on a form wherein there's a dropdown menu and it's values are from the database. My problem is it doesnt show the value selected after submitting the form. what maybe the problem?
<select name="professional" />
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
echo "<option value ='$prc'>$prof</option>";
}
?>
</select>
<select name="professional" disabled/>
<option value="">Choose one</option>
<?php
$result2 = mysql_query("SELECT * FROM professional");
$i=0;
while($row2 = mysql_fetch_array($result2))
{
$prc = $row2['name'];
$p1[$i] = $prc;
$prof = $row2['prcno'] ."\t"."|\t". $row2['name'] ."\t"."|\t".$row2['profession'];
$p2[$i] = $prof;
if($_POST['professional'] == $p1[$i])
{
echo "<option selected value ='$p1[$i]'>$p2[$i]</option>";
}
else
{
echo "<option value ='$p1[$i]'>$p2[$i]</option>";
}
}
?>
</select>
It seems to me, you're not incrementing $i, so you keep overwriting $p1[0] and $p2[0] in each iteration of the while-loop.
So add $i++ at the beginning or the end of your loop - or drop the whole use of these to arrays ($p1 and $p2) and use $prc and $prof just as you do in the first code-block - or do you need them for something?
Another thing, try removing the space between value and ='$p1[$i]' - but I'm not sure if that's a problem.
Try
selected="selected"
in stead of
selected
change these lines to
echo "<option selected value ='<?php echo $p1[$i]; ?>'><?php echo $p2[$i]; ?></option>";
and do not forget to increment your $i too
Hope it will help :)

PHP : Get the value of Dropdown and pass it to another dropdown

I have a problem right now [PHP]. I have a dropdown and its loading my database for the first page, when I proceed to the next page it also have a dropdown where its also loading the my database and also I can get the value of my dropdown in the first page using an echo only.
This is the scenario:
I choose in the dropdown first page the "Letter A" and when I click the button it will proceed to the next page. The dropdown in the second page loaded the items in the database but instead of "-select-" is the first index in the dropdown I want is "Letter A" will be the first index.
This is my code in first page for drowdown:
<select name="id">
<option value="" >- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
and this is my code in second page for dropdown:
if ($get_ID != "")
{
echo "<br/>";
echo $get_ID;
//echo "show()";
}
else
{
echo "No Network Selected";
echo "<br/>";
//echo "hide()";
}
?>
<option value="">- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
Thanks in advance!
On your second page, you need to check whether the value is the same as the one you received from the first page:
echo "<option value='".$row1[fldNetname]."' " . (($row1[fldNetname] == $get_id)?"selected":"") . ">".$row1[fldNetname]."</option>";
if $get_ID, is actually your $_GET['id'] value, then just do...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."'";
if($row1['fldNetname']==$get_ID){echo "selected='selected'";}
echo ">".$row1['fldNetname']."</option>";
}
Or as one line...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."' " . (($row1['fldNetname'] == $get_ID)?"selected='selected'":"") . ">".$row1'[fldNetname']."</option>"
}
Have you tried?
changing (on the second page)
<option value="">- select -</option>
to
<option value="<?php echo $yourVar; ?>"><?php echo $yourVar; ?></option>
Then in the while loop, skip the value selected to prevent a duplicate choice.
Something like:
while ($row1 = mysql_fetch_array($q))
{
if($yourVar != $row1[fldNetname]){
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
}

Show selected value in dropdown after submit

hi guys i added dropdown field to form however after i submit the form if there is any error dropdown resets itself how can keep to value after validation thanks a lot for your any helps and idea
here is my code
<td><select id="country" name="country" style="width:150px;">
<option value="-1">Select</option>
<?php
$query = "SELECT country_id, name FROM countries ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['country_id']."\" >".$row['name']."</option>\n ";
}
?>
</select></td>
<td><?php echo $form->error("country"); ?></td>
Typically you would set the default option with attribute selected that is tied to the current selected value. So in this case, the option that equals the value of $_POST['country']:
while ($row = mysql_fetch_array($result))
{
if ($row['country_id'] == $_POST['country'])
$selected = "selected=\"selected\"";
else
$selected = "";
echo "<option value=\"".$row['country_id']."\" $selected>".$row['name']."</option>\n ";
}
Which would render as the following on the appropriate option:
<option value="123" selected="selected">456</option>

display from database to dropdown list

I have stored value from drop down list to database. I want to echo the same value to be displayed in that drop down list in my edit form. how can I achieve that in php?
Region
<select name="stf_region">
<option>Select</option>
<option value="1">MDU</option>
<option value="2">TMM</option>
</select>
i have stored in database using value of selection
but i dont know to display that value in same drop down
Use something like this:
<?
$sql = "SELECT id, description FROM dropDownTable";
$rs = mysql_query($sql);
?>
<select name="dropDown">
<option value="-1">Please select...</option>
<? while ($obj = mysql_fetch_object($rs)) { ?>
<option value="<?= $obj->id; ?>" <? if ($data['downDown'] == $obj->id) echo "SELECTED"; ?>>
<?= $obj->description; ?>
</option>
<? } ?>
</select>
Please note $data needs to be set as an associative array containing attributes of the entity that is being edited. This code is flexible because in the case of a form where a user may have submitted an incomplete form $data could be set to the $_POST variable and so all entered fields can be included without the user needing to re-specify fields they previously filled in. This basically means your form template, inserting an entry and editing an entry can be the same!
Well you could do like this
$query = "select id, label from lookup_table";
$result = mysql_query($query);
$html = "<select name='yourname'><option value="">Please select...</option>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$html .= "<option value='$row[id]'>$row[label]</option>";
}
$html = "</select>";
echo ($html);//Display the select in the page
If you're able to get db data into array, you can work with them like this:
<?php
$options = array('label 1' => 'value 1', 'label 2' => 'value 2');
echo "<select name=somename>";
foreach($options as $key => $value){
echo "<option value=" . $value . ">" . $key . "</option>";
}
echo "</select>";
?>
i used the COOKIE to match the value that should be selected when it comes to edit the form.
<?php
while($result_row=mysql_fetch_array($result)){
if ( $_COOKIE['MY_COOKIE'] == $result_row[importance_level_id )
{
echo "<option SELECTED=\"SELECTED\" value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
else
{
echo "<option value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
?>
Assuming you are ok with selecting the value out of the database into a PHP variable (let's say $region) I think you are just after
Region
<select name="stf_region">
<option>Select</option>
<option value="1"<?php echo ($region == '1') ? ' selected="selected"' : ''; ?>>MDU</option>
<option value="2"<?php echo ($region == '2') ? ' selected="selected"' : ''; ?>>TMM</option>
</select>
This is the most concise answer to the question that I think you are asking. However this is a very specific answer and assumes that your dropdown values are hard-coded and won't really be changing.
If you want a more flexible setup that retrieves the values of the dropdown from the database you are looking for something more along the lines of what has been suggested by Gordon Murray Dent above
<div class="form-group has-success col-md-6">
<label class="control-label" for="state-success">Select Buyer</label>
<select id="state-success" class="form-control ">
<?php
$sql="SELECT full_name FROM `new_customer`";
$data=mysqli_query($dbcon,$sql);
?>
<option>Select byer...</option>
<?php while($row1=mysqli_fetch_array($data)){?>
<option value="<?php echo $row1['full_name'];?>"><?php echo $row1['full_name'];?></option>
<?php } ?>
</select>

Categories