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>
Related
I created my profile page with show all data in form from MySQL. All data is show rightly on form and also in drop-down list. But the problem is selected value is shows two time in the option list.
Here is the my code:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
echo "<option value='$country[1]'>$country[1]</option>";
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1])
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}
?>
</option>
</select>
You need to change your while code like below:-
while($country = mysqli_fetch_array($result)){
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1]){
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}else{
echo "<option value='$country[1]'>$country[1]</option>";
}
}
Note: In your code first option is created and then condition is checked, that's why two times it will show the selected option.
The correct answer to the issue you are facing is provided by A-2-A.
Additionally
You are nesting all of your looped options inside your "Select Country" option. you should remove the last</option> tag before the </select> tag and move it after "Select Country" like so:
<option value="">Select Country</option>
This should fix your issue:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
echo "<option value='$country[1]'".($row[4] == $country[1] ? " selected" : "").">$country[1]</option>";
}
?>
</option>
The problem was that you had echoed the selected option additionally to the non-selected option. Now, it adds the 'selected' attribute if the option should be selected. (Based on your condition)
It should be set up like this:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1]){
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}
else{
echo "<option value='$country[1]'>$country[1]</option>";
}
}
?>
</option>
</select>
Because you need to check if the value is selected, then if it isn't display the data accordingly.
You have two issues:
using option inside the option.
second you just need to print selected attribute instead of printing complete option.
Example:
<option value="">Select Country
</option>
<?php
//Get country list from Country master $qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result)) {
if($row[4] == $country[1]) {
$selected = 'selected=""';
}
else{
$selected = "";
}
?>
<option <?php echo $selected;?> value='<?php echo $country[1];?>'>
<?php echo $country[1];?>
</option>
<?php
}
?>
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>
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 :)
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
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>";
}
}