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'>
Related
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.
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.
Is it possible to automatically get first item from drop down list to the input on page load? For example, the drop down list have 2 options: happy& sad. When the page loads, I want happy to be in the input on page load. So far, I got an empty input where the user need to select the option on the drop down list.
Jquery code:
<script type="text/javascript">
$(document).ready(function() {
$("#username_select").change(function() {
$("#username_custom").val($(this).val());
});
});
</script>
username_custom id:
<input id="username_custom" name="custom" value="
<?php
if(isset($_POST['getusername'])) {
$getusername = $_POST['getusername'];
echo "".$getusername."";
}
if(empty($_POST['username'])) {
echo "".$row['username']."";
}
?>
">
username_select id:
<select id="username_select" name="getusername">
<?php
$username = $_SESSION['username'];
$result = mysql_query("SELECT username FROM account WHERE websiteusername='".$username."'");
while($row = mysql_fetch_array($result)) {
echo "<option value='".$row['username']."'>".$row['username']."</option>";
}
?>
</select>
To show the selected value of select in input
$("#YourInputId").val($("#username_custom").val());
To set the first option text of select
$("#YourInputId").val($("#username_custom option:eq(0)").text());
To change the textbox value on change of select
$(document).ready(function() {
$("#username_select").change(function() {
$("#username_custom").val($(this).val());
$("#YourInputId").val($("#username_custom").val());
});
});
In this line
echo "<option value='".$row['username']."'>".$row['username']."</option>";
you can add SELECTED to the option you want to get selected. You can use a variable in the while loop to find out which item you are at and get that item selected;
For ex.
$ctr = 0;
while($row = mysql_fetch_array($result)) {
echo "<option value='".$row['username']."'" . ( $ctr == 1 ? "selected" : "" ) . ">".$row['username']."</option>";
$ctr++
}
Hope this helps.
Cheers
In this case you can also call change event of drop down.
$("#username_select").change();
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.
this is my code which populates a drop down menu, all working perfectly, but when editing a database record, i want the first value in the drop down to be what is currently in the database, how would i do this?
<li class="odd"><label class="field-title">Background <em>*</em>:</label> <label><select class="txtbox-middle" name="background" />
<?php
$bgResult = mysql_query("SELECT * FROM `backgrounds`");
while($bgRow = mysql_fetch_array($bgResult)){
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
?>
</select></li>
You would set the selected="selected" attribute on the relevant <option>. Presumably there would be some sort of check in your while loop, checking against the variable that contains the current value.
You can do like:
$counter = 1;
while($bgRow = mysql_fetch_array($bgResult)){
if ($counter === 1)
{
echo '<option value="'.$bgRow['name'].'" selected="selected">'.$bgRow['name'].'</option>';
}
else
{
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
$counter++;
}
As can be seen I have added selected="selected" for you so it will work automatically for you :)
Correct me if I'm wrong, I believe that you have another database table which holds the selected background (e.g. users table with background field), you need to do a query to get the background from the other table and add selected="selected" attribute to the option tag of the background, please check the code below (hope it helps):
<?php
$result = mysql_query("SELECT `background` FROM `users` LIMIT 1");
$myBg = mysql_fetch_array($result, MYSQL_ASSOC);
$bgResult = mysql_query("SELECT * FROM `backgrounds`");
while($bgRow = mysql_fetch_array($bgResult)){
if($myBg['background'] == $bgRow['name'])
echo '<option value="'.$bgRow['name'].'" selected="selected">'.$bgRow['name'].'</option>';
else
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
?>