I have a working select that is generated by PHP.
Code:
echo "<form method='post'><select name='selectid'>";
$result_select = mysql_query("SELECT * FROM DB.Table");
while($row_select = mysql_fetch_array($result_select))
{
echo "<option ";
if ($row['Value'] == $row_select['Value'] ) echo 'selected';
echo ">{$row_select['Value']} {$row_select['Value']}
</option>";
}
echo "</select></form>";
Now the problem is, I want to $_POST the selected value from select. It is working, too. But I want to get the string without  
I have tried:
$text_description=" Helloworld! ";
$text_description = str_replace(' ', '', $text_description);
echo $text_description;
And it's working. But when I replace
$text_description=" Helloworld! ";
to
$text_description=$_POST['selectid'];
it's not working any more.
You can just add a value attribute to your options:
echo "<form method='post'><select name='selectid'>";
$result_select = mysql_query("SELECT * FROM DB.Table");
while($row_select = mysql_fetch_array($result_select))
{
echo "<option value='{$row_select['Value']}'";
if ($row['Value'] == $row_select['Value'] ) echo 'selected';
echo ">{$row_select['Value']} {$row_select['Value']}
</option>";
}
echo "</select></form>";
Related
I have below code which creates drop-down from php. I want to achieve 2 things here.
1. I want to set one of the option to be set default. It may be hard coded or selected from query.
2. When hit button, it should retain selected option. I could retrieve selected option using session data with this: echo $print_version1[array_keys($print_version1)[0]];
Drop-down code:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
echo '<option>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'><b>Add Base Verison</b></button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
$array_select = $_SESSION['data'];
print_r($array_select);
echo "<form>";
i assume that the option that should be selected is$print_version1[array_keys($print_version1)[0]
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($_SESSION["id"]) {
if($nx_version == "the hardcode value you want to be selected"){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}else{
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}
}
I have below code which creates drop-down from php. I want to achieve 2 things here.
1. I want to set one of the option to be set default. It may be hard coded or selected from query.
2. When hit button, it should retain selected option. I could retrieve selected option using session data with this: echo $print_version1[array_keys($print_version1)[0]];
Drop-down code:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
echo '<option>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'><b>Add Base Verison</b></button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
$array_select = $_SESSION['data'];
print_r($array_select);
echo "<form>";
i assume that the option that should be selected is$print_version1[array_keys($print_version1)[0]
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($_SESSION["id"]) {
if($nx_version == "the hardcode value you want to be selected"){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}else{
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}
}
I have been trying to work this out for a while.
I have created a form with a dropdown box that gets results from a database. from this i then $_POST that from to another page. From that second page i wish to get the ID number and then get the records and display them on screen.
I will then put them in a table to organise the results better.
can anyone help me in achieving this.
Here is the code for the form (which works and sends the $PlantID)
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
/* $plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];*/
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
I have created this page to get the ID and display that ID on screen. AS you can tell i have probably doubled up on ways to try work this out.
$sql = "SELECT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
if(isset($_POST['submit'])){
$selected_val = $_POST['Botannical_Name']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
echo "<br />";
echo "well:".$_POST["Botannical_Name"]."<br/>";
echo "now:".$plantquery."<br />";
echo $_POST;
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Any help would be greatly appreciated.
you should use following to get the selected value,
$selected_val = $_POST['flower'];
if(isset($_POST['submit'])){
$selected_val = $_POST['flower']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
$sql = "SELECT * FROM PLANTS WHERE FlowerID='.$selected_val.'";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while ($row=mysqli_fetch_assoc($result))
{
echo $row['Botannical_Name'];
}
}
echo "<br />";
print_r($_POST);
if(!empty(_POST)) {
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
This script does not display the DB value in a drop down on the edit form.
<?php
echo "<select name='assign' value=''><option>Select name</option>";
while ($r = mysql_fetch_array($result)) {
$value = $r['name'];
echo "<option value=" . $r['emp_id'] . ">" . $r['name'] . " if ($name=='$value') echo 'selected = 'selected''></option>";
}
echo "</select>";
It does not show any error. How it can write in a correct way.
You can try this :
$echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
$echoSting .= '<option value="'.$r['emp_id'].'" '.($name==$value ? 'selected' : '').'>'.$r['name'].'</option>'.PHP_EOL;
}
$echoSting .= '</select>'.PHP_EOL;
echo $echoSting;
a side note, try looking into PDO for your database stuff : http://php.net/manual/en/book.pdo.php
Try this:
echo "<select name='assign' value=''><option>Select name</option>";
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
echo "<option value='.$r['emp_id'].'>'.$r['name'].' "; if ($name=='$value') echo "selected = 'selected'";echo">$value</option>";
}
echo "</select>";
<div class="select_list">
<div class="labels">Category:</div>
<?php
if(!isset($_POST['postbtn)){
echo "<script>$(document).ready(function(){
$('.category').val(0);
})
</script>";
}
echo "<select name='category' id='catg_list' class='list_catg'>
<option value='0'";
if(isset($_POST['category']) && $_POST['category']=='0'){
echo "selected";
}
echo">none</option>";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
echo "<option value='".$query_fetch['id']."'";
if(isset($_POST['category']) && $_POST['category']==$query_fetch['id'])
{
echo "selected";
}
echo ">".$query_fetch['name']."</option>";
}
echo "</select>";
?>
</div>
The problem with the above code is that the selected element stays selected after the submission is done. I need the select option to return back to 'none' when the form is submitted successfully. How can that be done?
You have an syntax error in your line:
if(!isset($_POST['postbtn)){
change it to:
if(!isset($_POST['postbtn'])){
I'm guessing some things you didn't mention:
You validate your script above the code you posted, and you want - if the script was validated successfully - that the "none" option is selected.
$selected_value = $validation ? $_POST['category'] : false;
Only if the Validation is correct the value will be set, else it will be set to false;
<?php
echo "<select name='category' id='catg_list' class='list_catg'>
<option value='0'";
//CHANGE
if($selected_value == 0){ //Or if($selected_value === FALSE ||$selected_value === 0)
echo "selected";
}
echo">none</option>";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
echo "<option value='".$query_fetch['id']."'";
//CHANGE
if($selected_value !== FALSE && $selected_value == $query_fetch['id'])
{
echo "selected";
}
echo ">".$query_fetch['name']."</option>";
}
echo "</select>";
?>
Simplify your code as following to do required task.
$category = isset($_POST['category']) ? $_POST['category'] : 0;
$array["0"]="none";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
$id = $query_fetch['id'];
$array[$id] = $query_fetch['name'];
}
echo "<select name='category' id='catg_list' class='list_catg'>";
foreach($array as $id=>$name) {
$selected = $category==$id ? "selected" : "";
echo "<option value='".$id."' ".$selected." >".$name."</option>";
}
echo "</select>";