set default drop down value on php generated form - php

I generated a drop down menu with the code below. How can I set the field to the GET variable after submit is clicked?
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
$options.="<option value=\"$id\">".$c;
}
?>
<option value=''>C <?=$options?> </option>
</select>

<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
if($_GET['var'] == $id)
echo '<option selected="selected" value=\"$id\">' . $c . '</option>';
else
echo '<option value=\"$id\">' . $c . '</option>';
}
?>
</select>
Basic idea is compare value GET data with database data and using if else condition add selected="selected" if condition matched. I am directly printing string as they will not be getting use later on.

I'm a bit confused about what you want to know. To have an option selected by default, use the selected="selected" attribute on that option.
If you want to know how to get the submitted value in PHP, you need to assign the name attribute to the <select> tag. In general, however, you've got the idea right.

Related

select dropdown value according to previous valuesubmit new one

I use the following code to select all value from a DB table and put it in dropdown. Here is the code :
<select name="dis_name">
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
Based on previous code a value is selected and submitted. I want to show previously selected value in dropdown on another page so i use following code:
<select name="dis_name">
<option value="RAM"<?php echo $r20 == "RAM" ? " selected" : ""; ?>>RAM</option>
<option value="ROHAN"<?php echo $r20 == "ROHAN" ? " selected" : ""; ?>>ROHAN</option>
</select>
This code work fine till no new value added in DB table. But whenever i add new value i have to update edit code manually. Is there any way, so that dropdown previous selection updated automatically in edit page.
Add the condition in your loop, eg:
<?php
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if ($r[0] == $r20) echo " selected";
echo "> $r[0] </option>";
}
?>
If I get what you want (assuming that $r20 is the previously selected value:
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if($r[0]==$r20){echo 'selected="selected"';}
echo "> $r[0] </option>";
}
?>
this way the select will display as selected the option that match the $r20 value dinamically.
BTW, you should use id in your code. This way you could also have more than one user with the same name (but different ID). With DISTINCT, as you are doing, if you have more than one user with the same name you will get only one.

Keep selected option in php

I have below code which runs query correctly but does not keep selected option. I could get similar answers but not worked with this code. Here select options are in combination of user given and rest are from mysql column.
I am new to php, so any help will be appreciated.
<select name="tester">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($conn, "Select DISTINCT tester from workflow1");
while($r=mysqli_fetch_row($dd_res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
You can use the $_POST superglobal array to check the value of submitted form fields. In this case $_POST['tester'] contains the value of the select field after submitting the form.
echo "<option value='$r[0]'".($_POST['tester']==$r[0]?' selected':'')."> $r[0] </option>";
I am thinking you are asking to select the value passed into the form? If I am correct in that, you would do something like:
$selVal = $_REQUEST['tester'];
$val = $r[0];
echo '<option value="'.$val.'"';
if ($val == $selVal) {
echo ' selected="selected"';
}
echo '>'.$val.'</option>';
The $_REQUEST gets any POST, GET or COOKIE value and should be validated to make sure no hack attempts or anything. Once you have the value, you just add the selected attribute if it matches the value in the list.

SET selected in the select tag

<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'">'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
This is Edit.php
This is the result of that code
but i want is. after getting the data from the database. it will automatically selected the value of the option base on the database data.
Suppose the query result returned from tblchurchs contains a field Selected which is either set to 1 or 0. If it is set to 1 then you have to select that option in your drop-down list, and if it is set to 0, it is not selected.
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'"';
if($get_detail['Selected'] == 1)
{
echo ' selected="selected"';
}
echo '>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
to getting selected for selected option.
first you have to save the selected data getting from database to a variable and then using an if loop you can do this.
<?php
$AChurchID=1; // this is your already selected value that is in db .
?>
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
$sel="";
if($AChurchID==$get_detail['AChurchID']){
$sel="selected";
}
echo'<option value="'.$get_detail['AChurchID'].'" '.$sel.'>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
This will work in your case. This is works fine in ma case. Please have a try.

Sticky select box with while loop

I can't seem to get this right. I am trying to make a select box that gets it values from the database sticky please see my code below :
<select name="cob_bank">
<?php while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
if($row['Id']==$myId) {
echo '<option value="'.$row['Id'].'"selected="selected">'.$row['Name'].'</option>';
} else {
echo '<option value="'.$row['Id'].'">'.$row['Name'].'</option>';
}
}?>
</select>
after the select box is filled in and the submit button is pressed where I save my Post variable
$myId = $_POST['cob_name'];
The above code does not work I please assist
the no space could be causing the problem as a space before selected
if($row['Id']==$myId) {
echo '<option value="'.$row['Id'].'" selected="selected">'.$row['Name'].'</option>';
}
The select name should be set as:
<select name='cob_name'>

echo drop down menu onto a page

I have a drop down menu with two options in html, I also created a PHP script that checks what option from the drop down menu has been selected and based on the selection executes a mysql query to fetch data from database.
But I am also trying to echo out a new drop down menu with the results obtained from database and that is where the I am struggling because no errors are diaplayed but also no drop down menu is 'echoed' out onto the page.
HTML:
<?php require "course.php" ?>
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP code:
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value=''>".$result2['course']."</option>";
}
echo "</select>";
echo '</br>';
}
Could someone point out a mistake I am doing or perhaps suggest where I could look for answers
you said $query2 is displaying value in print_r.so the only mistake i find in your code is display:none .
remove display:none
echo "<select id='Forex' name='Forex' style='display: none'>";
----------------------------------------------------------------------^

Categories